缓存技术之Ehcache(5)对象缓存

来源:互联网 发布:hbuilder下载 mac 编辑:程序博客网 时间:2024/04/30 14:35

因为ehcache是hibernate默认的缓存,所以现在从hibernate对ehcache开始看。后面会贴出一下截图,下面是我工程结构图:


公共配置信息

<span style="font-family:FangSong_GB2312;font-size:14px;">#application configs#jdbc c3p0 configjdbc.driver = com.mysql.jdbc.Driverjdbc.url = jdbc:mysql://localhost:3306/work?useUnicode=true&amp;characterEncoding=utf-8jdbc.username = mysqljdbc.password = mysql#hibernate confighibernate.dialect = org.hibernate.dialect.MySQLDialecthibernate.show_sql = truehibernate.format_sql = falsehibernate.hbm2ddl.auto = updatehibernate.cache.use_second_level_cache = truehibernate.cache.use_query_cache = truehibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactoryhibernate.cache.provider_configuration_file_resource_path = ehcache.xml</span>

Spring配置信息

<span style="font-family:FangSong_GB2312;font-size:14px;"><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><!-- 加载配置文件 --><context:property-placeholder location="classpath:config.properties"/><!-- 扫描service自动注入为bean --><context:component-scan base-package="com.ehcache.service.impl,com.ehcache.dao.impl" /></span>


Hibernate配置信息

<span style="font-family:FangSong_GB2312;font-size:14px;"><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"><!-- 配置数据源 c3p0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><property name="driverClass" value="${jdbc.driver}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><!-- 请求超时时间 --><property name="checkoutTimeout" value="30000" /><!-- 每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 --><property name="idleConnectionTestPeriod" value="30" /><!-- 连接数据库连接池最大空闲时间 --><property name="maxIdleTime" value="30" /><!-- 连接池初始化连接数 --><property name="initialPoolSize" value="5" /><property name="minPoolSize" value="5" /><property name="maxPoolSize" value="20" /><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 --><property name="acquireIncrement" value="5" /></bean><!-- 配置hibernate的SessionFactory --><bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!-- 注入数据源 相关信息看源码 --><property name="dataSource" ref="dataSource" /><!-- hibernate配置信息 --><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><!-- 开启二级缓存 ehcache --><prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop><prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop><prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop><prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.cache.provider_configuration_file_resource_path}</prop></props></property><!-- 扫描hibernate注解配置的entity --><property name="packagesToScan" value="com.ehcache.pojo" /></bean><!-- 配置事务管理器 --><bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置事务增强处理Bean,指定事务管理器 --><tx:advice id="transactionAdvice" transaction-manager="transactionManager"><!-- 配置详细事务处理语义 --><tx:attributes><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="get*" propagation="SUPPORTS" read-only="true" /><tx:method name="find*" propagation="SUPPORTS" read-only="true" /><tx:method name="select*" propagation="SUPPORTS" read-only="true" /><tx:method name="load*" propagation="SUPPORTS" read-only="true" /><!-- 其他采用默认事务方式 --><tx:method name="*" /></tx:attributes></tx:advice><!-- Spring aop事务管理 --><aop:config><!-- 配置切入点 --><aop:pointcut id="transactionPointcut"expression="execution(* com.ehcache.service..*Impl.*(..))" /><!-- 指定在txAdvice切入点应用txAdvice事务增强处理 --><aop:advisor pointcut-ref="transactionPointcut"advice-ref="transactionAdvice" /></aop:config></beans></span>

DAO层的调用

<span style="font-family:FangSong_GB2312;font-size:14px;">package com.ehcache.dao.impl;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import com.ehcache.dao.UserDao;import com.ehcache.pojo.AcctUser;@Repository("userDao")public class UserDaoImpl implements UserDao {@Autowiredprivate SessionFactory sessionFactory;private Session getCurrentSession() {return this.sessionFactory.getCurrentSession();}@Overridepublic AcctUser load(String id) {return (AcctUser) this.getCurrentSession().load(AcctUser.class, id);}@Overridepublic AcctUser get(String id) {return (AcctUser) this.getCurrentSession().get(AcctUser.class, id);}@SuppressWarnings("unchecked")@Overridepublic List<AcctUser> findAll() {List<AcctUser> acctUsers = this.getCurrentSession().createQuery("from AcctUser").setCacheable(true).list();return acctUsers;}@Overridepublic void persist(AcctUser entity) {this.getCurrentSession().persist(entity);}@Overridepublic String save(AcctUser entity) {return (String) this.getCurrentSession().save(entity);}@Overridepublic void saveOrUpdate(AcctUser entity) {this.getCurrentSession().saveOrUpdate(entity);}@Overridepublic void delete(String id) {AcctUser entity = this.load(id);this.getCurrentSession().delete(entity);}@Overridepublic void flush() {this.getCurrentSession().flush();}}</span>


缓存存放(配置文件中指定c:\ehcache目录)


测试

测试前在加一段测试代码(查询要走的方法):

@SuppressWarnings("unchecked")@Overridepublic List<AcctUser> findAll() {System.out.println("查询开始时间==============="+System.currentTimeMillis());List<AcctUser> acctUsers = this.getCurrentSession().createQuery("from AcctUser").setCacheable(true).list();System.out.println("查询结束时间==============="+System.currentTimeMillis());return acctUsers;}


第一次查询时


第二次查询时


很明显,除了第一次之外的所有查询不再执行sql

源码下载:http://pan.baidu.com/s/1eSJQVLK

参考:http://www.blogjava.net/hoojo/archive/2012/07/12/382860.html

0 0
原创粉丝点击