用户登录
用户注册

分享至

hibernate二级缓存配置

  • 作者: 你带孩子先走-
  • 来源: 51数据库
  • 2020-09-24
答案转载自:http://blog.csdn.net/runming56/article/details/8574047

如何在程序里使用二级缓存:
首先在hibernate.cfg.xml开启二级缓存

Xml代码




......


true

true






net.sf.ehcache.hibernate.EhCacheRegionFactory





true


true

......




然后是ehcache配置(ehcache.xml)
cache参数详解:
● name:指定区域名
● maxElementsInMemory :缓存在内存中的最大数目
● maxElementsOnDisk:缓存在磁盘上的最大数目
● eternal :设置是否永远不过期
● overflowToDisk : 硬盘溢出数目
● timeToIdleSeconds :对象处于空闲状态的最多秒数后销毁
● timeToLiveSeconds :对象处于缓存状态的最多秒数后销毁
● memoryStoreEvictionPolicy:缓存算法,有LRU(默认)、LFU、LFU

关于缓存算法,常见有三种:
● LRU:(Least Rencently Used)新来的对象替换掉使用时间算最近很少使用的对象
● LFU:(Least Frequently Used)替换掉按命中率高低算比较低的对象
● LFU:(First In First Out)把最早进入二级缓存的对象替换掉

Xml代码














在每个实体的hbm文件中配置cache元素,usage可以是read-only或者是read-write等4种。

Xml代码


"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >





......



也可以用Hibernate注解配置缓存实体类

Java代码

@Entity
@Table
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User implements Serializable {
private static final long serialVersionUID = -5121812640999313420L;
private Integer id;
private String name;

......
}



  答案转载自:

如何在程序里使用二级缓存:
首先在hibernate.cfg.xml开启二级缓存

xml代码




......


true

true






net.sf.ehcache.hibernate.ehcacheregionfactory





true


true

......




然后是ehcache配置(ehcache.xml)
cache参数详解:
● name:指定区域名
● maxelementsinmemory :缓存在内存中的最大数目
● maxelementsondisk:缓存在磁盘上的最大数目
● eternal :设置是否永远不过期
● overflowtodisk : 硬盘溢出数目
● timetoidleseconds :对象处于空闲状态的最多秒数后销毁
● timetoliveseconds :对象处于缓存状态的最多秒数后销毁
● memorystoreevictionpolicy:缓存算法,有lru(默认)、lfu、lfu

关于缓存算法,常见有三种:
● lru:(least rencently used)新来的对象替换掉使用时间算最近很少使用的对象
● lfu:(least frequently used)替换掉按命中率高低算比较低的对象
● lfu:(first in first out)把最早进入二级缓存的对象替换掉

xml代码














在每个实体的hbm文件中配置cache元素,usage可以是read-only或者是read-write等4种。

xml代码


"-//hibernate/hibernate mapping dtd 3.0//en"
"" >





......



也可以用hibernate注解配置缓存实体类

java代码

@entity
@table
@cache(usage = cacheconcurrencystrategy.read_write)
public class user implements serializable {
private static final long serialversionuid = -5121812640999313420l;
private integer id;
private string name;

......
}
软件
前端设计
程序设计
Java相关