首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

EHCache的简略使用

2012-11-01 
EHCache的简单使用一、创建ehcache配制文件,放到classes目录下?xml version1.0 encodingUTF-8?!--

EHCache的简单使用

一、创建ehcache配制文件,放到classes目录下

<?xml version="1.0" encoding="UTF-8"?>
<!-- <ehcache xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”ehcache.xsd”
updateCheck="true" monitoring="autodetect" dynamicConfig="true"> -->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
?<!-- 磁盘存储路径 -->???
??? <diskStore path="c:\\myapp\\cache"/>???
??? <defaultCache??
??????? maxElementsInMemory="10000"??
??????? eternal="false"??
??????? timeToIdleSeconds="120"??
??????? timeToLiveSeconds="120"??
??????? overflowToDisk="true"??
??????? diskSpoolBufferSizeMB="30"??
??????? maxElementsOnDisk="10000000"??
??????? diskPersistent="false"??
??????? diskExpiryThreadIntervalSeconds="120"??
??????? memoryStoreEvictionPolicy="LRU"??
??????? />

</ehcache>

?

二、修改hibernate.cfg.xml配制文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
??<session-factory>
????<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
????<property name="cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property>
????<property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
????<property name="hibernate.cache.use_second_level_cache">true</property>
????<property name="hibernate.cache.use_query_cache">true</property>
????<property name="hibernate.generate_statistics">true</property>
????<property name="show_sql">true</property>
????<!-- <property name="hbm2ddl.auto">update</property>????
????<property name="namingStrategy">net.sf.hibernate.cfg.ImprovedNamingStrategy</property> -->
????
????<!-- pojo 配制
????<mapping encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?xsi:schemaLocation="http://www.springframework.org/schema/beans
?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

??<bean id="sessionFactory" ref="dataSource"/>
????<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
????<property name="lobHandler" ref="lobHandler"></property>
??</bean>??
????
??<bean id="myTxManager" ref="sessionFactory"/>
??</bean>
???
??<bean id="myDaoTxProxy" abstract="true" ref="myTxManager"/>
????<property name="transactionAttributes">
??????<props>
????????<prop key="save*">PROPAGATION_REQUIRED</prop>
????????<prop key="update*">PROPAGATION_REQUIRED</prop>
????????<prop key="delete*">PROPAGATION_REQUIRED</prop>
????????<prop key="*">PROPAGATION_REQUIRED</prop>
??????</props>
????</property>
??</bean>
??
??<bean id="myServiceTxProxy" abstract="true" ref="myTxManager"/>
????<property name="transactionAttributes">
??????<props>
????????<prop key="save*">PROPAGATION_REQUIRED</prop>
????????<prop key="update*">PROPAGATION_REQUIRED</prop>
????????<prop key="delete*">PROPAGATION_REQUIRED</prop>
????????<prop key="find*">PROPAGATION_REQUIRED</prop>
????????<prop key="*">PROPAGATION_REQUIRED</prop>
??????</props>
????</property>
??</bean>
????
??<bean id="lobHandler" parent="myDaoTxProxy">
????<property name="target">
??????<ref bean="userDaoTarget"/>
????</property>
??</bean>??
??
??<!-- *********************** -->
??<!-- ******? Service? ****** -->
??<!-- *********************** -->??
??<!-- 应用service管理
??<bean id="appServiceTarget" parent="myServiceTxProxy">
???? <property name="target">
??????? <ref bean="appServiceTarget"/>
???? </property>
??</bean> -->
??
??<!-- 配置 BeanFactoryAwarebean -->
??<bean id="nativeBean" />??
??????
</beans>

?

四、在需要增加缓存的DAO中修改:

?

public class UserDaoImpl extends BaseDaoImpl<UserInfo> implements IUserDao{

?@SuppressWarnings("unchecked")
?@Override
?public List<UserInfo> queryAllUser() {
??try{
???Query query=this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery("from UserInfo");
???query.setCacheable(true);
???List<UserInfo> list=query.list();
???if(list.size()>0){
????return list;
???}???
??}catch(Exception e){
???e.printStackTrace();
??}
??return null;
?}?

}

热点排行