Hibernate EHCache

By default, Hibernate uses EHCache for JVM-level caching. (JCS support is now deprecated and will be removed in a future

version of Hibernate.) You may choose a different implementation by specifying the name of a class that implements

net.sf.hibernate.cache.CacheProvider using the property hibernate.cache.provider_class.

 

以上引用自:http://www.hibernate.org/hib_docs/reference/en/html/performance.html#performance-cache

 

EHCache支持read-only和read/write缓存,内存和磁盘缓存。是一个非常轻量级的缓存实现,而且从1.2 之后就支持了集群,目前的最新版本好像是2.0了。
集群时候推荐使用jboss-cache。

 

EHCache的使用场合:
1、比较少更新表数据
   EhCache一般要使用在比较少执行write操作的表(包括update,insert,delete等)[Hibernate的二级缓存也都是这样];
2、对并发要求不是很严格的情况
   两台机子中的缓存是不能实时同步的;

 

步骤:
1. 先下载ehcache的jar包。
   最新版本: ehcache-1.4 released。
   解压后,有几个文件:
   ehcache-1.4.0.jar:需要将它放置到WEB-INF/lib下
   ehcache-1.4.0-remote-debugger.jar:不要发布到工程中,是用来调试和监控你的cache状况的
   ehcache-1.4.0-sources.jar:源代码
   ehcache.xml:重要的配置文件,需要复制到classpath下 。

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache> 
 <diskStore path="Java.io.tmpdir"/>
 <defaultCache maxElementsInMemory="10000" <!-- 缓存最大数目 --> 
  eternal="false" <!-- 缓存是否持久 --> 
  overflowToDisk="true" <!-- 当缓存中的数量超过限制时,是否持久化到硬盘 --> 
  timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 --> 
  timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁 --> 
  diskPersistent="false" 
  diskExpiryThreadIntervalSeconds="120"/> 
</ehcache>

 

2. 修改hbm.xml配置

<hibernate-mapping package="******">
 <class name="******" table="******">
  <!-- enable second level cache -->
  <cache usage="read-write"/>

  <id name="id">
   <generator class="native"/>
  </id>

  <property name="******" type="java.lang.Integer" not-null="true"/>
 </class> 
</hibernate-mapping>

 

3. 修改log4j和hibernate配置

 

# log4j.properties
log4j.logger.net.sf.hibernate=info
log4j.logger.net.sf.hibernate.cache=debug

# hibernate配置
hibernate.show_sql true

 

使用QueryCache缓存时加上如下配置,在程序中需要为Query对象设置Cachable属性:

hibernate.cache.use_query_cache true 

 

query.setCacheable(true);

 

另:Ehcache可以利用Terracotta方式同步缓存

参考:http://nlslzf.iteye.com/blog/563114

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

关于网站首页的页面缓存

 

缓存策略:当用户第一次请求时,页面被缓存,再次访问时,过滤器进行过滤,不再执行action,从而降低服务器的压力并提高客户端响应速度。

如设置缓存存活时间为2分钟,将如下配置加入到ehcache.xml中:

<cache name="simplePageCachingFilter"
 maxElementsInMemory="10"
 maxElementsOnDisk="10"
 eternal="false"
 overflowToDisk="true"
 diskSpoolBufferSizeMB="20"
 timeToIdleSeconds="60"
 timeToLiveSeconds="120" />

 

simplePageCachingFilter 缓存的名称
maxElementsInMemory 缓存中元素的最大数量
maxElementsOnDisk 持久化到硬盘的缓存元素的最大数量
eternal="false" 缓存会被清除
overflowToDisk="true" 当缓存中元素数量超过限制时,将这些元素持久化到硬盘,为false时,设置没意义。
timeToIdleSeconds 多长时间不访问缓存,那么就清除该缓存
timeToLiveSeconds 缓存的存活时间


对应web.xml中的配置:

<filter> 
    <filter-name>indexCacheFilter</filter-name> 
    <filter-class>
        net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter
    </filter-class> 
</filter>
  
<filter-mapping> 
    <filter-name>indexCacheFilter</filter-name> 
    <url-pattern>*index.do</url-pattern> 
</filter-mapping>

 

 新增附件:缓存技术浅谈.ppt.zip(Robin)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值