Overivew
- encache를 활용하여 캐싱할시, 발생되는 예외에 대한 문제와 해결책을 강구 할 수 있습니다
시나리오
- 사용자들은 자신의 포트폴리오를 조회 할 수 있다
- 자신의 포트폴리오 조회시 캐싱에 저장하여, 사용자가 다음에 조회할때는 캐싱된 데이터를 이용 할 수 있게한다
- 캐싱에는 String,int,LocalDateTime등 다양한 형식의 데이터가 저장 될 수 있다
코드
@Transactional(readOnly = true)
@Override
@Cacheable(value = "portfolioCache",key = "'portfolioId'+#portfolioId")
public PortfolioResponse getPortfolio(Long portfolioId) {
Portfolio portfolio = findById(portfolioId);
List<String> filePaths = attachmentGetService.getAllAttachmentPath(portfolio.getUser().getId(), portfolio.getId(), AttachmentTargetType.PORTFOLIO);
return PortfolioResponse.PortfolioResponseGetFilePaths.of(portfolio,filePaths);
}
에러
2024-11-03T19:56:52.221+09:00 INFO 10055 --- [nio-8080-exec-2] o.e.i.i.r.RobustResilienceStrategy : Ehcache key portfolioId1 recovered from
org.ehcache.spi.resilience.StoreAccessException: org.ehcache.spi.serialization.SerializerException: java.io.NotSerializableException: com.sparta.doguin.domain.portfolio.model.PortfolioResponse$PortfolioResponseGetFilePaths
at org.ehcache.core.exceptions.StorePassThroughException.handleException(StorePassThroughException.java:80) ~[ehcache-3.10.0.jar:3.10.0]
at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.computeWithRetry(AbstractOffHeapStore.java:1046) ~[ehcache-3.10.0.jar:3.10.0]
at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.put(AbstractOffHeapStore.java:250) ~[ehcache-3.10.0.jar:3.10.0]
at org.ehcache.impl.internal.store.tiering.TieredStore.put(TieredStore.java:108) ~[ehcache-3.10.0.jar:3.10.0]
at org.ehcache.core.Ehcache.doPut(Ehcache.java:94) ~[ehcache-3.10.0.jar:3.10.0]
at org.ehcache.core.EhcacheBase.put(EhcacheBase.java:189) ~[ehcache-3.10.0.jar:3.10.0]
at org.ehcache.jsr107.Eh107Cache.put(Eh107Cache.java:175) ~[ehcache-3.10.0.jar:3.10.0]
at org.springframework.cache.jcache.JCacheCache.put(JCacheCache.java:101) ~[spring-context-support-6.1.13.jar:6.1.13]
at org.springframework.cache.interceptor.AbstractCacheInvoker.doPut(AbstractCacheInvoker.java:87) ~[spring-context-6.1.13.jar:6.1.13]
at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.performCachePut(CacheAspectSupport.java:1037) ~[spring-context-6.1.13.jar:6.1.13]
흠 원인이 뭘까 ?
- 실제 return 값이 되는 클래스에 직렬화를 설정해주지 않아서 그렇다고 한다
- 다시한번 붙여서 진행해보자

- 직렬화 클래스를 implements 해주자
그렇다면 결과는 ?

내친김에 캐싱적용하기전 vs 하고나서 시간 비교
캐싱 적용전

캐싱 적용후

결과