@Cacheable

springBoot gradle 2017. 1. 21. 22:42

REF   REF한글


REF_NEW(good config)


spring에서

@Cacheable


@Cacheevict


@Cacheput 


을 이용해서 각종 조건을 지정하면서... 함수 리턴결과 등을 cache할 수 있다.


단, Cacheable등은 proxy형태의 AOP이므로, 별도 class를 만들어서 추가해야 한다.


@CacheConfig(cacheNames = CacheManagerConfig.DEFAULT_CACHE_NAME) public class ForCacheService {

@Cacheable(key = "'myCache'")
public Optional<Something> findSomething(){
return db.findSomething();
}

@CacheEvict(key = "'myCache'")
public void clearCache() {
log.info("clear cache: key=myCache");
}

}

 ==> 음 Optional은 serialize가 안되서 Cache가 안되는 문제 발견.

일반 class 일 경우에도 implements Serializable을 해야 한다.




null Value가 cache가 안되도록 하려면  다음과 같이 #unless를 이용하면 된다.  REF

  (unless는 함수가 실행된 이후에 평가됨.  즉, cache가 있을때는 상관없이 pass.)


@Cacheable( key="'myCache'", unless="#result == null")
public Person findPerson(int pk) {
   return getSession.getPerson(pk);
}


null Value도 cache가 되도록 하고 싶으면? ==>안됨.


Posted by yongary
,