revise cache API

+ update failing test
This commit is contained in:
Costin Leau 2011-05-17 18:01:35 +00:00
parent dea1fc933f
commit 0eb40e1e5e
5 changed files with 20 additions and 18 deletions

View File

@ -21,7 +21,7 @@ import net.sf.ehcache.Element;
import net.sf.ehcache.Status; import net.sf.ehcache.Status;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.cache.interceptor.DefaultValue; import org.springframework.cache.interceptor.DefaultValueWrapper;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -60,7 +60,7 @@ public class EhCacheCache implements Cache<Object, Object> {
public ValueWrapper<Object> get(Object key) { public ValueWrapper<Object> get(Object key) {
Element element = cache.get(key); Element element = cache.get(key);
return (element != null ? new DefaultValue<Object>(element.getObjectValue()) : null); return (element != null ? new DefaultValueWrapper<Object>(element.getObjectValue()) : null);
} }
public void put(Object key, Object value) { public void put(Object key, Object value) {

View File

@ -185,11 +185,11 @@ public abstract class CacheAspectSupport implements InitializingBean {
for (Iterator<Cache<?, ?>> iterator = caches.iterator(); iterator.hasNext() && !cacheHit;) { for (Iterator<Cache<?, ?>> iterator = caches.iterator(); iterator.hasNext() && !cacheHit;) {
Cache cache = iterator.next(); Cache cache = iterator.next();
Cache.ValueWrapper<Object> value = cache.get(key); Cache.ValueWrapper<Object> wrapper = cache.get(key);
if (value != null) { if (wrapper != null) {
cacheHit = true; cacheHit = true;
retVal = value.get(); retVal = wrapper.get();
} }
} }
@ -199,16 +199,16 @@ public abstract class CacheAspectSupport implements InitializingBean {
+ method); + method);
} }
retVal = invocation.call(); retVal = invocation.call();
// update all caches
for (Cache cache : caches) {
cache.put(key, retVal);
}
} else { } else {
if (log) { if (log) {
logger.trace("Key " + key + " found in cache, returning value " + retVal); logger.trace("Key " + key + " found in cache, returning value " + retVal);
} }
} }
// update all caches
for (Cache cache : caches) {
cache.put(key, retVal);
}
} }
if (cacheOp instanceof CacheEvictOperation) { if (cacheOp instanceof CacheEvictOperation) {

View File

@ -23,11 +23,11 @@ import org.springframework.cache.Cache.ValueWrapper;
* *
* @author Costin Leau * @author Costin Leau
*/ */
public class DefaultValue<V> implements ValueWrapper<V> { public class DefaultValueWrapper<V> implements ValueWrapper<V> {
private final V value; private final V value;
public DefaultValue(V value) { public DefaultValueWrapper(V value) {
this.value = value; this.value = value;
} }

View File

@ -20,7 +20,7 @@ import java.io.Serializable;
import java.util.Map; import java.util.Map;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.cache.interceptor.DefaultValue; import org.springframework.cache.interceptor.DefaultValueWrapper;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -76,7 +76,8 @@ public abstract class AbstractDelegatingCache<K, V> implements Cache<K, V> {
} }
public ValueWrapper<V> get(Object key) { public ValueWrapper<V> get(Object key) {
return new DefaultValue<V>(filterNull(delegate.get(key))); V v = delegate.get(key);
return (v != null ? new DefaultValueWrapper<V>(filterNull(v)) : null);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -85,8 +86,9 @@ public abstract class AbstractDelegatingCache<K, V> implements Cache<K, V> {
Map map = delegate; Map map = delegate;
map.put(key, NULL_HOLDER); map.put(key, NULL_HOLDER);
} }
else {
delegate.put(key, value); delegate.put(key, value);
}
} }
public void evict(Object key) { public void evict(Object key) {

View File

@ -9,7 +9,7 @@
<bean id="apc" class="org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator"/> <bean id="apc" class="org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator"/>
<bean id="annotationSource" class="org.springframework.cache.annotation.AnnotationCacheDefinitionSource"/> <bean id="annotationSource" class="org.springframework.cache.annotation.AnnotationCacheOperationSource"/>
<aop:config> <aop:config>
<aop:advisor advice-ref="debugInterceptor" pointcut="execution(* *..CacheableService.*(..))" order="1"/> <aop:advisor advice-ref="debugInterceptor" pointcut="execution(* *..CacheableService.*(..))" order="1"/>
@ -20,7 +20,7 @@
<property name="cacheDefinitionSources" ref="annotationSource"/> <property name="cacheDefinitionSources" ref="annotationSource"/>
</bean> </bean>
<bean id="advisor" class="org.springframework.cache.interceptor.BeanFactoryCacheDefinitionSourceAdvisor"> <bean id="advisor" class="org.springframework.cache.interceptor.BeanFactoryCacheOperationSourceAdvisor">
<property name="cacheDefinitionSource" ref="annotationSource"/> <property name="cacheDefinitionSource" ref="annotationSource"/>
<property name="adviceBeanName" value="cacheInterceptor"/> <property name="adviceBeanName" value="cacheInterceptor"/>
</bean> </bean>