protected Collection<? extends Cache> getCaches( CacheOperationInvocationContext<CacheOperation> context, CacheResolver cacheResolver) { Collection<? extends Cache> caches = cacheResolver.resolveCaches(context); if (caches.isEmpty()) { throw new IllegalStateException("No cache could be resolved for '" + context.getOperation() + "' using resolver '" + cacheResolver + "'. At least one cache should be provided per cache operation."); } return caches; }
/** * Generate a key for the specified invocation. * @param context the context of the invocation * @return the key to use */ protected Object generateKey(CacheOperationInvocationContext<O> context) { KeyGenerator keyGenerator = context.getOperation().getKeyGenerator(); Object key = keyGenerator.generate(context.getTarget(), context.getMethod(), context.getArgs()); if (logger.isTraceEnabled()) { logger.trace("Computed cache key " + key + " for operation " + context.getOperation()); } return key; }
/** * Create a {@link CacheKeyInvocationContext} based on the specified invocation. * @param context the context of the invocation. * @return the related {@code CacheKeyInvocationContext} */ protected CacheKeyInvocationContext<A> createCacheKeyInvocationContext(CacheOperationInvocationContext<O> context) { return new DefaultCacheKeyInvocationContext<>(context.getOperation(), context.getTarget(), context.getArgs()); }
@Override public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) { List<Cache> caches = new ArrayList<Cache>(); for(String cacheName : context.getOperation().getCacheNames()) { caches.add(cacheManager.getCache(cacheName)); } if(context.getTarget() instanceof UserService2) { caches.add(cacheManager.getCache("user2")); caches.add(cacheManager.getCache("user3")); } return caches; } }
private <T extends Annotation> T getAnnotation(CacheOperationInvocationContext<?> context, Class<T> clazz) { try { // due to some cache proxy behaviour we can get method of superinterface instead of annotated method from target class // but sometime annotation has been appear on interface therefore we need check both cases Method proxiedMethod = context.getMethod(); Class<?> targetClazz = context.getTarget().getClass(); T annotation = null; if(!targetClazz.equals(proxiedMethod.getDeclaringClass())) { Method origMethod = targetClazz.getMethod(proxiedMethod.getName(), proxiedMethod.getParameterTypes()); annotation = origMethod.getAnnotation(clazz); } if(annotation == null) { annotation = proxiedMethod.getAnnotation(clazz); } return annotation; } catch (NoSuchMethodException e) { throw Throwables.asRuntime(e); } }
@Override protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) { String cls = context.getTarget().getClass().getSimpleName() + (suffix.equals("")?suffix:concat+suffix); return Collections.singletonList(cls); }
@Override @Nullable protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) { String cacheName = (String) context.getArgs()[1]; return Collections.singleton(cacheName); } }
/** * Create a {@link CacheKeyInvocationContext} based on the specified invocation. * @param context the context of the invocation. * @return the related {@code CacheKeyInvocationContext} */ protected CacheKeyInvocationContext<A> createCacheKeyInvocationContext(CacheOperationInvocationContext<O> context) { return new DefaultCacheKeyInvocationContext<>(context.getOperation(), context.getTarget(), context.getArgs()); }
@Override @Nullable protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) { String cacheName = (String) context.getArgs()[0]; if (cacheName != null) { return Collections.singleton(cacheName); } return null; }
@Override protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) { BasicOperation operation = context.getOperation(); if (!(operation instanceof CacheResultOperation)) { throw new IllegalStateException("Could not extract exception cache name from " + operation); } CacheResultOperation cacheResultOperation = (CacheResultOperation) operation; String exceptionCacheName = cacheResultOperation.getExceptionCacheName(); if (exceptionCacheName != null) { return Collections.singleton(exceptionCacheName); } return null; }
/** * Generate a key for the specified invocation. * @param context the context of the invocation * @return the key to use */ protected Object generateKey(CacheOperationInvocationContext<O> context) { KeyGenerator keyGenerator = context.getOperation().getKeyGenerator(); Object key = keyGenerator.generate(context.getTarget(), context.getMethod(), context.getArgs()); if (logger.isTraceEnabled()) { logger.trace("Computed cache key " + key + " for operation " + context.getOperation()); } return key; }
/** * Create a {@link CacheKeyInvocationContext} based on the specified invocation. * @param context the context of the invocation. * @return the related {@code CacheKeyInvocationContext} */ protected CacheKeyInvocationContext<A> createCacheKeyInvocationContext( CacheOperationInvocationContext<O> context) { return new DefaultCacheKeyInvocationContext<A>(context.getOperation(), context.getTarget(), context.getArgs()); }
@Override protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) { return context.getOperation().getCacheNames(); }
/** * Generate a key for the specified invocation. * @param context the context of the invocation * @return the key to use */ protected Object generateKey(CacheOperationInvocationContext<O> context) { KeyGenerator keyGenerator = context.getOperation().getKeyGenerator(); Object key = keyGenerator.generate(context.getTarget(), context.getMethod(), context.getArgs()); if (logger.isTraceEnabled()) { logger.trace("Computed cache key " + key + " for operation " + context.getOperation()); } return key; }
/** * Resolve the cache to use. * @param context the invocation context * @return the cache to use (never null) */ protected Cache resolveCache(CacheOperationInvocationContext<O> context) { Collection<? extends Cache> caches = context.getOperation().getCacheResolver().resolveCaches(context); Cache cache = extractFrom(caches); if (cache == null) { throw new IllegalStateException("Cache could not have been resolved for " + context.getOperation()); } return cache; }
protected Collection<? extends Cache> getCaches( CacheOperationInvocationContext<CacheOperation> context, CacheResolver cacheResolver) { Collection<? extends Cache> caches = cacheResolver.resolveCaches(context); if (caches.isEmpty()) { throw new IllegalStateException("No cache could be resolved for '" + context.getOperation() + "' using resolver '" + cacheResolver + "'. At least one cache should be provided per cache operation."); } return caches; }
@Nullable private Cache resolveExceptionCache(CacheOperationInvocationContext<CacheResultOperation> context) { CacheResolver exceptionCacheResolver = context.getOperation().getExceptionCacheResolver(); if (exceptionCacheResolver != null) { return extractFrom(context.getOperation().getExceptionCacheResolver().resolveCaches(context)); } return null; }
@Override protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) { return context.getOperation().getCacheNames(); }
protected void removeAll(CacheOperationInvocationContext<CacheRemoveAllOperation> context) { Cache cache = resolveCache(context); if (logger.isTraceEnabled()) { logger.trace("Invalidating entire cache '" + cache.getName() + "' for operation " + context.getOperation()); } doClear(cache); }
@Override public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) { Collection<String> cacheNames = getCacheNames(context); if (cacheNames == null) { return Collections.emptyList(); } Collection<Cache> result = new ArrayList<>(cacheNames.size()); for (String cacheName : cacheNames) { Cache cache = getCacheManager().getCache(cacheName); if (cache == null) { throw new IllegalArgumentException("Cannot find cache named '" + cacheName + "' for " + context.getOperation()); } result.add(cache); } return result; }