EhCache 是纯Java实现的简单、快速的Cache组件。EHCache支持内存和磁盘两级缓存,支持LRU、LFU和FIFO多种淘汰算法,支持经过rmi,jgroup,jms实现分布式缓存,能够做为Hibernate的缓存插件。同时它也能提供基于Filter的Cache,提供缓存管理的监听接口,这对业务处理很是有用,且能与Spring良好结合。java
你有几个cache 每一个cache中只存放一种类型的对象,当不一样类型的对象,put/update/expired cache时你须要分别对这不一样类型的对象有一些操做,并且不一样对象,处理方式是彻底不同的。那么你就须要为这每一个cache配置一个单独的监听器类,下面提供一种利用Spring的简单是用的配置方式。算法
下面配置描述了以下场景,配置有两个cache用来缓存卡数据对象和报警数据对象,并分别对两个cache采用单独的监听器实现类,方便实现对事件的不一样响应
一、ehcache.xmlspring
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false" monitoring="off"> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="200" eternal="false" overflowToDisk="true" diskPersistent="true" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LFU" /> <cache name="cardStateCache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="30" > <cacheEventListenerFactory class="com.listener.CacheEventListenerFactory" properties="bean=locationListener" /> </cache> <cache name="alarmRecordCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" > <cacheEventListenerFactory class="com.listener.CacheEventListenerFactory" properties="bean=alarmRecordListener"/> </cache> </ehcache>
二、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: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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" default-lazy-init='true'> <bean name="springContextTool" class="com.helper.SpringContextHelper" lazy-init="false"></bean> <!-- 缓存管理器 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" scope="singleton"> <property name="configLocation"> <value>classpath:ehcache.xml</value> </property> <property name="shared" value="true" /> </bean> <!-- 报警信息缓存 --> <bean id="alarmRecordCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean" > <property name="cacheManager"> <ref local="cacheManager" /> </property> <property name="cacheName"> <value>alarmRecordCache</value> </property> </bean> <!-- 卡状态信息缓存 --> <bean id="cardStateCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean" > <property name="cacheManager"> <ref local="cacheManager" /> </property> <property name="cacheName"> <value>cardStateCache</value> </property> </bean> <!-- 缓存监听器 配置 --> <bean id="alarmRecordListener" class="com.listener.AlarmRecordListener" lazy-init="false" /> <bean id="locationListener" class="com.listener.LocationListener" lazy-init="false" /> </beans>
三、java 代码 架构
public class CacheEventListenerFactory extends net.sf.ehcache.event.CacheEventListenerFactory{ @Override public CacheEventListener createCacheEventListener(Properties properties) { String beanName = properties.getProperty( "bean" ); if ( beanName == null ) { throw new IllegalArgumentException( "缓存监听器名字未定义" ); } return (CacheEventListener) SpringContextHelper.getBean( beanName ); } }
public class LocationListener implements CacheEventListener { //你的监听器实现类,实现ehcache的CacheEventListener接口 }
能够看到,简单的利用Spring的Ioc,就能够很简单、方便的实现上述业务需求、框架
一、OSCache是另一个开源的缓存方案。它同时还支持JSP页面或任意对象的缓存。OSCache功能 强大、灵活,和EHCache同样支持read-only和read/write缓存、支持内存和磁盘缓存。同时,它还提供经过JGroups或JMS进行集群的基本支持。
二、SwarmCache 是一个简单的、基于JavaGroups提供集群的缓存方案。支持read-only和nonstrict read/write缓存。这种缓存适用于读操做远远高于写操做频率的应用。
三、JBoss TreeCache 是一个强大的、可复制(同步或异步)和支持事务的缓存。若是你须要一个真正的支持事务的缓存架构,使用这个方案吧。 异步