缓存二级缓存Spring环境下Hibernate二级缓存的应用
2013-05-17 22:20
375 查看
发一下牢骚和主题无关:
在数据不经常变化的地方,为了可以提高网站系统的执行效率,使用缓存是我们经常想到的处理方式。Hibernate的缓存分为三类,我们在平时应用开发中,最容易疏忽的就是它的一级缓存,因为它的生命周期与session分歧。二级缓存的应用是最广泛的,也是卓有成效的。那么怎么配置它呢?重要分为以下几步:
《1》搭建Spring+Hibernate环境
《2》加载Hibernate-ehcache相应的jar包
《3》在applicationContext.xml中添加缓存的相关配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" ></property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/entity/test</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<!-- 开启hibernate的查询缓存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- 开启hibernate二级缓存-->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- hibernate缓存管理类-->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!-- hibernate缓存配置文件-->
<prop key="hibernate.cache.configurationResourceName">ehcache.xml</prop>
</props>
</property>
</bean>
每日一道理
闷热的天,蝉儿耐不住寂寞地不停在鸣叫,我孤单一人,寂静的身旁没有一个知音,想疯狂地听摇滚乐,听歇斯底里的歌声,那只为逃避无人的世界里那浓烈的孤单气息。一个人是清冷,两个人便是精彩,于是,莫名的冲动让我格外想念旧日的好友,怀念过去的日子,尽管不够现实的遐想追回不了曾经一切,但却希望思绪可以飞扬于闭上双目后的世界中,印有微笑,印有舞动的身姿,翩翩起舞……
《4》在src目录下创建ehcache.xml缓存文件,内容大致如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir" />
<defaultCache eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU" />
<cache name="departCache"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
其中详细的含意可以参考网上的定义,这里不再标注。
《5》在dao中引入二级缓存:
HibernateTemplate template = this.getHibernateTemplate();
template.setCacheQueries(true);//开启hibernate二级缓存
List result = template.find(sql);
template.setCacheQueries(false);
总结:初次查询会连接数据库,其后会从缓存中返回结果,而不会继承从数据库中读取。
文章结束给大家分享下程序员的一些笑话语录:
手机终究会变成PC,所以ip会比wm更加畅销,但是有一天手机强大到一定程度了就会发现只有wm的支持才能完美享受。就好比树和草,草长得再高也是草,时间到了条件成熟了树就会窜天高了。www.ishuo.cn
在数据不经常变化的地方,为了可以提高网站系统的执行效率,使用缓存是我们经常想到的处理方式。Hibernate的缓存分为三类,我们在平时应用开发中,最容易疏忽的就是它的一级缓存,因为它的生命周期与session分歧。二级缓存的应用是最广泛的,也是卓有成效的。那么怎么配置它呢?重要分为以下几步:
《1》搭建Spring+Hibernate环境
《2》加载Hibernate-ehcache相应的jar包
《3》在applicationContext.xml中添加缓存的相关配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" ></property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/entity/test</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<!-- 开启hibernate的查询缓存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- 开启hibernate二级缓存-->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- hibernate缓存管理类-->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!-- hibernate缓存配置文件-->
<prop key="hibernate.cache.configurationResourceName">ehcache.xml</prop>
</props>
</property>
</bean>
每日一道理
闷热的天,蝉儿耐不住寂寞地不停在鸣叫,我孤单一人,寂静的身旁没有一个知音,想疯狂地听摇滚乐,听歇斯底里的歌声,那只为逃避无人的世界里那浓烈的孤单气息。一个人是清冷,两个人便是精彩,于是,莫名的冲动让我格外想念旧日的好友,怀念过去的日子,尽管不够现实的遐想追回不了曾经一切,但却希望思绪可以飞扬于闭上双目后的世界中,印有微笑,印有舞动的身姿,翩翩起舞……
《4》在src目录下创建ehcache.xml缓存文件,内容大致如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir" />
<defaultCache eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU" />
<cache name="departCache"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
其中详细的含意可以参考网上的定义,这里不再标注。
《5》在dao中引入二级缓存:
HibernateTemplate template = this.getHibernateTemplate();
template.setCacheQueries(true);//开启hibernate二级缓存
List result = template.find(sql);
template.setCacheQueries(false);
总结:初次查询会连接数据库,其后会从缓存中返回结果,而不会继承从数据库中读取。
文章结束给大家分享下程序员的一些笑话语录:
手机终究会变成PC,所以ip会比wm更加畅销,但是有一天手机强大到一定程度了就会发现只有wm的支持才能完美享受。就好比树和草,草长得再高也是草,时间到了条件成熟了树就会窜天高了。www.ishuo.cn
相关文章推荐
- Spring环境下Hibernate二级缓存的应用
- Spring-4.0.1+Hibernate-4.3.2+Struts-2.3.16环境配置(包含hibernate二级缓存和spring缓存)
- (转)为Spring集成的Hibernate配置二级缓存
- hibernate二级缓存应用
- spring+hibernate 二级缓存 配置+java使用实例
- SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本
- 使用hibernate二级缓存优化你的应用
- Spring 整合 Hibernate 时启用二级缓存实例详解
- hibernate 一二级缓存对比一级应用场景
- spring+hibernate 二级缓存 配置+java使用实例
- spring中配置hibernate3.X 二级缓存
- spring中配置hibernate二级缓存
- spring中配置hibernate二级缓存
- 【Hibernate】hibernate的二级缓存(spring)
- EJB3缓存 JPA环境下使用Hibernate二级缓存
- spring学习笔记16--Spring2.x+Hibernate3.x +Struts1.x整合开发(4)-----Hibernate3.x配置二级缓存
- Spring中启用Hibernate二级缓存步骤
- hibernate的二级缓存(spring) 帮我大忙啦!
- SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本
- spring中配置hibernate二级缓存