一、简介
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<
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配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<?
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 代码
1
2
3
4
5
6
7
8
9
10
11
|
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 );
}
}
|
1
2
3
|
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 是一个强大的、可复制(同步或异步)和支持事务的缓存。如果你需要一个真正的支持事务的缓存架构,使用这个方案吧。