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)
分享到:
相关推荐
Spring-boot,hibernate ehcache 整合源码,可直接运行
hibernate+ehcache hibernate+ehcache hibernate+ehcache hibernate+ehcache hibernate+ehcache
Hibernate EhCache 二级缓存配置
spring,spring mvc,hibernate,ehcache JavaWeb后台框架
包含jar包:ehcache-core-2.6.8.jar、hibernate-ehcache-4.1.0.Final.jar
hibernate整合ehcache的jar包
spring、 spring mvc、 hibernate、 ehcache Java后台框架
NULL 博文链接:https://dreamoftch.iteye.com/blog/2208828
spring,spring mvc,hibernate,ehcache Java后台框架
spring+springmvc+hibernate+ehcache JavaWeb后台框架,不仅提高了开发程序的速度,且其中还是用到hibernate和ehcache缓存的使用,加快了程序运行的数据,该框架亲测好用。值得注意的是该种框架现在还算是用的比较多...
NULL 博文链接:https://dreamoftch.iteye.com/blog/1985366
osprey.zip,另一个redis java客户端,在单个jvm中使用的可靠异步消息。
NULL 博文链接:https://llying.iteye.com/blog/492307
该包是hibernate中所用的一个包,专门用来去处理特定的问题,它是和ehcache一起用的一个包
spring3 hibernate4 ehcache整合的实例
hibernate-ehcache-4.1.0.Final.jar 是hibernate4.1使用缓存的jar包
自己整合的Spring+Hibernate+Ehcache 整合的项目 需要struts的自己添加。 里面带有注释,而且带Jar.写的很清楚,良心分。
6.Hibernate继承 HibernateDaoSupport。 7.Spring+Junit4单元测试,优点:不会破坏数据库现场,等等。 2)Demo 导入说明: 1.Eclipse Encoding:GBK 2.Eclipse 导入后可能需要在 Xml Catalog 手动添加:ehcache-...
hibernate ehcache.xml 配置详解
hibernate-ehcache-5.3.0.jar本来是想免费的,奈何最低也要一个资源分数