`
network-eagle
  • 浏览: 57984 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

hibernate 二级缓存 个人总结

阅读更多
今天特地的回顾了一下hibernate 的二级缓存, 我平常用到的是两种
1:   ehcache 
    1.1 首先配置 ehcache.xml  当然也可以默认 也可以自定义
<cache name="Student"
		maxElementsInMemory="80" eternal="false" overflowToDisk="false"
		timeToIdleSeconds="80" timeToLiveSeconds="80"   /> 

     说明: name 就是自定义的名称   
             maxElementsInMemory   缓存存储的总记录量
             eternal  缓存是否永远不销毁
             overflowToDisk  当缓存到达总数后是否覆盖原来的
             timeToIdleSeconds   当缓存空闲时间超过该值 则缓存自动销毁 感觉上没多大用处 可能是测试的时候 缓存量的问题
             timeToLiveSeconds 缓存创建之后,到达该缓存自动销毁 同上
    1.2 让后在用到的 hibernate映射文件中 添加
<hibernate-mapping>
    <class name="com.eagle.model.cache.Student" table="t_student" >
      [u]<cache usage="read-write" region="Student"  />[/u]   
        ………省略 ……………    </class>
</hibernate-mapping>

   表示该类要用缓存   另 可以在 hibernate.cfg.xml 添加
<class-cache class="com.eagle.model.cache.Student"
			usage="read-write" region="Student" />
  可以在hibernate.cfg.xml 统一管理那里类用到了缓存 
说明  region  指定使用哪个缓存机制。这个在ehcache 中所配置的
       usage   这个是必须的  缓存的策略: transactional、 read-write、 nonstrict-read-write或 read-only。
    1.3    在hibernate.cfg.xml 数据连接池 别忘记加上
   
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
		
		<property name="hibernate.cache.use_second_level_cache">true</property>



2 如果 你用的是 org.hibernate.cache.HashtableCacheProvider 
   就只需要在需要用的hibernate映射文件中 添加 <cache usage="read-write" />当然这个也可以跟 上面的一样可以统一在配置文件中管理起来 。
分享到:
评论
1 楼 imi00 2010-04-01  
overflowToDisk:
Sets whether elements can overflow to disk when the memory store
has reached the maxInMemory limit.
设置当内存对象达到内存缓存对象最大数量限制时,是否允许溢出保存到磁盘

timeToIdleSeconds:
Sets the time to idle for an element before it expires.
i.e. The maximum amount of time between accesses before an element expires
Is only used if the element is not eternal.
Optional attribute. A value of 0 means that an Element can idle for infinity.
The default value is 0.
设置缓存对象空闲失效时间

timeToLiveSeconds:
Sets the time to live for an element before it expires.
i.e. The maximum time between creation time and when an element expires.
Is only used if the element is not eternal.
Optional attribute. A value of 0 means that and Element can live for infinity.
The default value is 0.
设置缓存对象存活时间

相关推荐

Global site tag (gtag.js) - Google Analytics