5

I have tried the following code , but it does not work:

@Component
@Aspect
@Order(Integer.MAX_VALUE)
public class CacheAspect {

    @Around("execution(public * org.springframework.cache.interceptor.CacheInterceptor.invoke(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //CLASS_CACHE.set(signature.getReturnType());
        return joinPoint.proceed();
    }
}

P.S. I am sure CacheInterceptor is a spring managed bean.

Xstian
  • 8,184
  • 10
  • 42
  • 72
尤慕李
  • 425
  • 6
  • 15

1 Answers1

7

After some experimenting, I find that replacing the spring built in CacheInterceptor with a user defined one can solve my problem. Here is the code in case someone has similar requirments.

  @Configuration
  @EnableCaching
  @Profile("test")
  public class CacheConfig {
    @Bean
    @Autowired
    public CacheManager cacheManager(RedisClientTemplate redisClientTemplate) {
      return new ConcurrentMapCacheManager(redisClientTemplate, "test");
    }

    @Bean
    public CacheOperationSource cacheOperationSource() {
      return new AnnotationCacheOperationSource();
    }

    @Bean
    public CacheInterceptor cacheInterceptor() {
      CacheInterceptor interceptor = new MyCacheInterceptor();
      interceptor.setCacheOperationSources(cacheOperationSource());
      return interceptor;
    }
  }

MyCacheInterceptor.java, which shares the same logic with CacheAspect:

  public class MyCacheInterceptor extends CacheInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
      Method method = invocation.getMethod();
      //CLASS_CACHE.set(signature.getReturnType());
      return super.invoke(invocation);
    }
  }

The spring built in CacheInterceptor bean can be found in ProxyCachingConfiguration class.

Hope it helps.

尤慕李
  • 425
  • 6
  • 15
  • 3
    this solution is help me to use a cache provider put method in my custom interceptor , which enable me to set eviction time on put , which is not part of spring cache abstraction - and I added @Primary on cacheInterceptor() bean method , thanks! – lukass77 Feb 01 '17 at 11:46