Announcement Announcement Module
Collapse
No announcement yet.
catch the method calls with @Cacheable annotation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • catch the method calls with @Cacheable annotation

    Hi,

    I'm working on a project that uses Spring Cache (version 3.2.3), and I want to extends it with new informations like "groups of objects" (to evict groups of objects from cache in one shot) and "expiresInMinute" (to check when the returned object from cache is invalid).

    For doing that, first of all I'd created a new annotation @CacheableInfo and I'd applied it near the @Cacheable annotation, like that:


    Code:
    @Cacheable(value = "content_cache")
    @CacheableInfo(groups = "news,genericContent", expiresInMinute = 120)
        public Content getPublicContent(String id) throws Exception {
        ...
        ...
        //my implementation
        ...
        ...
        return content;
    }

    After, I'd created a @Aspect with an advice that catches the method calls with @Cacheable annotation, like:

    Code:
    @Around("@annotation(cacheable)")
    public Object aroundCacheableMethod(ProceedingJoinPoint pjp, Cacheable cacheable) throws Throwable {
        ...
        ...
        //my implementation
        ...
        ...
        return pjp.proceed();
    }

    but, this strategy catches only the first call of methods where the @Cacheable annotation was applied, missing others... in my strategy I need to analize the returned object from the spring cache, so I need to catch also next calls.
    I get the same result if the advice is applied to the @CacheableInfo annotation.

    Any ideas what's the best way to get what I want?
    Thanks

  • #2
    I think you're asking for something impossible. If the method is not actually executed because the previously cached results are returned instead, neither do the advices associated with that method, and that is a fact.

    Well actually there is a possibly feasible way but it means messing around with the framework so you've got to consider well if it's worth it for your case...you should apply your advice not only to your cacheable methods (to intercept the actual method call), but also to the methods that the Spring framework uses when it accesses the cache to return a cached result (to intercept subsequent accesses to the cache).

    Comment

    Working...
    X