配置ehcache地址:
http://code.google.com/p/ehcache-spring-annotations/wiki/UsingTriggersRemove
hi :
大家好,我目前使用的是spring ehcache作為項目的緩衝.詳細配置如下:
ehcaceh_config.xml
<?xml version="1.0" encoding="utf-8"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="/WEB-INF/classes/ehcache.xml"></property>
</bean>
</beans>
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir/EhCacheSpringAnnotationsExampleApp"/>
<defaultCache
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="3000"
timeToLiveSeconds="3000"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="100"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="100"
memoryStoreEvictionPolicy="LRU"
statistics="false"
/>
<cache name="UserCache"
maxElementsInMemory="100"
maxElementsOnDisk="100"
eternal="false"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="100"
timeToLiveSeconds="100"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off"
/>
</ehcache>
@Cacheable(cacheName="userCache")
public ConcurrentMap<Integer, UserModel> getAllUserInfoMap() {
List<UserModel> list = (List<UserModel>) getSqlMapClientTemplate()
.queryForList("user.SELECT_USER_ALL_INFO");
ConcurrentMap<String, UserModel> userMap = null;
if (CollectionUtils.isNotEmpty(list)) {
userMap = new ConcurrentHashMap<String, UserModel>();
for (UserModel userModel : list) {
userMap.put(userModel.getId(), userModel);
}
}
return userMap;
}
@TriggersRemove(cacheName="userCache",removeAll=true)
public void insertUser(UserModel userModel){
getSqlMapClientTemplate().insert("user.INSERT_USER_POJO", userModel);
}
在這裡用的同學要注意你緩衝的密度
我建議大家用下面的放方式
@Cacheable(cacheName="cacheName",keyGenerator = @KeyGenerator (
name = "HashCodeCacheKeyGenerator",
properties = @Property( name="includeMethod", value="false" )
)
)
有興趣的同學可以看一下ehcache source 它的緩衝是通過hashcode 來控制
具體可以看AbstractCacheKeyGenerator、、
{code}
public final T generateKey(MethodInvocation methodInvocation) {
final Object[] arguments = methodInvocation.getArguments();
if (this.includeMethod) {
final Method method = methodInvocation.getMethod();
final Class<?> declaringClass = method.getDeclaringClass();
final String name = method.getName();
final Class<?> returnType = method.getReturnType();
if (this.includeParameterTypes) {
final Class<?>[] parameterTypes = method.getParameterTypes();
return this.generateKey(declaringClass, name, returnType, parameterTypes, arguments);
}
return this.generateKey(declaringClass, name, returnType, arguments);
}
try {
return this.generateKey(arguments);
}
finally {
if (this.checkforCycles) {
//Cleanup our thread local data
REGISTRY.remove();
}
}
}
{code}