Spring与Ehcache简单自定义监听器配置
2013-11-14 10:58
232 查看
一、简介
EhCache 是纯Java实现的简单、快速的Cache组件。EHCache支持内存和磁盘两级缓存,支持LRU、LFU和FIFO多种淘汰算法,支持通过rmi,jgroup,jms实现分布式缓存,可以作为Hibernate的缓存插件。同时它也能提供基于Filter的Cache,提供缓存管理的监听接口,这对业务处理非常有用,且能与Spring良好结合。二、应用场景
你有几个cache 每个cache中只存放一种类型的对象,当不同类型的对象,put/update/expired cache时你需要分别对这不同类型的对象有一些操作,而且不同对象,处理方式是完全不一样的。那么你就需要为这每个cache配置一个单独的监听器类,下面提供一种利用Spring的简单是用的配置方式。三、简单实现方式
下面配置描述了如下场景,配置有两个cache用来缓存卡数据对象和报警数据对象,并分别对两个cache采用单独的监听器实现类,方便实现对事件的不同响应1、ehcache.xml
<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>
2、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>
3、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,就可以很简单、方便的实现上述业务需求、
四、其他主流缓存框架一览
1、OSCache是另外一个开源的缓存方案。它同时还支持JSP页面或任意对象的缓存。OSCache功能 强大、灵活,和EHCache一样支持read-only和read/write缓存、支持内存和磁盘缓存。同时,它还提供通过JGroups或JMS进行集群的基本支持。2、SwarmCache 是一个简单的、基于JavaGroups提供集群的缓存方案。支持read-only和nonstrict read/write缓存。这种缓存适用于读操作远远高于写操作频率的应用。
3、JBoss TreeCache 是一个强大的、可复制(同步或异步)和支持事务的缓存。如果你需要一个真正的支持事务的缓存架构,使用这个方案吧。
相关文章推荐
- Spring与Ehcache简单自定义监听器配置
- Spring与Ehcache简单自定义监听器配置
- Spring与Ehcache简单自定义监听器配置
- Spring 自定义注解,配置简单日志注解
- Spring自定义缓存管理及配置Ehcache缓存
- Spring自定义缓存管理及配置Ehcache缓存
- Spring 自定义注解,配置简单日志注解
- Spring Web Flow 简单实现-自定义配置文件位置
- Spring自定义缓存管理及配置Ehcache缓存
- Spring 自定义注解,配置简单日志注解
- Springboot 之 自定义配置文件及读取配置文件
- Spring 配置log4j和简单介绍Log4J的使用
- Spring关于使用注解@Configuration去配置FormattingConversionServiceFactoryBean来实现自定义格式字符串处理无效的问题(未找到是什么原因造成的)
- Struts2,Hibernate,Spring自定义配置文件路径
- Maven+SpringMVC+Dubbo 简单的入门demo配置
- Spring dbcp连接池简单配置 示例
- 实际springMVC项目中自定义异常、spring事务与异常的简单应用
- 最新版的SSH框整合(Spring 3.1.1 + Struts 2.3.1.2 + BoneCP + Hibernate 4.1)更新增加C3P0和EhCache配置
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置