Ehcache的配置(自学,有问题请指出)

最近在看Ehcache,刚开始看的时候无从下手,晚上介绍的文章有很多,但是我们实际用到的很少,本次用缓存关键是加快速度,对于那些非即时返回的数据的查询和存储进行缓存处理。本次是用于接口项目中,进行了监控和缓存。

ehcache缓存空间的建立主要是在spring配置中实现,配置文件在晚上有很多

spring下的配置

<?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:p="http://www.springframework.org/schema/p"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
	
	<bean id="testServiceTarget" class="com.intime.test.TestServiceImpl"/> 
	
	<!-- 缓存注解声明,使用注解缓存 -->
	<cache:annotation-driven cache-manager="cacheManager"></cache:annotation-driven>
	<!-- 指定ehcache.xml的位置 -->
	<bean id="cacheManagerFactory"
	class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
	p:configLocation="classpath:/ehcache.xml" />

	<!-- 声明缓存Manager -->
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
	p:cacheManager-ref="cacheManagerFactory" />
</beans>

  这里的缓存指定了ehcache.xml文件的位置(路径可自己定义):ehcache.xml的配置为

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
	<diskStore path="java.io.tmpdir" />
	<defaultCache maxElementsInMemory="50" eternal="false"
		timeToIdleSeconds="30" timeToLiveSeconds="120" overflowToDisk="true" />
	<cache name="interface" maxElementsInMemory="5000" eternal="true"
		timeToIdleSeconds="500" timeToLiveSeconds="500" overflowToDisk="true" />
</ehcache>

 其中这里配置了一个名为"interface"的缓存空间,这个空间的作用为存储临时数据,之后会将这个文件的数据存入数据库,这样做的原因是在监控接口时,不是将监控数据每次存入数据库,而是一段时间去查询一次缓存,将里面的数据放到数据库。因此这个缓存没有自动清理的功能,而是要手动管理(eternal="true"),其余的参数配置为:

        name:Cache的唯一标识 
·           maxElementsInMemory:内存中最大缓存对象数。 
·           maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。 
·           eternal:Element是否永久有效,一但设置了,timeout将不起作用。 
·           overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。 
·           timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 
·           timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。 
·           diskPersistent:是否缓存虚拟机重启期数据。 
·           diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 
·           diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 

  在以后要用到缓存的时候随时能添加新的空间加以控制。  

  java类的写法:这里主要介绍存入缓存和取出缓存数据,本人在网上找了好久也没找到一个完整的写入写出过程,一般写入可以用注解或者调用cache的put方法,但是cache没有get方法,这里附上自己写的一个类的两个方法:

package ××××××××××××××××××;
import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
/**
 * 
 * <b>类名称:</b>WriteCache.<br/>  
 * <b>类描述:</b><br/>  
 * <b>创建者:Zhoupz</b> <br/>
 * <b>创建时间:</b>2014-3-31 上午10:29:42<br/>  
 * <pre>
 * <b>修订记录:</b>
 * 版本	日期		修订人		描述
 * -------------------------------------------------------------
 * 1.0	2014-3-31	Zhoupz  写入缓存
 *
 * </pre>
 */
public class EhCache {
	/**
	 * 
	 * sqlCache:。<br/>  
	 * (存Cache)<br/>    
	 * @since  1.0  
	 * @param data
	 */
	public void setCache(String cacheName, String key, Object value) {
		CacheManager cacheManager = CacheManager.create();
		Cache cache = cacheManager.getCache(cacheName);
		Element element = new Element(key, value);
		cache.put(element);
	}
	/**
	 * 
	 * getCache:。<br/>  
	 * (取Cache)<br/>    
	 * @since  1.0
	 */
	public Object getCache(String cacheName,String key){
		CacheManager cacheManager = CacheManager.create();
		Cache cache = cacheManager.getCache(cacheName);
		List<String> list = cache.getKeys();
		Object object = null;
		for (int i = 0; i < list.size(); i++) {					//遍历缓存中所有数据
			String s = cache.get(list.get(i)).getKey().toString();
			if(s.equals(key)){
				object = cache.get(list.get(i)).getValue();
			}
		}
		return object;
	}
	/**
	 * 
	 * getCache:。<br/>  
	 * (调取缓存数据并清除调取数据---用于非自动清空的缓存)<br/>    
	 * @since  1.0  
	 * @param cacheNames
	 * @return
	 */
	public List<Object> getCache(String cacheName){
		List<Object> result = new ArrayList<Object>();
		CacheManager cacheManager = CacheManager.create();
		Cache cache = cacheManager.getCache(cacheName);
		List<String> list = cache.getKeys();
		for (int i = 0; i < list.size(); i++) {
			result.add(cache.get(list.get(i)).getValue());
			cache.remove(list.get(i));
		}
		return result;
	}
}

  之后要用到的方法都可以在里面添加。暂时只写了部分。这个还要添加。

原文地址:https://www.cnblogs.com/derek1208/p/3638136.html