Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

[#45] Item 조회 메서드에 대한 로컬캐싱 적용 #46

Open
wants to merge 1 commit into
base: feature/26
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/me/jjeda/mall/items/service/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import me.jjeda.mall.items.domain.ItemCategory;
import me.jjeda.mall.items.dto.ItemDto;
import me.jjeda.mall.items.repository.ItemRepository;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -16,6 +19,7 @@

@Service
@RequiredArgsConstructor
@EnableCaching
public class ItemService {

private final ItemRepository itemRepository;
Expand All @@ -31,10 +35,14 @@ public Item saveItem(ItemDto itemDto) {
}

@Transactional(readOnly = true)
@Cacheable(value = "findItemCache",key ="#itemId")
public Optional<Item> getItem(Long itemId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이곳에 캐싱을 걸어준 이유가 같이 들어있으면 좋을 것 같습니다~

return itemRepository.findById(itemId);
}

@CacheEvict(value = "findItemCache",key ="#itemId")
public void refreshCache(Long itemId) { }

@Transactional(readOnly = true)
public List<Item> salesList(Long accountId) {
return itemRepository.findAllByAccountId(accountId);
Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir" />

<cache name="findItemCache"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

각 옵션에 대한 설명과 사용 이유도 주석으로 있으면 좋을 것 같습니다.

maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LRU가 아니라 LFU를 사용한 이유가 있으실까요?

transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>