@AroundInvoke public Object cacheRemoveAll(InvocationContext invocationContext) throws Exception { final CacheKeyInvocationContext<CacheRemoveAll> cacheKeyInvocationContext = contextFactory.getCacheKeyInvocationContext(invocationContext); final CacheRemoveAll cacheRemoveAll = cacheKeyInvocationContext.getCacheAnnotation(); final Cache<CacheKey, Object> cache = cacheResolver.resolveCache(cacheKeyInvocationContext); if (!cacheRemoveAll.afterInvocation()) { cache.clear(); } final Object result = invocationContext.proceed(); if (cacheRemoveAll.afterInvocation()) { cache.clear(); } return result; }
@AroundInvoke public Object cacheResult(InvocationContext invocationContext) throws Exception { if (log.isTraceEnabled()) { log.tracef("Interception of method named '%s'", invocationContext.getMethod().getName()); } final CacheKeyInvocationContext<CacheResult> cacheKeyInvocationContext = contextFactory.getCacheKeyInvocationContext(invocationContext); final CacheKeyGenerator cacheKeyGenerator = cacheKeyInvocationContext .unwrap(CacheKeyInvocationContextImpl.class) .getCacheKeyGenerator(); final CacheResult cacheResult = cacheKeyInvocationContext.getCacheAnnotation(); final CacheKey cacheKey = cacheKeyGenerator.generateCacheKey(cacheKeyInvocationContext); final Cache<CacheKey, Object> cache = cacheResolver.resolveCache(cacheKeyInvocationContext); Object result = null; if (!cacheResult.skipGet()) { result = cache.get(cacheKey); if (log.isTraceEnabled()) { log.tracef( "Entry with value '%s' has been found in cache '%s' with key '%s'", result, cache.getName(), cacheKey); } } if (result == null) { result = invocationContext.proceed(); if (result != null) { cache.put(cacheKey, result); if (log.isTraceEnabled()) { log.tracef( "Value '%s' cached in cache '%s' with key '%s'", result, cache.getName(), cacheKey); } } } return result; }