[Spring] @Cacheable, @CacheEvict

동민·2021년 12월 31일
0

@Cacheable 데이터를 조회할 때, 레디스 캐싱 처리

// FeignClient
@Cacheable(value = "itemReview", key = "#criteria.itemCode", unless = "#result == null")
@GetMapping(".../XXX")
Json<?> getData(@SpringQueryMap Criteria criteria);
  • value: 레디스 캐시 이름
  • key = "#criteria.itemCode": 레디스 키값을 criteria DTO 내 itemCode 로 설정
  • unless = "#result == null": 결과값이 null 이면 캐싱하지 않겠다.

@CacheEvict 메소드를 수행하는 시점에서 value or cacheNames 으로 지정된 레디스 캐시 제거

// FeignClient
@CacheEvict(value = "itemReview")
@PostMapping(".../OOO")
Json<?> postData(@SpringQueryMap Criteria criteria);

@CacheEvict(cacheNames = "XXX", allEntries = true) 해당 cacheNames 을 가지고 있는 모든 캐시 엔트리를 비운다.

// FeignClient
@CacheEvict(cacheNames = "itemReview", allEntries = true)
@PostMapping(".../OOO")
Json<?> postData(@SpringQueryMap Criteria criteria);
  • 파라미터인 valuecacheNames 는 같음
  • cacheNamesapplication.yml (spring-cloud-config) 에 등록하여 사용

참고

http://blog.breakingthat.com/2018/03/19/springboot-ehcache-%EC%A0%81%EC%9A%A9/

profile
BE Developer

0개의 댓글