ehcache配置

1.ehcache-2.10.3.jar slf4j-api.jar sl44j-jdk.jar 开始用ehcache3.1和hibernate-ehcache.jar 不匹配,坑的要死
2.hibernate-ehcache.jar spring-context-support.jar 这两jar包里面有对ehcache的支持。
3.hibernate配置
<prop key="cache.use_second_level_cache">true</prop>
<prop key="cache.use_query_cache">true</prop>
<!--hibernate3 -->
<!-- <prop key="cache.provider_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</prop> -->
<!--hibernate4 搞错版本会一直报provider错误-->
<prop key="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</prop>

4. springMVC中配置,此处有坑,p:shared="true"不加这个的话,使用二级缓存的时候会报已经存在一个vm。
xmlns:p="http://www.springframework.org/schema/p"

<cache:annotation-driven cache-manager="cacheManager" />
<!-- 缓存 属性-->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>

5.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="java.io.tempdir" />
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true" />
<cache name="user"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="1800"
timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
又是抄袭:
 name:缓存名称。 
 maxElementsInMemory:缓存最大个数。 
 eternal:对象是否永久有效,一但设置了,timeout将不起作用。 
 timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 
 timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 
 overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 
 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 
 maxElementsOnDisk:硬盘最大缓存个数。 
 diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. 
 diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 
 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 
 clearOnFlush:内存数量最大时是否清除。 

6.实体类配置
@Entity
@Table(name="t_user")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY, include="all", region="user")
public class User implements Serializable{
其中
read- only 无需修改, 可以对其进行只读缓存,注意:在此策略下,如果直接修改数据库,即使能够看到前台显示效果,但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。
read-write 需要更新数据,那么使用读/写缓存比较合适,前提: 数据库不可以为serializable transaction isolation level(序列化事务隔离级别)
nonstrict-read-write 只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,那么比较适合使用非严格读/写缓存策略。
include=“all” 即使延迟加载的数据也会被缓存,
region="user"对应ehcache.xml中的配置


7.方法配置,dao或者service应该都是可以的。但是注意list不能用,但是序列化list之后应该是可以的,我没有尝试过
@Override
@Cacheable(value="sampleCache1",key="#id")
public T get(int id) {
return (T) getSession().get(getClz(), id);
}

8. @CacheEvict (value= "serviceCache" ,allEntries= true )  删除

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值