原文地址:http://ligson.iteye.com/blog/923959
1、在hibernate中配置
2、在src下建立echcache.xml
3、在类资源文件中加入
缓存策略
只读缓存(read-only):没有什么好说的
读/写缓存(read-write):程序可能要的更新数据
不严格的读/写缓存(nonstrict-read-write):需要更新数据,但是两个事务更新同一条记录的可能性很小,性能比读写缓存好
事务缓存(transactional):缓存支持事务,发生异常的时候,缓存也能够回滚,只支持jta环境,这个我没有怎么研究过
还可以在hibernate配置文件加入
<class-cache usage="read-only" class=""/>
--------------------------------------------------------------------------------
使用二级缓存的前提条件
------
你的hibernate程序对数据库有独占的写访问权,其他的进程更新了数据库,hibernate是不可能知道的。你操作数据库必需直接通过hibernate,如果你调用存储过程,或者自己使用jdbc更新数据库,hibernate也是不知道的。hibernate3.0的大批量更新和删除是不更新二级缓存的,但是据说3.1已经解决了这个问题。
这个限制相当的棘手,有时候hibernate做批量更新、删除很慢,但是你却不能自己写jdbc来优化,很郁闷吧。
SessionFactory也提供了移除缓存的方法,你一定要自己写一些JDBC的话,可以调用这些方法移除缓存,这些方法是:
不过我不建议这样做,因为这样很难维护。比如你现在用JDBC批量更新了某个表,有3个查询缓存会用到这个表,用evictQueries(String cacheRegion)移除了3个查询缓存,然后用evict(Class persistentClass)移除了class缓存,看上去好像完整了。不过哪天你添加了一个相关查询缓存,可能会忘记更新这里的移除代码。如果你的jdbc代码到处都是,在你添加一个查询缓存的时候,还知道其他什么地方也要做相应的改动吗?
- <!-- 启用二级缓存 -->
- <prop key="hibernate.cache.use_second_level_cache">true</prop>
- <!-- 设置缓存提供者 -->
- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
- <!--使用查询缓存-->
- <!--
- query.setCacheable(true);//激活查询缓存
- query.setCacheRegion("myCacheRegion");//指定要使用的cacheRegion,可选
- -->
- <prop key="hibernate.cache.use_query_cache">true</prop>
2、在src下建立echcache.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache>
- <diskStore path="java.io.tmpdir"/>
- <defaultCache
- maxElementsInMemory="10000"
- eternal="false"
- overflowToDisk="true"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- diskPersistent="false"
- />
- </ehcache>
3、在类资源文件中加入
- <cache usage="read-only"/>
- <!--
- <cache usage="nonstrict-read-write"/>
- <cache usage="read-write"/>
- <cache usage="transactional"/>
- -->
缓存策略
只读缓存(read-only):没有什么好说的
读/写缓存(read-write):程序可能要的更新数据
不严格的读/写缓存(nonstrict-read-write):需要更新数据,但是两个事务更新同一条记录的可能性很小,性能比读写缓存好
事务缓存(transactional):缓存支持事务,发生异常的时候,缓存也能够回滚,只支持jta环境,这个我没有怎么研究过
还可以在hibernate配置文件加入
<class-cache usage="read-only" class=""/>
--------------------------------------------------------------------------------
使用二级缓存的前提条件
------
你的hibernate程序对数据库有独占的写访问权,其他的进程更新了数据库,hibernate是不可能知道的。你操作数据库必需直接通过hibernate,如果你调用存储过程,或者自己使用jdbc更新数据库,hibernate也是不知道的。hibernate3.0的大批量更新和删除是不更新二级缓存的,但是据说3.1已经解决了这个问题。
这个限制相当的棘手,有时候hibernate做批量更新、删除很慢,但是你却不能自己写jdbc来优化,很郁闷吧。
SessionFactory也提供了移除缓存的方法,你一定要自己写一些JDBC的话,可以调用这些方法移除缓存,这些方法是:
- void evict(Class persistentClass)
- Evict all entries from the second-level cache.
- void evict(Class persistentClass, Serializable id)
- Evict an entry from the second-level cache.
- void evictCollection(String roleName)
- Evict all entries from the second-level cache.
- void evictCollection(String roleName, Serializable id)
- Evict an entry from the second-level cache.
- void evictQueries()
- Evict any query result sets cached in the default query cache region.
- void evictQueries(String cacheRegion)
- Evict any query result sets cached in the named query cache region.
不过我不建议这样做,因为这样很难维护。比如你现在用JDBC批量更新了某个表,有3个查询缓存会用到这个表,用evictQueries(String cacheRegion)移除了3个查询缓存,然后用evict(Class persistentClass)移除了class缓存,看上去好像完整了。不过哪天你添加了一个相关查询缓存,可能会忘记更新这里的移除代码。如果你的jdbc代码到处都是,在你添加一个查询缓存的时候,还知道其他什么地方也要做相应的改动吗?