EhCache java缓存框架介绍

EhCache是一个纯Java的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。

Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。

下图是 EhCache 在应用程序中的位置:


主要特性有:
1. 快速、精干;
2. 简单;
3. 多种缓存策略;
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题;
5. 缓存数据会在虚拟机重启的过程中写入磁盘;
6. 可以通过 RMI、可插入 API 等方式进行分布式缓存;
7. 具有缓存和缓存管理器的侦听接口;
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域;
9. 提供 Hibernate 的缓存实现;

下面演示EhCache与spring集成配置。

1.引入依赖jar包

<dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.10.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.1.6.RELEASE</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.5.8</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.5.8</version></dependency>

2.EhCache配置

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"><diskStore path="java.io.tmpdir/ehcache" /><!-- 默认缓存 --><defaultCache maxElementsInMemory="1000" eternal="true"timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"diskPersistent="true" diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"></defaultCache><!-- demo缓存 --><cache name="demoCache" maxElementsInMemory="1000" eternal="false"timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"diskPersistent="false" diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"></cache>
</ehcache>

3.spring配置

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd  http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"default-autowire="byName" default-lazy-init="false"><!-- 引用ehCache的配置 --><bean id="defaultCacheManager"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation"><value>classpath:ehcache.xml</value></property></bean><!-- 定义ehCache的工厂,并设置所使用的Cache name --><bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"><property name="cacheManager"><ref local="defaultCacheManager" /></property><property name="cacheName"><value>demoCache</value></property></bean>
</beans>

4.测试demo

package cn.slimsmart.ehcache;import java.io.BufferedReader;
import java.io.InputStreamReader;import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class EhcacheTest {public static void main(String[] args) throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Cache  ehCache = (Cache)context.getBean("ehCache");ehCache.put(new Element("key", "value"));ehCache.put(new Element("key1", "aaaa", 0, 10));BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));String line=null;while ((line=strin.readLine())!=null && !"exit".equals(line)) {String[] ss = line.split("\\s+");if(ss.length==1){System.out.println(ehCache.get(ss[0]));}else if (ss.length==2){ehCache.put(new Element(ss[0],ss[1]));System.out.println("ok");}}strin.close();}}

参考文章:

1.Ehcache 整合Spring 使用页面、对象缓存 
2.spring+ehCache简单整合使用示例  
3.Ehcache整合spring配置