ehcache3.0嚐鮮

項目中要用緩存,考慮用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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章