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

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

缓存技术之Ehcache(5)对象缓存_第1张图片


公共配置信息

#application configs
#jdbc c3p0 config
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/work?useUnicode=true&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




Spring配置信息



	
	
	
	
	


Hibernate配置信息




	
	
		
		
		
		

		
		
		
		
		
		
		
		
		
		
		
		
	

	
	
		
		
		
		
			
				${hibernate.dialect}
				${hibernate.show_sql}
				${hibernate.format_sql}
				${hibernate.hbm2ddl.auto}

				
				${hibernate.cache.use_second_level_cache}
				${hibernate.cache.use_query_cache}
				${hibernate.cache.region.factory_class}
				${hibernate.cache.provider_configuration_file_resource_path}
				
			
		
		
		
	

	
	
		
	

	
	
		
		
			
			
			

			
			
			
			

			
			

		
	

	
	
		
		
		
		
	


DAO层的调用

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 findAll() {
		List 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();

	}

}


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

缓存技术之Ehcache(5)对象缓存_第2张图片

测试

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

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


第一次查询时

缓存技术之Ehcache(5)对象缓存_第3张图片

第二次查询时

缓存技术之Ehcache(5)对象缓存_第4张图片

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

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

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

你可能感兴趣的:(缓存技术)