InvalidateMultiCacheAdvice extends CacheAdvice { @Around("invalidateMulti()") public Object cacheInvalidateMulti(final ProceedingJoinPoint pjp) throws Throwable { if (isDisabled()) { getLogger().info("Cache disabled"); return pjp.proceed(); } Collection<String> cacheKeys = null; final AnnotationData data; final Method methodToCache; try { methodToCache = getCacheBase().getMethodToCache(pjp, InvalidateMultiCache.class); final InvalidateMultiCache annotation = methodToCache.getAnnotation(InvalidateMultiCache.class); data = AnnotationDataBuilder.buildAnnotationData(annotation, InvalidateMultiCache.class, methodToCache); if (!data.isReturnKeyIndex()) { cacheKeys = getCacheBase().getCacheKeyBuilder().getCacheKeys(data, pjp.getArgs(), methodToCache.toString()); } } catch (Exception ex) { warn(ex, "Caching on method %s aborted due to an error.", pjp.toShortString()); return pjp.proceed(); } final Object result = pjp.proceed(); try { if (data.isReturnKeyIndex()) { if (!getCacheBase().verifyTypeIsList(result.getClass())) { throw new InvalidAnnotationException(String.format("The return type is not a [%s]. " + "The method [%s] does not fulfill the requirements.", List.class.getName(), methodToCache.toString())); } @SuppressWarnings("unchecked") final List<Object> keyObjects = (List<Object>) result; cacheKeys = getCacheBase().getCacheKeyBuilder().getCacheKeys(keyObjects, data.getNamespace()); } getCacheBase().getCache(data).delete(cacheKeys); } catch (Exception ex) { warn(ex, "Caching on method %s aborted due to an error.", pjp.toShortString()); } return result; }  @Pointcut("@annotation(com.google.code.ssm.api.InvalidateMultiCache)") void invalidateMulti(); @Around("invalidateMulti()") Object cacheInvalidateMulti(final ProceedingJoinPoint pjp);  }
@Test public void valid() throws Throwable { Assume.assumeTrue(isValid); when(pjp.proceed()).thenReturn(expectedValue); assertEquals(expectedValue, advice.cacheInvalidateMulti(pjp)); verify(pjp).proceed(); verify(cache).delete(argThat(new ArgumentMatcher<Collection<String>>() { @Override public boolean matches(Collection<String> set) { Set<String> target = new HashSet<String>(Arrays.asList(cacheKeys)); return set.containsAll(target) && target.containsAll(set); } })); }
@Test @SuppressWarnings("unchecked") public void invalid() throws Throwable { Assume.assumeThat(isValid, CoreMatchers.is(false)); when(pjp.proceed()).thenReturn(expectedValue); assertEquals(expectedValue, advice.cacheInvalidateMulti(pjp)); verify(pjp).proceed(); verify(cache, never()).delete(any(Collection.class)); verify(cache, never()).delete(anyString()); }