Hibernate和Spring的缓存机制
2016-03-23 14:10
281 查看
在Hibernate配置文件中设置:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Hibernate SessionFactory -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="dataSource" ref="dataSource"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="mappingResources">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>com/ouou/model/Videos.hbm.xml</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernateProperties">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.current_session_context_class">thread</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--add ehcache-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查询缓存 -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.show_sql">true</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--<prop key="hibernate.transaction.auto_close_session">true</prop>-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Create/update the database tables automatically when the JVM starts up
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.hbm2ddl.auto">update</prop> -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Turn batching off for better error messages under PostgreSQL -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.jdbc.batch_size">25</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.connection.pool_size">10</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cachetrue 才行。
2.首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<defaultCache
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
maxElementsInMemory="10000"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
eternal="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
overflowToDisk="true"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
timeToIdleSeconds="120"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
timeToLiveSeconds="120"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
diskPersistent="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
diskExpiryThreadIntervalSeconds="120"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
eternal="true" overflowToDisk="true"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
timeToLiveSeconds="120" overflowToDisk="true"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=
"600"
timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
diskPersistent="false"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
timeToLiveSeconds="600"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
overflowToDisk="false" diskPersistent="false"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="com.ouou.model.Videos" maxElementsInMemory="10000" eternal="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="com.ouou.model.Tags" maxElementsInMemory="10000" eternal="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</ehcache>
以com.ouou.model.Videos为例子
在Videos.hbm.xml中配置:
<class name="Videos" table="TEST"lazy="false">
<cache usage="read-write"region="ehcache.xml中的name的属性值"/>注意:这一句需要紧跟在class标签下面,其他位置无效。
hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.ouou.model.Videos的cache,
如果不存在与类名匹配的cache名称,则用defaultCache。
如果Videos包含set集合,则需要另行指定其cache
例如Videos包含Tags集合,则需要
添加如下配置到ehcache.xml中
<cache name="com.ouou.model.Tags"
maxElementsInMemory="10000" eternal="false"timeToIdleSeconds="120"
timeToLiveSeconds="120" overflowToDisk="false"/>
另,针对查询缓存的配置如下:
<cachename="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true"/>
<cachename="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="10000"
eternal="false"
timeToLiveSeconds="120"
overflowToDisk="true"/>
3、 选择缓存策略依据:
<cache usage="transactional|read-write|nonstrict-read-write|read-only"(1)/>
ehcache不支持transactional,其他三种可以支持。
read-only:无需修改,那么就可以对其进行只读缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,
但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。
read-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transactionisolation level
(序列化事务隔离级别)
nonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,
那么比较适合使用非严格读/写缓存策略。
4、调试时候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作过程,主要用于调试过程,实际应用发布时候,请注释掉,以免影响性能。
5、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集(
org.hibernate.cache.StandardQueryCache);另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。
请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。
需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。
--------------------------------------------------------------------------------------------------
spring 3.0.5 发布后,公司使用Spring MVC +Hibernate 3.5做项目,其中用到了缓存机制,spring 3.0.5中ehcache配置方法很简单,其中缓存机制很细颗粒化,可以具体到把每个方式的返回值做缓存,好了不说废话下面开始:
需要JAR包:
第一:spring 3.0.5 其中JAR;
第二:另外需要增量JAR包(cglib-2.2.jar,ehcache-spring-annotations-1.1.2.jar)注意版本;
其中applicationContext.xml 其中配置:
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<ehcache:annotation-drivencache-manager="ehCacheManager" />
<bean id="ehCacheManager"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation"value="classpath:ehcache.xml"/>
</bean>
加到你的文件中去,上边是头信息自己可以比照下,没有的加进去;
在你的 src 目录下新建ehcache.xml内容如下:
<?xml version="1.0"encoding="UTF-8"?>
<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir"/>
<defaultCacheeternal="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>
其中里边详细配置自己可以去上网搜下;
这样基本上配置完成了:
DAO层缓存:例如下边这个方法的返回值需要缓存:
@SuppressWarnings("unchecked")
//spring 3 基于注解ehcache缓存配置;
@Cacheable(cacheName="departCache")
publicList<AppDepart>getChildDepart(Integer id) throws Exception {
return this.getHibernateTemplate().find("fromAppDepart where state=1 and idParent="+id);
}
@Cacheable(cacheName="departCache") 加上这句话,其中cacheName对应ehcache.xml 中的<cachename="departCache"
这样这个方法返回值就可以被缓存起来的了,但是怎么样把缓存数据和数据库中的数据实现同步呢?
如果对这个PO做update ,save,delete 可以实现这样策略如下:
@Transactional(propagation = Propagation.REQUIRED)
//设定spring的ecache缓存策略,当编辑机构时候,把缓存全部清除掉,以达到缓存那数据同步;
@TriggersRemove(cacheName="departCache",removeAll=true)
public boolean editDepart(String depno, StringdepName) {
boolean flag = false;
try {
AppDepartdepart = departDao.getAppdepart(depno);
depart.setDepName(depName);
departDao.update(depart);
flag =true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Hibernate SessionFactory -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="dataSource" ref="dataSource"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="mappingResources">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>com/ouou/model/Videos.hbm.xml</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernateProperties">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.current_session_context_class">thread</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--add ehcache-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查询缓存 -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.show_sql">true</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--<prop key="hibernate.transaction.auto_close_session">true</prop>-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Create/update the database tables automatically when the JVM starts up
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.hbm2ddl.auto">update</prop> -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Turn batching off for better error messages under PostgreSQL -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.jdbc.batch_size">25</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<prop key="hibernate.connection.pool_size">10</prop>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</props>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cachetrue 才行。
2.首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<defaultCache
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
maxElementsInMemory="10000"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
eternal="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
overflowToDisk="true"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
timeToIdleSeconds="120"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
timeToLiveSeconds="120"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
diskPersistent="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
diskExpiryThreadIntervalSeconds="120"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
eternal="true" overflowToDisk="true"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
timeToLiveSeconds="120" overflowToDisk="true"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=
"600"
timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
diskPersistent="false"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
timeToLiveSeconds="600"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
overflowToDisk="false" diskPersistent="false"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="com.ouou.model.Videos" maxElementsInMemory="10000" eternal="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<cache name="com.ouou.model.Tags" maxElementsInMemory="10000" eternal="false"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</ehcache>
以com.ouou.model.Videos为例子
在Videos.hbm.xml中配置:
<class name="Videos" table="TEST"lazy="false">
<cache usage="read-write"region="ehcache.xml中的name的属性值"/>注意:这一句需要紧跟在class标签下面,其他位置无效。
hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.ouou.model.Videos的cache,
如果不存在与类名匹配的cache名称,则用defaultCache。
如果Videos包含set集合,则需要另行指定其cache
例如Videos包含Tags集合,则需要
添加如下配置到ehcache.xml中
<cache name="com.ouou.model.Tags"
maxElementsInMemory="10000" eternal="false"timeToIdleSeconds="120"
timeToLiveSeconds="120" overflowToDisk="false"/>
另,针对查询缓存的配置如下:
<cachename="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true"/>
<cachename="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="10000"
eternal="false"
timeToLiveSeconds="120"
overflowToDisk="true"/>
3、 选择缓存策略依据:
<cache usage="transactional|read-write|nonstrict-read-write|read-only"(1)/>
ehcache不支持transactional,其他三种可以支持。
read-only:无需修改,那么就可以对其进行只读缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,
但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。
read-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transactionisolation level
(序列化事务隔离级别)
nonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,
那么比较适合使用非严格读/写缓存策略。
4、调试时候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作过程,主要用于调试过程,实际应用发布时候,请注释掉,以免影响性能。
5、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集(
org.hibernate.cache.StandardQueryCache);另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。
请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。
需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。
--------------------------------------------------------------------------------------------------
spring 3.0.5 发布后,公司使用Spring MVC +Hibernate 3.5做项目,其中用到了缓存机制,spring 3.0.5中ehcache配置方法很简单,其中缓存机制很细颗粒化,可以具体到把每个方式的返回值做缓存,好了不说废话下面开始:
需要JAR包:
第一:spring 3.0.5 其中JAR;
第二:另外需要增量JAR包(cglib-2.2.jar,ehcache-spring-annotations-1.1.2.jar)注意版本;
其中applicationContext.xml 其中配置:
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<ehcache:annotation-drivencache-manager="ehCacheManager" />
<bean id="ehCacheManager"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation"value="classpath:ehcache.xml"/>
</bean>
加到你的文件中去,上边是头信息自己可以比照下,没有的加进去;
在你的 src 目录下新建ehcache.xml内容如下:
<?xml version="1.0"encoding="UTF-8"?>
<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir"/>
<defaultCacheeternal="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>
其中里边详细配置自己可以去上网搜下;
这样基本上配置完成了:
DAO层缓存:例如下边这个方法的返回值需要缓存:
@SuppressWarnings("unchecked")
//spring 3 基于注解ehcache缓存配置;
@Cacheable(cacheName="departCache")
publicList<AppDepart>getChildDepart(Integer id) throws Exception {
return this.getHibernateTemplate().find("fromAppDepart where state=1 and idParent="+id);
}
@Cacheable(cacheName="departCache") 加上这句话,其中cacheName对应ehcache.xml 中的<cachename="departCache"
这样这个方法返回值就可以被缓存起来的了,但是怎么样把缓存数据和数据库中的数据实现同步呢?
如果对这个PO做update ,save,delete 可以实现这样策略如下:
@Transactional(propagation = Propagation.REQUIRED)
//设定spring的ecache缓存策略,当编辑机构时候,把缓存全部清除掉,以达到缓存那数据同步;
@TriggersRemove(cacheName="departCache",removeAll=true)
public boolean editDepart(String depno, StringdepName) {
boolean flag = false;
try {
AppDepartdepart = departDao.getAppdepart(depno);
depart.setDepName(depName);
departDao.update(depart);
flag =true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
相关文章推荐
- java枚举使用详解
- (整理)Java实现链表----链表的反转
- Spring IOC
- Lombok 安装、入门 - 消除冗长的 java 代码
- java介绍
- 各种java时间格式转换
- idea Intellij 远程调试java程序
- eclipse 引用(依赖) 其他 lib 工程(可只做最后一步)
- Java中的final关键字-----(java复习)
- Java [Leetcode 338]Counting Bits
- Java核心知识点-synchronized关键字
- spring学习笔记(14)引介增强详解:定时器实例:无侵入式动态增强类功能
- Struts2 doubleselect错误Expression parameters.formName
- Java 数据库操作
- java多线程问题
- java集合(每日学习) Fighting!!!
- JavaWeb学习总结(十三)——使用Session防止表单重复提交
- servlet
- SSH学习之Struts1深入学习
- Java Maven项目引入Maven Dependencies