本人用的是Spring3.2,所以如果你用的是其他版本,则本文不能保证完全正确。
Spring的cache是在Spring-context中实现的,你额外的引入的spring-context包即可。
我们采用的是注解式的,所以spring的bean配置添加了如下两个组件:
第一个很好理解,但是有个地方要注意:proxy-target-class="false" 这个地方,如果你程序中已经采用了一层代理,比如事务,则此处只能是false;否则会出现(我用了hibernate)“HibernateException: no session in current thread”的问题。 原因以及含义不解释, 程序员能谈到代理/缓存的,相信能理解这个话题。
说第二个,它里面需要配置一些caches,概念类似于ehcache的cache配置,这里我按照业务需要,为四种业务对象配置了4个cache池。大家知道凡是cache都要考虑缓存失效问题,按业务需要配置缓存,可以在必要的时候分别失效各个缓存池。缓存的实现类我就简单的使用了ConcurrentMapCacheFactoryBean。或许有别的实现。
在service类的接口或实现类的方法上添加如下的注解:
schoolCache就是上面配置文件xml里面的一个p:name对应的值
为了实现缓存失效,我在每个需要缓存的service都添加了如下的方法:
在我需要缓存失效的时候,我在web层调用一下这些方法即可。
关于失效时间
Spring Cache是要看你使用的实现,具体在SpringContext中是cacheManager Bean。默认内置3种实现,EHCache,Redis,ConcurrentHashMap。使用Spring对于EHCache可以使用如下的配置,可以有实效的。
( <defaultCachemaxElementsInMemory="10000"eternal="false"
timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="false"/>
timeToLiveSeconds====缓存数据在失效前的允许存活时间(单位:秒)
timeToIdleSeconds====缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用)
使用Redis在缓存的时候也可以使用实效的,但是Spring Cache没有提供便利的API,因此还无法利用Redis的默认缓存时限,需要自己更改Sprng context源码
ConcurrentHashMap实现的更无法使用时效限制。
三种配置如下:
EHCache:
<cache:annotation-driven/>
<!-- EhCache library setup -->
<beanid="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml"
p:shared="true"/>
<beanid="cacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache"/>
Redis:
<beanid="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-argindex="0"ref="redisTemplate"/>
</bean>
Java ConcurrentHashMap:
<beanid="cacheManager"
class="org.springframework.cache.concurrent.ConcurrentMapCacheManager">
</bean>
Spring Cache默认自带的基于ConcurrentHashMap的CacheManager实现是没有自动过期这一功能的,需要手工通过@CacheEvict 来剔除缓存数据。TTL、TTI等高级特性等高级特性需要使用EhCache来实现