您的位置:首页 > 其它

Hibernate学习笔记之EHCache的配置

2015-07-09 22:31 417 查看
转自:http://blog.csdn.net/hnzhangshilong/article/details/6689675,谢谢。

Hibernate默认二级缓存是不启动的,启动二级缓存(以EHCache为例)需要以下步骤:

1、添加相关的包:

Ehcache.jar和commons-logging.jar,如果hibernate.jar中含有ehcache就不用添加Ehcache.jar,commons-logging.jar是用来实现Ehcache写日志的。本示例使用Hibernate3.2

2、配置hibernate.cfg.xml文件

view plaincopy
to clipboardprint?

<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.cache.provider_class"> net.sf.ehcache.hibernate.EhCacheProvider</property><!-- 指定cache实现类 -->
<property name="cache.use_second_level_cache">true</property><!-- 启用二级缓存 -->
<property name="hibernate.cache.use_query_cache">true</property><!-- 启用查询缓存 -->
<!-- <property name="hibernate.cache.provider_configuration_file_resource_path">ehcache2.xml</property>//指定ehcache配置文件 -->

<mapping class="test.po.TUser" />

</session-factory>
</hibernate-configuration>

<hibernate-configuration> <session-factory> <property name="hibernate.show_sql">true</property> <property name="hibernate.cache.provider_class"> net.sf.ehcache.hibernate.EhCacheProvider</property><!-- 指定cache实现类 --> <property name="cache.use_second_level_cache">true</property><!--
启用二级缓存 --> <property name="hibernate.cache.use_query_cache">true</property><!-- 启用查询缓存 --> <!-- <property name="hibernate.cache.provider_configuration_file_resource_path">ehcache2.xml</property>//指定ehcache配置文件 --> <mapping class="test.po.TUser" />
</session-factory></hibernate-configuration>

3、添加配置文件--ehcache.xml,一般放在classpath或src下,也可以自定义文件名和路径,并在hibernate.cfg.xml中通过 hibernate.cache.provider_configuration_file_resource_path参数指定。

view plaincopy
to clipboardprint?

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="1000"
timeToLiveSeconds="1000"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>

<?xml version="1.0" encoding="UTF-8"?><ehcache><defaultCache maxElementsInMemory="10000"
eternal="false" timeToIdleSeconds="1000" timeToLiveSeconds="1000" overflowToDisk="false" memoryStoreEvictionPolicy="LRU"/></ehcache>

其中:

maxElementsInMemory=“10000” //Cache中最多允许保存的数据对象的数量

external=“false” //缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期

timeToIdleSeconds=“1000” //缓存数据钝化时间(设置对象在它过期之前的空闲时间)

timeToLiveSeconds=“1000” //缓存数据的生存时间(设置对象在它过期之前的生存时间)

overflowToDisk=“false” /> //内存不足时,是否启用磁盘缓存

memoryStoreEvictionPolicy="LRU" //内存不足时数据对象的清除策略

ehcache中缓存的3种清空策略:

FIFO(first in first out):先进先出

LFU( Less Frequently Used):一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。

LRU(Least Recently Used):最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。

4、配置相关实体的缓存策略

view plaincopy
to clipboardprint?

@Entity
@Table(name="cui_user")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)//可读可写
public class TUser {
private Integer id;
private String name;

@Id //标识主键
@GeneratedValue(strategy=GenerationType.AUTO)//指定主键值的产生策略由Hibernate根据数据库字段选择
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

最后还是需要测试的。通过id缓存的例子如下:

view plaincopy
to clipboardprint?

public static void main(String[] args) {
//单对象缓存
Session session=HibernateSessionFactory.getSession();
TUser user=(TUser)session.load(TUser.class, 200);
System.out.println("1---"+user.getName());
session.close();

try {
Thread.sleep(2000);//休眠2秒
} catch (InterruptedException e) {
e.printStackTrace();
}

session=HibernateSessionFactory.getSession();
TUser user2=(TUser)session.load(TUser.class, 200);
System.out.println("2---"+user2.getName());
session.close();
}

@Entity@Table(name="cui_user")@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)//可读可写public class TUser {private Integer id;private String name;@Id
//标识主键@GeneratedValue(strategy=GenerationType.AUTO)//指定主键值的产生策略由Hibernate根据数据库字段选择public Integer getId() {
return id;}public void setId(Integer id) {
this.id = id;}public String getName() { return name;}public void setName(String name) {
this.name = name;}}

Hibernate生成的sql语句:

Hibernate: select tuser0_.id as id0_0_, tuser0_.name as name0_0_ from cui_user tuser0_ where tuser0_.id=?

1---cuisea

2---cuisea

可见第二次读取TUser对象并没有去数据库查询,说明是从缓存里读取的,ehcache配置成功。

查询缓存(必须在hibernate.cfg.xml中配置hibernate.cache.use_query_cache为true)的例子如下:

view plaincopy
to clipboardprint?

public static void main(String[] args) {
Session session=HibernateSessionFactory.getSession();
Query query=session.createQuery("from TUser");//使用Query缓存结果集
query.setCacheable(true);//必须设置
List<TUser> list=query.list();
for (TUser user : list) {
System.out.println("1---"+user.getName());
}

//另外开启一个事务
session=HibernateSessionFactory.getSession();
query=session.createQuery("from TUser");
query.setCacheable(true);//必须设置
list=query.list();
for (TUser user : list) {
System.out.println("2---"+user.getName());
}
}

public static void main(String[] args) { Session session=HibernateSessionFactory.getSession();
Query query=session.createQuery("from TUser");//使用Query缓存结果集
query.setCacheable(true);//必须设置 List<TUser> list=query.list();
for (TUser user : list) { System.out.println("1---"+user.getName());
} //另外开启一个事务
session=HibernateSessionFactory.getSession();
query=session.createQuery("from TUser");
query.setCacheable(true);//必须设置 list=query.list();
for (TUser user : list) { System.out.println("2---"+user.getName());
}}

Hibernate生成的sql语句:

Hibernate: select tuser0_.id as id0_, tuser0_.name as name0_ from cui_user tuser0_

1---tester

1---cuisea

2---tester

2---cuisea

可见,第二次查询并没有从数据库查询,而是从缓存中取数据。查询缓存使用hibernate生成的sql语句和参数作为key缓存起来,当执行相同的sql并使用相同参数时从缓存取数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: