配置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方案可以尽情提出来。谢谢大家。 

解决方案 »

  1.   

    因为项目没有集群所以不考虑用memcached
    谁说了不集群就不考虑memcached呢?
    但是如果这里把cache remove了以后。下次调用getAllUserInfoMap 方法时候就会重新查询一下数据库
    你都清空了当然会重新查询呀?你想要什么预期效果,你清空了还去找缓存?
      

  2.   

    我想要的效果是,如果新增数据、删除数据、更新数据 直接更新到缓存中啊。 关键我不知道怎么配置。楼上有撒高见啊。
    因为我想要查询到及时的数据啊。不得不把cache remove.
    楼上的有什么好的办法吗
      

  3.   

    Memcached是“分布式”的内存对象缓存系统,那么就是说,那些不需要“分布”的,不需要共享的,或者干脆规模小到只有一台服务器的应用,memcached不会带来任何好处,相反还会拖慢系统效率,因为网络连接同样需要资源,即使是UNIX本地连接也一样。 在我之前的测试数据中显示,memcached本地读写速度要比直接PHP内存数组慢几十倍,而APC、共享内存方式都和直接数组差不多。可见,如果只是本地级缓存,使用memcached是非常不划算的。
      

  4.   

    今天突然想起结贴,最后把我的解决方案跟大家分享一下吧
    建议大家用EHCACHE用googlecode里面的jar包
    看下面的配置
    @Cacheable(cacheName="RoleCache",keyGenerator = @KeyGenerator (
                name = "HashCodeCacheKeyGenerator",
                properties = @Property( name="includeMethod", value="false" )
            ))
    )这里的cache是用method里面的参数作为hashcode去比较是不是同一个cache
    具体看AbstractCacheKeyGenerator.java
     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();
                }
            }
        }