ehcache是Hibernate的二级缓存,Hibernate有两种缓存一种是Session里面的一级缓存,
再有就是第三方的二级缓存,这个必须手工配置,要不然是不会生效的,
用Ehcache并不代表不操作数据库了,而是它会把实体类在内存中放一个缓存, 当不修改数据库时,
它就不用时行数据库操作了,比如说读取的操作,如果进行保存的话,他也会更新缓存的。
配置方法很简单:
在Application.xml里的SessionFactory里加上如下:
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">
true
</prop>
这是启用缓存,另外还需要在ClassPath下建立:
ehcache.xml这个文件,里面这样写就行了:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- enable second level cache -->
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="180" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" overflowToDisk="false" />
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="100" eternal="true" overflowToDisk="true" />
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="100" eternal="false" timeToLiveSeconds="120"
overflowToDisk="true" />
<cache name="com.clove.cms.model.User" maxElementsInMemory="100"
eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="180"
overflowToDisk="false" />
</ehcache>User,就是你的实体类了!
别忘了加Jar包呀!
再有就是第三方的二级缓存,这个必须手工配置,要不然是不会生效的,
用Ehcache并不代表不操作数据库了,而是它会把实体类在内存中放一个缓存, 当不修改数据库时,
它就不用时行数据库操作了,比如说读取的操作,如果进行保存的话,他也会更新缓存的。
配置方法很简单:
在Application.xml里的SessionFactory里加上如下:
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">
true
</prop>
这是启用缓存,另外还需要在ClassPath下建立:
ehcache.xml这个文件,里面这样写就行了:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- enable second level cache -->
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="180" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" overflowToDisk="false" />
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="100" eternal="true" overflowToDisk="true" />
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="100" eternal="false" timeToLiveSeconds="120"
overflowToDisk="true" />
<cache name="com.clove.cms.model.User" maxElementsInMemory="100"
eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="180"
overflowToDisk="false" />
</ehcache>User,就是你的实体类了!
别忘了加Jar包呀!
解决方案 »
- java指纹识别+谷歌图片识别技术=======源码实现===分享出来===
- 求!hibernate缓存同步问题!!!
- struts求助!
- 如何根据某个html表单元素被选中来选择相应的servlet来处理
- 分层的问题
- 关于<html:select>的使用,紧急!!!
- 帮忙呀,struts我快疯了!
- 我想投身JAVA阵营,不知道从哪里开始。
- 求救:我第一次用JBuilder7+WebLogic7.0的配置开发的第一个EJB发生错误
- idea启动项目是报错。求解。
- Could not read mappings from resource
- import javax.ejb.Remote;报错是什么原因?
http://www.javaeye.com/topic/18904