hibernate 二级缓存 个人总结
今天特地的回顾了一下hibernate 的二级缓存, 我平常用到的是两种
1: ehcache
1.1 首先配置 ehcache.xml 当然也可以默认 也可以自定义
说明: name 就是自定义的名称
maxElementsInMemory 缓存存储的总记录量
eternal 缓存是否永远不销毁
overflowToDisk 当缓存到达总数后是否覆盖原来的
timeToIdleSeconds 当缓存空闲时间超过该值 则缓存自动销毁 感觉上没多大用处 可能是测试的时候 缓存量的问题
timeToLiveSeconds 缓存创建之后,到达该缓存自动销毁 同上
1.2 让后在用到的 hibernate映射文件中 添加
表示该类要用缓存 另 可以在 hibernate.cfg.xml 添加可以在hibernate.cfg.xml 统一管理那里类用到了缓存
说明 region 指定使用哪个缓存机制。这个在ehcache 中所配置的
usage 这个是必须的 缓存的策略: transactional、 read-write、 nonstrict-read-write或 read-only。
1.3 在hibernate.cfg.xml 数据连接池 别忘记加上
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.
设置缓存对象存活时间