InvalidateSingleCacheAdvice extends CacheAdvice { @Around("invalidateSingle()") public Object cacheInvalidateSingle(final ProceedingJoinPoint pjp) throws Throwable { if (isDisabled()) { getLogger().info("Cache disabled"); return pjp.proceed(); } String cacheKey = null; final AnnotationData data; final Method methodToCache; try { methodToCache = getCacheBase().getMethodToCache(pjp, InvalidateSingleCache.class); final InvalidateSingleCache annotation = methodToCache.getAnnotation(InvalidateSingleCache.class); data = AnnotationDataBuilder.buildAnnotationData(annotation, InvalidateSingleCache.class, methodToCache); if (!data.isReturnKeyIndex()) { cacheKey = getCacheBase().getCacheKeyBuilder().getCacheKey(data, pjp.getArgs(), methodToCache.toString()); } } catch (Exception ex) { warn(ex, "Caching on method %s and key [%s] aborted due to an error.", pjp.toShortString(), cacheKey); return pjp.proceed(); } final Object result = pjp.proceed(); try { if (data.isReturnKeyIndex()) { getCacheBase().verifyReturnTypeIsNoVoid(methodToCache, InvalidateSingleCache.class); cacheKey = getCacheBase().getCacheKeyBuilder().getCacheKey(result, data.getNamespace()); } getCacheBase().getCache(data).delete(cacheKey); } catch (Exception ex) { warn(ex, "Caching on method %s and key [%s] aborted due to an error.", pjp.toShortString(), cacheKey); } return result; }  @Pointcut("@annotation(com.google.code.ssm.api.InvalidateSingleCache)") void invalidateSingle(); @Around("invalidateSingle()") Object cacheInvalidateSingle(final ProceedingJoinPoint pjp);  }
@Test public void valid() throws Throwable { Assume.assumeTrue(isValid); when(pjp.proceed()).thenReturn(expectedValue); assertEquals(expectedValue, advice.cacheInvalidateSingle(pjp)); verify(cache).delete(cacheKey); verify(pjp).proceed(); }
@Test public void invalid() throws Throwable { Assume.assumeThat(isValid, CoreMatchers.is(false)); when(pjp.proceed()).thenReturn(expectedValue); assertEquals(expectedValue, advice.cacheInvalidateSingle(pjp)); verify(pjp).proceed(); verify(cache, never()).delete(anyString()); }