您的位置:首页 > 其它

ehcache3.0尝鲜

2016-04-19 15:32 579 查看
项目中要用缓存,考虑用ehcache,到官网一看,画风都变了,Logo也换了颜色,原来被收购了

最新的版本为3.0,直接maven引入

<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.0.0</version>
</dependency>

groupId已经变了,之前是net.sf.ehcache,整个包也发生了变化,API更是巨变,如果和spring3.x结合的话,就不能使用spring自带的那些FactoryBean了

配置文件也变了,一个简单的可用的配置文件如下:

<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/xml/ehcache-core-3.0.xsd"> 
<cache-template name="myDefaults">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<heap unit="entries">200</heap>
</cache-template>

<cache alias="bar" uses-template="myDefaults">
<key-type>java.lang.Number</key-type>
</cache>

<cache alias="simpleCache" uses-template="myDefaults" />

<cache alias="myCache">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<expiry>
<ttl unit="seconds">20</ttl>
</expiry>
<heap unit="entries">2</heap>
</cache>

</config>

简单的代码模板如下:

CacheManager cacheManager
= CacheManagerBuilder.newCacheManagerBuilder()
.withCache("preConfigured",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)))
.build();
cacheManager.init();

Cache<Long, String> preConfigured =
cacheManager.getCache("preConfigured", Long.class, String.class);

Cache<Long, String> myCache = cacheManager.createCache("myCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)).build());

myCache.put(1L, "da one!");
String value = myCache.get(1L);

cacheManager.removeCache("preConfigured");

cacheManager.close();

我们可以限制缓存的大小或者是数目

比较重要的改进就是提供了Off-heap存储,它是相对于堆内存而言的

Off-Heap Store – Limited in size only by available RAM (tested to as much as 6TB on a single machine!). Not subject
to Java garbage collection (GC). Is quite fast, yet slower than the On-Heap Store because data must be moved off and
on the JVM’s heap as it is stored and re-accessed.
Off-heap只受限于机器的物理内存,并且不会受到GC的影响,它很快,比磁盘要快多了,但是要比堆内存慢一点,存储的对象需要被序列化和反序列化
通过下面的参数来设置Off-heap的最大大小

-XX:MaxDirectMemorySize
所以这时我们有了3种存储方案:On-heap,Off-heap,disk,我们可以同时使用这三种方案,ehcache会对我们的数据重排序,并且使用分层的方式来存储,即最热的数据(也就是经常被访问的数据)会被存储在最快的层,而慢一点的数据会被移动到慢一点的层
至于eviction策略,貌似不能像2那样指定FIFO,LRU,LFU这种,它会自己管理eviction,而这种evict是不确定的,所以你会发现回下代码每次的结果可能都不同

Cache ce = myCacheManager.getCache("cache", Long.class, String.class);
ce.put(1L, "a");
ce.put(2L, "b");
ce.put(3L, "c");
System.out.println(ce.get(1L)); // a
System.out.println(ce.get(2L));// b
System.out.println(ce.get(3L));// c
配置的是永不过期,容量为2,你会发现a,b,c三处每一处都有可能取得null,所以这就需要我们在取得null时要进一步的处理
ehchache还提供了对javax.cache,也就是JSR-107的实现

好了,说了这么多废话,结论就是:不好用,还是用2好了,使用2的最新的版本

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.1</version>
<type>pom</type>
</dependency>

和spring结合,需要

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>

添加ehcache.xml

applicationContext.xml中添加

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>

<bean id="MyCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="cacheManager" />
<property name="cacheName" value="MyCache" />
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ehcache 3.0