Cache provider related exceptions handling
This commit adds the necessary infrastructure to handle exceptions thrown by a cache provider in both Spring's and JCache's caching abstractions. Both interceptors can be configured with a CacheErrorHandler that defines several callbacks on typical cache operations. In particular, handleCacheGetError can be implemented in such a way that an exception thrown by the provider is handled as a cache miss by the caching abstraction. The handler can be configured with both CachingConfigurer and the XML namespace (error-handler property) Issue: SPR-9275
This commit is contained in:
@@ -55,6 +55,9 @@ public class ProxyJCacheConfiguration extends AbstractJCacheConfiguration {
|
||||
public JCacheInterceptor cacheInterceptor() {
|
||||
JCacheInterceptor interceptor = new JCacheInterceptor();
|
||||
interceptor.setCacheOperationSource(cacheOperationSource());
|
||||
if (this.errorHandler != null) {
|
||||
interceptor.setErrorHandler(this.errorHandler);
|
||||
}
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,8 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.interceptor.AbstractCacheInvoker;
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvoker;
|
||||
import org.springframework.cache.jcache.model.BaseCacheOperation;
|
||||
@@ -36,10 +38,14 @@ import org.springframework.cache.jcache.model.BaseCacheOperation;
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public abstract class AbstractCacheInterceptor<O extends BaseCacheOperation<A>, A extends Annotation>
|
||||
implements Serializable {
|
||||
extends AbstractCacheInvoker implements Serializable {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
protected AbstractCacheInterceptor(CacheErrorHandler errorHandler) {
|
||||
super(errorHandler);
|
||||
}
|
||||
|
||||
protected abstract Object invoke(CacheOperationInvocationContext<O> context,
|
||||
CacheOperationInvoker invoker) throws Throwable;
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.lang.annotation.Annotation;
|
||||
|
||||
import javax.cache.annotation.CacheKeyInvocationContext;
|
||||
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.cache.jcache.model.BaseKeyCacheOperation;
|
||||
@@ -34,6 +35,10 @@ import org.springframework.cache.jcache.model.BaseKeyCacheOperation;
|
||||
public abstract class AbstractKeyCacheInterceptor<O extends BaseKeyCacheOperation<A>, A extends Annotation>
|
||||
extends AbstractCacheInterceptor<O, A> {
|
||||
|
||||
protected AbstractKeyCacheInterceptor(CacheErrorHandler errorHandler) {
|
||||
super(errorHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a key for the specified invocation.
|
||||
* @param context the context of the invocation
|
||||
|
||||
@@ -20,6 +20,7 @@ import javax.cache.annotation.CacheKeyInvocationContext;
|
||||
import javax.cache.annotation.CachePut;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvoker;
|
||||
import org.springframework.cache.jcache.model.CachePutOperation;
|
||||
@@ -33,6 +34,10 @@ import org.springframework.cache.jcache.model.CachePutOperation;
|
||||
@SuppressWarnings("serial")
|
||||
public class CachePutInterceptor extends AbstractKeyCacheInterceptor<CachePutOperation, CachePut> {
|
||||
|
||||
public CachePutInterceptor(CacheErrorHandler errorHandler) {
|
||||
super(errorHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object invoke(CacheOperationInvocationContext<CachePutOperation> context,
|
||||
CacheOperationInvoker invoker) {
|
||||
@@ -65,7 +70,7 @@ public class CachePutInterceptor extends AbstractKeyCacheInterceptor<CachePutOpe
|
||||
protected void cacheValue(CacheOperationInvocationContext<CachePutOperation> context, Object value) {
|
||||
Object key = generateKey(context);
|
||||
Cache cache = resolveCache(context);
|
||||
cache.put(key, value);
|
||||
doPut(cache, key, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.cache.jcache.interceptor;
|
||||
import javax.cache.annotation.CacheRemoveAll;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvoker;
|
||||
import org.springframework.cache.jcache.model.CacheRemoveAllOperation;
|
||||
@@ -33,6 +34,10 @@ import org.springframework.cache.jcache.model.CacheRemoveAllOperation;
|
||||
public class CacheRemoveAllInterceptor
|
||||
extends AbstractCacheInterceptor<CacheRemoveAllOperation, CacheRemoveAll> {
|
||||
|
||||
protected CacheRemoveAllInterceptor(CacheErrorHandler errorHandler) {
|
||||
super(errorHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object invoke(CacheOperationInvocationContext<CacheRemoveAllOperation> context,
|
||||
CacheOperationInvoker invoker) {
|
||||
@@ -66,7 +71,7 @@ public class CacheRemoveAllInterceptor
|
||||
logger.trace("Invalidating entire cache '" + cache.getName() + "' for operation "
|
||||
+ context.getOperation());
|
||||
}
|
||||
cache.clear();
|
||||
doClear(cache);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.cache.jcache.interceptor;
|
||||
import javax.cache.annotation.CacheRemove;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvoker;
|
||||
import org.springframework.cache.jcache.model.CacheRemoveOperation;
|
||||
@@ -32,6 +33,10 @@ import org.springframework.cache.jcache.model.CacheRemoveOperation;
|
||||
@SuppressWarnings("serial")
|
||||
public class CacheRemoveEntryInterceptor extends AbstractKeyCacheInterceptor<CacheRemoveOperation, CacheRemove> {
|
||||
|
||||
protected CacheRemoveEntryInterceptor(CacheErrorHandler errorHandler) {
|
||||
super(errorHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object invoke(CacheOperationInvocationContext<CacheRemoveOperation> context,
|
||||
CacheOperationInvoker invoker) {
|
||||
@@ -66,7 +71,7 @@ public class CacheRemoveEntryInterceptor extends AbstractKeyCacheInterceptor<Cac
|
||||
logger.trace("Invalidating key [" + key + "] on cache '" + cache.getName()
|
||||
+ "' for operation " + context.getOperation());
|
||||
}
|
||||
cache.evict(key);
|
||||
doEvict(cache, key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.cache.jcache.interceptor;
|
||||
import javax.cache.annotation.CacheResult;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvoker;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
@@ -35,6 +36,10 @@ import org.springframework.util.filter.ExceptionTypeFilter;
|
||||
@SuppressWarnings("serial")
|
||||
public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheResultOperation, CacheResult> {
|
||||
|
||||
public CacheResultInterceptor(CacheErrorHandler errorHandler) {
|
||||
super(errorHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object invoke(CacheOperationInvocationContext<CacheResultOperation> context,
|
||||
CacheOperationInvoker invoker) {
|
||||
@@ -46,7 +51,7 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
|
||||
Cache exceptionCache = resolveExceptionCache(context);
|
||||
|
||||
if (!operation.isAlwaysInvoked()) {
|
||||
Cache.ValueWrapper cachedValue = cache.get(cacheKey);
|
||||
Cache.ValueWrapper cachedValue = doGet(cache, cacheKey);
|
||||
if (cachedValue != null) {
|
||||
return cachedValue.get();
|
||||
}
|
||||
@@ -74,7 +79,7 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
|
||||
if (exceptionCache == null) {
|
||||
return;
|
||||
}
|
||||
Cache.ValueWrapper result = exceptionCache.get(cacheKey);
|
||||
Cache.ValueWrapper result = doGet(exceptionCache, cacheKey);
|
||||
if (result != null) {
|
||||
throw rewriteCallStack((Throwable) result.get(), getClass().getName(), "invoke");
|
||||
}
|
||||
@@ -111,7 +116,6 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
|
||||
* @param exception the exception to merge with the current call stack
|
||||
* @param className the class name of the common ancestor
|
||||
* @param methodName the method name of the common ancestor
|
||||
* @param <T> the type of the exception
|
||||
* @return a clone exception with a rewritten call stack composed of the current
|
||||
* call stack up to (included) the common ancestor specified by the {@code className} and
|
||||
* {@code methodName} arguments, followed by stack trace elements of the specified
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
@@ -8,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.aop.framework.AopProxyUtils;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.cache.interceptor.AbstractCacheInvoker;
|
||||
import org.springframework.cache.interceptor.BasicCacheOperation;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvoker;
|
||||
@@ -36,7 +53,7 @@ import org.springframework.util.Assert;
|
||||
* @see KeyGeneratorAdapter
|
||||
* @see CacheResolverAdapter
|
||||
*/
|
||||
public class JCacheAspectSupport implements InitializingBean {
|
||||
public class JCacheAspectSupport extends AbstractCacheInvoker implements InitializingBean {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@@ -44,13 +61,13 @@ public class JCacheAspectSupport implements InitializingBean {
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
private final CacheResultInterceptor cacheResultInterceptor = new CacheResultInterceptor();
|
||||
private CacheResultInterceptor cacheResultInterceptor;
|
||||
|
||||
private final CachePutInterceptor cachePutInterceptor = new CachePutInterceptor();
|
||||
private CachePutInterceptor cachePutInterceptor;
|
||||
|
||||
private final CacheRemoveEntryInterceptor cacheRemoveEntryInterceptor = new CacheRemoveEntryInterceptor();
|
||||
private CacheRemoveEntryInterceptor cacheRemoveEntryInterceptor;
|
||||
|
||||
private final CacheRemoveAllInterceptor cacheRemoveAllInterceptor = new CacheRemoveAllInterceptor();
|
||||
private CacheRemoveAllInterceptor cacheRemoveAllInterceptor;
|
||||
|
||||
public void setCacheOperationSource(JCacheOperationSource cacheOperationSource) {
|
||||
Assert.notNull(cacheOperationSource);
|
||||
@@ -67,6 +84,13 @@ public class JCacheAspectSupport implements InitializingBean {
|
||||
public void afterPropertiesSet() {
|
||||
Assert.state(this.cacheOperationSource != null, "The 'cacheOperationSource' property is required: " +
|
||||
"If there are no cacheable methods, then don't use a cache aspect.");
|
||||
Assert.state(this.getErrorHandler() != null, "The 'errorHandler' is required.");
|
||||
|
||||
this.cacheResultInterceptor = new CacheResultInterceptor(getErrorHandler());
|
||||
this.cachePutInterceptor = new CachePutInterceptor(getErrorHandler());
|
||||
this.cacheRemoveEntryInterceptor = new CacheRemoveEntryInterceptor(getErrorHandler());
|
||||
this.cacheRemoveAllInterceptor = new CacheRemoveAllInterceptor(getErrorHandler());
|
||||
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,8 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
@Rule
|
||||
public final TestName name = new TestName();
|
||||
|
||||
protected ApplicationContext ctx;
|
||||
|
||||
private JCacheableService<?> service;
|
||||
|
||||
private CacheManager cacheManager;
|
||||
@@ -51,9 +53,9 @@ public abstract class AbstractJCacheAnnotationTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
ApplicationContext context = getApplicationContext();
|
||||
service = context.getBean(JCacheableService.class);
|
||||
cacheManager = context.getBean("cacheManager", CacheManager.class);
|
||||
ctx = getApplicationContext();
|
||||
service = ctx.getBean(JCacheableService.class);
|
||||
cacheManager = ctx.getBean("cacheManager", CacheManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -27,13 +27,16 @@ import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
import org.springframework.cache.config.SomeKeyGenerator;
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.cache.interceptor.NamedCacheResolver;
|
||||
import org.springframework.cache.interceptor.SimpleCacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.SimpleCacheResolver;
|
||||
import org.springframework.cache.interceptor.SimpleKeyGenerator;
|
||||
import org.springframework.cache.jcache.interceptor.AnnotatedJCacheableService;
|
||||
import org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource;
|
||||
import org.springframework.cache.jcache.interceptor.JCacheInterceptor;
|
||||
import org.springframework.cache.jcache.interceptor.SimpleExceptionCacheResolver;
|
||||
import org.springframework.cache.support.NoOpCacheManager;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
@@ -63,6 +66,8 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
|
||||
cos.getDefaultCacheResolver());
|
||||
assertSame(context.getBean("exceptionCacheResolver", CacheResolver.class),
|
||||
cos.getDefaultExceptionCacheResolver());
|
||||
JCacheInterceptor interceptor = context.getBean(JCacheInterceptor.class);
|
||||
assertSame(context.getBean("errorHandler", CacheErrorHandler.class), interceptor.getErrorHandler());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -138,6 +143,12 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
|
||||
return new SimpleKeyGenerator();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
public CacheErrorHandler errorHandler() {
|
||||
return new SimpleCacheErrorHandler();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
public CacheResolver cacheResolver() {
|
||||
|
||||
@@ -16,6 +16,12 @@
|
||||
|
||||
package org.springframework.cache.jcache.config;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.jcache.interceptor.JCacheInterceptor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
|
||||
@@ -30,4 +36,10 @@ public class JCacheNamespaceDrivenTests extends AbstractJCacheAnnotationTests {
|
||||
"/org/springframework/cache/jcache/config/jCacheNamespaceDriven.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheErrorHandler() {
|
||||
JCacheInterceptor ci = ctx.getBean(JCacheInterceptor.class);
|
||||
assertSame(ctx.getBean("errorHandler", CacheErrorHandler.class), ci.getErrorHandler());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import javax.cache.annotation.CacheDefaults;
|
||||
import javax.cache.annotation.CachePut;
|
||||
import javax.cache.annotation.CacheRemove;
|
||||
import javax.cache.annotation.CacheRemoveAll;
|
||||
import javax.cache.annotation.CacheResult;
|
||||
import javax.cache.annotation.CacheValue;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Role;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class JCacheErrorHandlerTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private Cache cache;
|
||||
|
||||
private CacheErrorHandler errorHandler;
|
||||
|
||||
private SimpleService simpleService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
||||
this.cache = context.getBean("mockCache", Cache.class);
|
||||
this.errorHandler = context.getBean(CacheErrorHandler.class);
|
||||
this.simpleService = context.getBean(SimpleService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFail() {
|
||||
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
|
||||
SimpleGeneratedCacheKey key = new SimpleGeneratedCacheKey(0L);
|
||||
doThrow(exception).when(cache).get(key);
|
||||
|
||||
this.simpleService.get(0L);
|
||||
verify(errorHandler).handleCacheGetError(exception, cache, key);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putFail() {
|
||||
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put");
|
||||
SimpleGeneratedCacheKey key = new SimpleGeneratedCacheKey(0L);
|
||||
doThrow(exception).when(cache).put(key, 234L);
|
||||
|
||||
this.simpleService.put(0L, 234L);
|
||||
verify(errorHandler).handleCachePutError(exception, cache, key, 234L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void evictFail() {
|
||||
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
|
||||
SimpleGeneratedCacheKey key = new SimpleGeneratedCacheKey(0L);
|
||||
doThrow(exception).when(cache).evict(key);
|
||||
|
||||
this.simpleService.evict(0L);
|
||||
verify(errorHandler).handleCacheEvictError(exception, cache, key);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearFail() {
|
||||
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
|
||||
doThrow(exception).when(cache).clear();
|
||||
|
||||
this.simpleService.clear();
|
||||
verify(errorHandler).handleCacheClearError(exception, cache);
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
static class Config {
|
||||
|
||||
@Bean(name = "jCacheInterceptor")
|
||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
public JCacheInterceptor cacheInterceptor() {
|
||||
JCacheInterceptor interceptor = new JCacheInterceptor();
|
||||
interceptor.setCacheOperationSource(cacheOperationSource());
|
||||
interceptor.setErrorHandler(errorHandler());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheErrorHandler errorHandler() {
|
||||
return mock(CacheErrorHandler.class);
|
||||
}
|
||||
|
||||
@Bean(name = "jCacheOperationSource")
|
||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
public JCacheOperationSource cacheOperationSource() {
|
||||
DefaultJCacheOperationSource source = new DefaultJCacheOperationSource();
|
||||
source.setCacheManager(cacheManager());
|
||||
return source;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleService simpleService() {
|
||||
return new SimpleService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
SimpleCacheManager cacheManager = new SimpleCacheManager();
|
||||
cacheManager.setCaches(Arrays.asList(mockCache()));
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Cache mockCache() {
|
||||
Cache cache = mock(Cache.class);
|
||||
given(cache.getName()).willReturn("test");
|
||||
return cache;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@CacheDefaults(cacheName = "test")
|
||||
public static class SimpleService {
|
||||
private AtomicLong counter = new AtomicLong();
|
||||
|
||||
@CacheResult
|
||||
public Object get(long id) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@CachePut
|
||||
public void put(long id, @CacheValue Object object) {
|
||||
}
|
||||
|
||||
@CacheRemove
|
||||
public void evict(long id) {
|
||||
}
|
||||
|
||||
@CacheRemoveAll
|
||||
public void clear() {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
http://www.springframework.org/schema/cache
|
||||
http://www.springframework.org/schema/cache/spring-cache.xsd">
|
||||
|
||||
<cache:annotation-driven proxy-target-class="false" order="0"/>
|
||||
<cache:annotation-driven proxy-target-class="false" order="0" error-handler="errorHandler"/>
|
||||
|
||||
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
|
||||
<property name="caches">
|
||||
@@ -26,6 +26,8 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="errorHandler" class="org.springframework.cache.interceptor.SimpleCacheErrorHandler"/>
|
||||
|
||||
<bean id="defaultCache"
|
||||
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
|
||||
<property name="name" value="default"/>
|
||||
|
||||
Reference in New Issue
Block a user