使用Spring Cache

来源:互联网 发布:金相分析软件报价 编辑:程序博客网 时间:2024/06/05 04:44

本人用的是Spring3.2,所以如果你用的是其他版本,则本文不能保证完全正确。

Spring的cache是在Spring-context中实现的,你额外的引入的spring-context包即可。

<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.framework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.framework.version}</version></dependency>

我们采用的是注解式的,所以spring的bean配置添加了如下两个组件:
<cache:annotation-driven cache-manager="cacheManager" proxy-target-class="false" /><bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"><property name="caches"><set><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="realEstateCache" /><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="schoolCache" /><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="infoCache" /><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="chartDataCache" /></set></property></bean>
第一个很好理解,但是有个地方要注意:proxy-target-class="false"      这个地方,如果你程序中已经采用了一层代理,比如事务,则此处只能是false;否则会出现(我用了hibernate)“HibernateException: no session in current thread”的问题。 原因以及含义不解释, 程序员能谈到代理/缓存的,相信能理解这个话题。
说第二个,它里面需要配置一些caches,概念类似于ehcache的cache配置,这里我按照业务需要,为四种业务对象配置了4个cache池。大家知道凡是cache都要考虑缓存失效问题,按业务需要配置缓存,可以在必要的时候分别失效各个缓存池。缓存的实现类我就简单的使用了ConcurrentMapCacheFactoryBean。或许有别的实现。

在service类的接口或实现类的方法上添加如下的注解:

@Cacheable(value="schoolCache", key="'findTop10Primary'")public List<School> findTop10Primary();@Cacheable(value="schoolCache", key="'findTop10Middle'")public List<School> findTop10Middle();
注意value就是你在spring配置文件里面配置的缓存名称,key是可区分相互的一个常量或者表达式,我这里就用了和方法名一样的一个常量。
为了实现缓存失效,我在每个需要缓存的service都添加了如下的方法:
@CacheEvict(value="schoolCache", allEntries=true)public void removeAllCache();
在我需要缓存失效的时候,我在web层调用一下这些方法即可。

0 0
原创粉丝点击