當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Caching集成Ehcache
生活随笔
收集整理的這篇文章主要介紹了
Spring Caching集成Ehcache
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Ehcache可以對頁面、對象、數(shù)據(jù)進(jìn)行緩存,同時(shí)支持集群/分布式緩存。在應(yīng)用中用于常常需要讀取的數(shù)據(jù)交換,而不是通過DB DAO數(shù)據(jù)交換(cache不占用DB寶貴的NIO,直接交換堆內(nèi)存)。
整合Spring、Hibernate也非常的簡單,Spring對Ehcache的支持也非常好。EHCache支持內(nèi)存和磁盤的緩存,支持LRU、LFU和FIFO多種淘汰算法,支持分布式的Cache。
從Spring3.1開始添加了對緩存的支持。
?
Maven的依賴:
<dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.9.0</version></dependency><!-- Optional, to log stuff --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.0.13</version></dependency><!-- Spring caching framework inside this --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.4.RELEASE</version></dependency><!-- Support for Ehcache and others --><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.1.4.RELEASE</version></dependency> </project>?
Gradle的依賴:
apply plugin: 'java' apply plugin: 'eclipse-wtp'version = '1.0'// Uses JDK 7 sourceCompatibility = 1.7 targetCompatibility = 1.7// Get dependencies from Maven central repository repositories {mavenCentral() }//Project dependencies dependencies {compile 'org.springframework:spring-context:4.1.4.RELEASE'compile 'org.springframework:spring-context-support:4.1.4.RELEASE'compile 'net.sf.ehcache:ehcache:2.9.0'compile 'ch.qos.logback:logback-classic:1.0.13' }spring-config-cache.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"><property name="cacheManager" ref="ehcacheManager"/></bean><!--ehcache--><bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache/ehcache.xml"/></bean></beans>ehcache.xml:
<?xml version="1.0" encoding="UTF-8"?> <ehcache name="es"><diskStore path="java.io.tmpdir"/><cache name="code-cache"maxEntriesLocalHeap="2000"eternal="false"timeToIdleSeconds="3600"timeToLiveSeconds="0"overflowToDisk="false"statistics="true"></cache></ehcache>參數(shù)說明:
name:cache的名稱,必填。maxEntriesLocalHeap:在內(nèi)存中的最大保存對象數(shù)量,0則不限,如果填必須小于Integer.MAX_SIZE (2147483647)。maxEntriesLocalDisk:磁盤中保存的數(shù)量,0則不限,一般填0。eternal:true:對象永不過期。false:對象有過期時(shí)限。以下為選填:maxEntriesInCache:只在分布式環(huán)境使用,集群節(jié)點(diǎn)中的最大數(shù)量,0則不限overflowToOffHeap:企業(yè)版Ehcache才有的功能,用于java off-heap(off-heap允許Java直接操作內(nèi)存空間),這樣的目的是為了節(jié)省寶貴的jvm堆,又避免磁盤存儲的低速。maxBytesLocalHeap:定義緩存可以從VM的堆中使用多少字節(jié),如果定義這個(gè)則不能再定義maxEntriesLocalHeap。maxBytesLocalOffHeap:定義緩存可以從OffHeap中使用多少字節(jié)maxBytesLocalDisk:定義緩存可以從磁盤中使用多少OffHeap字節(jié)timeToIdleSeconds:對象空閑過期時(shí)間,以秒為單位,如果是0則不過期。只對eternal為false的有效。timeToLiveSeconds:對象生存時(shí)間,一般為0,為永久生存。只對eternal為false的有效。上邊的兩個(gè)配置容易混淆,區(qū)別:timeToLiveSeconds=x:緩存自創(chuàng)建日期起至失效時(shí)的間隔時(shí)間x;對象空閑時(shí)間,指對象在多長時(shí)間沒有被訪問就會失效。timeToIdleSeconds=y:緩存創(chuàng)建以后,最后一次訪問緩存的日期至失效之時(shí)的時(shí)間間隔y;diskExpiryThreadIntervalSeconds: 對象檢測線程運(yùn)行時(shí)間間隔。標(biāo)識對象狀態(tài)的線程多長時(shí)間運(yùn)行一次。 diskSpoolBufferSizeMB:DiskStore使用的磁盤大小,默認(rèn)值30MB。每個(gè)cache使用各自的DiskStore。memoryStoreEvictionPolicy: 如果內(nèi)存中數(shù)據(jù)超過內(nèi)存限制,向磁盤緩存時(shí)的策略。默認(rèn)值LRU,可選FIFO、LFU。clearOnFlush:flush()的時(shí)候清除內(nèi)存。persistence sub-element. 持久化策略,這是子屬性,一般不用設(shè)置。* localRestartable - 可以重用的緩存,持久化在磁盤,只有企業(yè)版才有這個(gè)功能.* localTempSwap - 當(dāng)(on-heap and/or off-heap)滿的時(shí)候保存到磁盤,但并不重用持久化,即進(jìn)程結(jié)束則緩存全部清除。* none - 不持久化?
使用
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.stereotype.Service;......private Cache cache;......public void addObjToCache(Object key, Object value) {cache.put(key, value);}public String getValueByCache(String key) {return (String)cache.get(key).get();}......?
轉(zhuǎn)載于:https://www.cnblogs.com/starcrm/p/7168765.html
總結(jié)
以上是生活随笔為你收集整理的Spring Caching集成Ehcache的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 纯java生成excel
- 下一篇: TCP/IP详解学习笔记(2)-数据链路