论坛首页 Java企业应用论坛

spring 2.5 annotations配置ehcache

浏览 2676 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-12-09  
配置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);
}
代码都贴出来了,ehcache的内部机制我不太了解。希望大家指点一下。
现在是这样的场景,先在action 中调用getAllUserInfoMap 方法,ehcache会把所有的数据都放到cache中。
然后在action中调用insertUser方法。如果在insert 方法中不使用@TriggersRemove(cacheName="userCache",removeAll=true)
ehcache不会把新增的数据同步到cache中。但是如果这里把cache remove了以后。下次调用getAllUserInfoMap 方法时候就会重新查询一下数据库。这样就跟我们前期想要的效果不符。实际缓存的意义就不大了。
或许我没把ehcache的原理吃透。所以请大家指点一下。因为项目没有集群所以不考虑用memcached。大家有好的cache方案可以尽情提出来。谢谢大家。

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics