1

I was trying to implement cache in our springboot application. This initially was not working as this is called internally.

@Service
public class ServiceCache {
    Logger logger = LoggerFactory.getLogger(ServiceCache.class);

    public void test1(){
        logger.info("test1");
        test2(10);
    }

    @Cacheable(value="cache", key="#id")
    public void test2(Integer id){
        logger.info("test2");
    }
}

So when I call test1() from controller, cache is not working. Obviously due to proxy being created during application startup.

Now when I add @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) to the class, now it is working. Can you please let me know what exactly is happening here and is it okay to use this attribute? Thanks.

Eugene
  • 5,269
  • 2
  • 14
  • 22
DK93
  • 89
  • 1
  • 14
  • Could you please share working code? – Eugene Jul 04 '22 at 00:01
  • This problem was described several times: https://stackoverflow.com/questions/12115996/spring-cache-cacheable-method-ignored-when-called-from-within-the-same-class/34090850#34090850 https://stackoverflow.com/questions/16899604/spring-cache-cacheable-not-working-while-calling-from-another-method-of-the-s – Eugene Jul 04 '22 at 19:20
  • I feel like it really shouldn't work... all changing the proxymode achieve is using gclib proxies instead of jdk ones. Both should be incompatible with self-calls... – jeekiii Jul 09 '22 at 20:59
  • Yes, that's why I am asking for a working code example. Solutions for this problem are shared above. – Eugene Jul 09 '22 at 22:58

0 Answers0