So, I am trying to add a logging aspect to my spring caching, that will execute(at least I was hoping) every time before the @Cacheable annotation.
I have my DAO object where certain methods have the @Cacheable annotation:
@Cacheable(value = "BalanceGroupCahce", key = "#balanceGroupId")
public BalanceGroup fetchBalanceGroup(Integer userId, Integer balanceGroupId) {
//code to fetch balanceGroup from database;
return balanceGroup;
}
And I also have an aspect that defines a @Pointcut as the following:
@Pointcut(value="@annotation(org.springframework.cache.annotation.Cacheable)")
public void cacheablePointcut(){}
And a @Before advice that will get the the annotation as a parameter and will attach the logger to the appropriate cache:
@Before(value="cacheablePointcut() && @annotation(annotation)")
public void addLoggerForCacheable(JoinPoint joinPoint, Cacheable annotation){
//code to add the logger
}
So what is happening is that everything works fine as long the requested resource was NOT found in the cache. When the resource was found in t he cache neither the method nor the @Cacheable annotation is executed. Which means that also my advice is not triggered. I knew that the method is not executed if spring finds the resource in the cache, but I thought that the @Cacheable annotation will still execute thus my pointcut too.
But apparently neither the @Cacheable annotation is executed, which means someone else is checking whether the resource is in the cache or not.
So the question is how could I rewrite my @Pointcut in order to be executed every time the cache is tested/updated/evicted? Also I am very curious who is testing if the resource is in the cache or not?