使用Spring Cache

本人用的是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层调用一下这些方法即可。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值