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的缓存数据。以后在策略配置的范围内每调用这个方法都会应用到生成的这个缓存。
分享到:
相关推荐
EhCache使用详解,HIBERNATE缓冲
Ehcache使用: CacheManager主要的缓存管理类,一般一个应用为一个实例,如下 CacheManager.create();也可以使用new CacheManager的方式创建 默认的配置文件为ehcache.xml文件,也可以使用不同的配置: ...
每次需要shiro做权限控制, Realm的授权方法就会被调用, 查询数据库重新完成授权! 问题: 性能开销比较大 解决: 对用户授权,只进行一次 查询,查询后,将用户授权信息放入缓存中,以后需要授权时,直接从缓存...
Ehcache API,页面缓存,分布式缓存。
包括配置、原理讲解及使用示例。简洁的文档
EHCache使用手册,Hibernate默认的缓存
此为ehcache的使用以及集群的使用,具体需要两台机子,将ehcache 的hostName替换相应的ip即可
NULL 博文链接:https://huntt.iteye.com/blog/2058937
为了写这个例子我从网上找了好几天的资料,终于看到的效果,里面有非分布式的例子,有分布式例子(包括 rmi,jgroups tcp/udp 的分布式调用方式)。
ehcache使用例子,maven项目。
EHCache使用SimplePageCachingFilter类实现Filter缓存。该类继承自CachingFilter,有默认产生cache key的calculateKey()方法,该方法使用HTTP请求的URI和查询条件来组成key。也可以自己实现一个Filter,同样继承...
hibernate二级缓存的使用,快速使用手册,一学就会。Ehcache是一个快速的、轻量级的、易于使用的、进程内的缓存。它支持read-only 和read/write 缓存,内存和磁盘缓存。是一个非常轻量级的缓存实现
ehcache配置使用详解,里面有具体的例子
java EHCache使用,Hibernate缓存 收集整理
EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
cache、ehcache等缓存使用,实现存对象读对象等等
ehcache3-samples, 关于使用 Ehcache 3,一些示例/教程 Ehcache示例这里知识库包含有关 Ehcache 3用法的示例和教程。示例'basic'演示 Ehcache 3的基本配置和用法'集群'- 演示如何在Terracotta服务器上使用分布式缓存...
1.解压缩到目录下,复制ehcache-monitor-kit-1.0.0\lib\ehcache-probe-1.0.0.jar包到application的web-inf/lib目录下 2.将以下配置copy的ehcache.xml文件的ehcache标签中,注:上述链接中说的配置少写了个probe包名...
Spring与ehcache结合使用,本地缓存的实现