public CacheOperationContext(CacheOperationMetadata metadata, Object[] args, Object target) { this.metadata = metadata; this.args = extractArgs(metadata.method, args); this.target = target; this.caches = CacheAspectSupport.this.getCaches(this, metadata.cacheResolver); this.cacheNames = createCacheNames(this.caches); }
protected CacheOperationContext getOperationContext( CacheOperation operation, Method method, Object[] args, Object target, Class<?> targetClass) { CacheOperationMetadata metadata = getCacheOperationMetadata(operation, method, targetClass); return new CacheOperationContext(metadata, args, target); }
@Override protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) { return context.getOperation().getCacheNames(); }
/** * Compute the key for the given caching operation. */ @Nullable protected Object generateKey(@Nullable Object result) { if (StringUtils.hasText(this.metadata.operation.getKey())) { EvaluationContext evaluationContext = createEvaluationContext(result); return evaluator.key(this.metadata.operation.getKey(), this.metadata.methodKey, evaluationContext); } return this.metadata.keyGenerator.generate(this.target, this.metadata.method, this.args); }
CacheableOperation newCacheOperation(String cacheName) { CacheableOperation.Builder builder = new CacheableOperation.Builder(); builder.setCacheManager("cacheManager"); builder.setCacheName(cacheName); return builder.build(); } }
@Override public Object generate(Object target, Method method, Object... params) { return generateKey(params); }
private void processCacheEvicts( Collection<CacheOperationContext> contexts, boolean beforeInvocation, @Nullable Object result) { for (CacheOperationContext context : contexts) { CacheEvictOperation operation = (CacheEvictOperation) context.metadata.operation; if (beforeInvocation == operation.isBeforeInvocation() && isConditionPassing(context, result)) { performCacheEvict(context, operation, result); } } }
public void apply(@Nullable Object result) { if (this.context.canPutToCache(result)) { for (Cache cache : this.context.getCaches()) { doPut(cache, this.key, result); } } } }
/** * Execute {@link Cache#clear()} on the specified {@link Cache} and * invoke the error handler if an exception occurs. */ protected void doClear(Cache cache) { try { cache.clear(); } catch (RuntimeException ex) { getErrorHandler().handleCacheClearError(ex, cache); } }
/** * Execute {@link Cache#evict(Object)} on the specified {@link Cache} and * invoke the error handler if an exception occurs. */ protected void doEvict(Cache cache, Object key) { try { cache.evict(key); } catch (RuntimeException ex) { getErrorHandler().handleCacheEvictError(ex, cache, key); } }
@Bean CacheProxyFactoryBean greeter() { CacheProxyFactoryBean factoryBean = new CacheProxyFactoryBean(); factoryBean.setCacheOperationSources(newCacheOperationSource("greet", newCacheOperation("Greetings"))); factoryBean.setTarget(new SimpleGreeter()); return factoryBean; }
/** * Execute the underlying operation (typically in case of cache miss) and return * the result of the invocation. If an exception occurs it will be wrapped in * a {@code ThrowableWrapper}: the exception can be handled or modified but it * <em>must</em> be wrapped in a {@code ThrowableWrapper} as well. * @param invoker the invoker handling the operation being cached * @return the result of the invocation * @see CacheOperationInvoker#invoke() */ protected Object invokeOperation(CacheOperationInvoker invoker) { return invoker.invoke(); }
/** * Set one or more sources to find cache operations. * @see CacheInterceptor#setCacheOperationSources */ public void setCacheOperationSources(CacheOperationSource... cacheOperationSources) { this.cacheInterceptor.setCacheOperationSources(cacheOperationSources); }
/** * Set the {@link CacheManager} to use to create a default {@link CacheResolver}. * Replace the current {@link CacheResolver}, if any. * @since 5.0.3 * @see CacheInterceptor#setCacheManager */ public void setCacheManager(CacheManager cacheManager) { this.cacheInterceptor.setCacheManager(cacheManager); }
/** * Set the default {@link CacheResolver} that this cache aspect should delegate * to if no specific cache resolver has been set for the operation. * <p>The default resolver resolves the caches against their names and the * default cache manager. * @since 5.0.3 * @see CacheInterceptor#setCacheResolver */ public void setCacheResolver(CacheResolver cacheResolver) { this.cacheInterceptor.setCacheResolver(cacheResolver); }
/** * Set the default {@link KeyGenerator} that this cache aspect should delegate to * if no specific key generator has been set for the operation. * <p>The default is a {@link SimpleKeyGenerator}. * @since 5.0.3 * @see CacheInterceptor#setKeyGenerator */ public void setKeyGenerator(KeyGenerator keyGenerator) { this.cacheInterceptor.setKeyGenerator(keyGenerator); }
@Override public CacheableOperation build() { return new CacheableOperation(this); } }
/** * Set the {@link ClassFilter} to use for this pointcut. * Default is {@link ClassFilter#TRUE}. */ public void setClassFilter(ClassFilter classFilter) { this.pointcut.setClassFilter(classFilter); }
public CacheEvictOperation build() { return new CacheEvictOperation(this); } }
/** * Execute the underlying operation (typically in case of cache miss) and return * the result of the invocation. If an exception occurs it will be wrapped in * a {@link CacheOperationInvoker.ThrowableWrapper}: the exception can be handled * or modified but it <em>must</em> be wrapped in a * {@link CacheOperationInvoker.ThrowableWrapper} as well. * @param invoker the invoker handling the operation being cached * @return the result of the invocation * @see CacheOperationInvoker#invoke() */ protected Object invokeOperation(CacheOperationInvoker invoker) { return invoker.invoke(); }