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

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


公共配置信息

<span style="font-family:FangSong_GB2312;font-size:14px;">#application configs
#jdbc c3p0 config
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/work?useUnicode=true&amp;characterEncoding=utf-8
jdbc.username = mysql
jdbc.password = mysql


#hibernate config
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = false
hibernate.hbm2ddl.auto = update
hibernate.cache.use_second_level_cache = true
hibernate.cache.use_query_cache = true
hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory
hibernate.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/beans
			http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
			http://www.springframework.org/schema/context
			http://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.xsd
		http://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 {

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return this.sessionFactory.getCurrentSession();
	}

	@Override
	public AcctUser load(String id) {
		return (AcctUser) this.getCurrentSession().load(AcctUser.class, id);
	}
	
	@Override
	public AcctUser get(String id) {
		return (AcctUser) this.getCurrentSession().get(AcctUser.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AcctUser> findAll() {
		List<AcctUser> acctUsers = this.getCurrentSession().createQuery("from AcctUser").setCacheable(true).list();
		return acctUsers;
	}

	@Override
	public void persist(AcctUser entity) {
		this.getCurrentSession().persist(entity);

	}

	@Override
	public String save(AcctUser entity) {
		return (String) this.getCurrentSession().save(entity);
	}

	@Override
	public void saveOrUpdate(AcctUser entity) {
		this.getCurrentSession().saveOrUpdate(entity);
	}

	@Override
	public void delete(String id) {
		AcctUser entity = this.load(id);
		this.getCurrentSession().delete(entity);
	}

	@Override
	public void flush() {
		this.getCurrentSession().flush();

	}

}
</span>


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


测试

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

	@SuppressWarnings("unchecked")
	@Override
	public 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值