반응형

간단하게 메모리위에 데이터를 올려놓고 사용하고 싶은 경우 보통 ehcache를 많이 사용하게 된다.

 

이때 주의해야하는 점이 있다.

 

동일한 클래스 내에서 호출되는 메서드에 @Cacheable을 적용해도 정상적으로 동작하지 않고 무시되어져 버린다. 프록시 생성방식때문...

 

다음처럼 getSpecificRangeContents메서드 내에서 getAllContentsIdAndLocation메서드를 같은 클래스내에서 호출하게되면 정상동작하지 않는 것이다.

간단히 설명하자면 캐시는 프록시기반으로 동작하는데 프록시 클래스는 모든 요청과 응답의 인터셉터에 의해 생성되어지기 때문에 같은 클래스 내부(internal) 콜에 의해서는 캐시기능이 적용되지 않는다. 관련하여 읽어보면 프록시 동작방식과 ehcache를 사용함에 도움이 될 만한 문서를 첨부한다.

Only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual cache interception at runtime even if the invoked method is marked with @Cacheable.

ref : https://stackoverflow.com/questions/16899604/spring-cache-cacheable-not-working-while-calling-from-another-method-of-the-s/40340964

 

[ ehcache관련 읽어보면 좋은 자료 ]

Transactions, Caching and AOP: understanding proxy usage in Spring

https://spring.io/blog/2012/05/23/transactions-caching-and-aop-understanding-proxy-usage-in-spring

 

ehcache-spring-annotations - UsingCacheable.wiki

https://code.google.com/archive/p/ehcache-spring-annotations/wikis/UsingCacheable.wiki

 

해당문제가 스프링4.3에서는 self-autowiring으로 해결되었다고 한다.

 

잘알고 쓰자...

 

반응형

+ Recent posts