Ehcache 使用

Cache的配置很灵活,官方提供的Cache配置方式有好几种。你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。

你可以将Cache的配置从代码中剥离出来,也可以在使用运行时配置,所谓的运行时配置无非也就是在代码中配置。以下是运行时配置的好处:

· 在同一个地方配置所有的Cache,这样很容易管理Cache的内存和磁盘消耗。
· 发布时可更改Cache配置。
· 可再安装阶段就检查出配置错误信息,而避免了运行时错误。


在我的项目中:
spring中定义ehcache支持:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"

xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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 />

<ehcache:config cache-manager="cacheManager">
<ehcache:evict-expired-elements interval="60" />
</ehcache:config>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:cache/ehcache.xml"/>
</bean>

<bean id="productKeyGenerator" class="**.**.cache.ProductCacheKeyGenerator"/>

</beans>


可以看到这里定义的cache使用的主配置文件是classpath下的cache文件夹下的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">

<diskStore path="user.home/web_ehcache/" />

<defaultCache
maxElementsInMemory="3000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
maxElementsOnDisk="100"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="100"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="AAACache"
maxElementsInMemory="3000"
maxElementsOnDisk="7000"
eternal="false"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LFU"
/>
</ehcache>


defaultCache是默认的缓存配置,AAACache是我自己定义的缓存策略,里面各个properties的含义可以自己去查资料很清楚,上面的diskStore path是缓存文件简历的目录,user.home是使用的系统的变量,就是你这个用户的文件夹,such as : C:\Documents and Settings\user\My Documents\...

缓存实际对反复查询的条件返回的一个结果进行缓存,而缓存的KEY,也是缓存的凭据一般就是使用这些查询的条件,这里我们可以定制我们的缓存KEY,可以从我的spring的配置文件中查询得到我定义了一个productKeyGenerator。 这个东西就是用来针对product查询生成缓存key的一个key生成器。


public class ProductCacheKeyGenerator implements CacheKeyGenerator<Serializable> {

@Override
public Serializable generateKey(MethodInvocation methodInvocation) {
String product = String.format("product_of_%s", methodInvocation.getArguments()[0]);
return product;
}

@Override
public Serializable generateKey(Object... data) {
// TODO Auto-generated method stub
return null;
}

}


这样所有生成的缓存关于查询product的进这个缓存key生成器的都会给它的缓存取一个名字叫product_of_%s %s这里就是传过来的参数的第一个值。

最后看看怎么在后台的数据访问层使用这个缓存和缓存KEY生成器:

@Cacheable(cacheName="AAACache",keyGeneratorName="productKeyGenerator" )
public List<Product> listProductsByRegion(String regionId);


这里的keyGeneratorName就是我spring里面配置的这个KEY生成器的BEAN ID。CACHENAME就是我要应用的缓存策略,你可以理解为就是个性缓存。

这样缓存配置就完成了,在应用这个操作的时候,如果你给listProductsByRegion方法传递的参数是'ABC',你可以发现后台日志会记录生成一个product_of_ABC的缓存数据。以后在策略配置的范围内每调用这个方法都会应用到生成的这个缓存。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值