- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- Java緩存ehcache的使用步驟
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.4</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=automatic, multicastGroupAddress=198.1.1.1, multicastGroupPort=10001, timeToLive=1" /> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="port=10001,socketTimeoutMillis=60000" /> <!-- 磁盤(pán)緩存位置 --> <diskStore path="java.io.tmpdir/anywhere" /> <cache name="oneCache" maxElementsInMemory="1500" eternal="false" timeToIdleSeconds="900" timeToLiveSeconds="900" overflowToDisk="false" memoryStoreEvictionPolicy="LRU"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateRemovals=false"/> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" /> </cache> </ehcache>
FIFO:先進(jìn)先出
LFU:最少被使用,緩存的元素有一個(gè)hit屬性,hit值最小的將會(huì )被清出緩存。
LRU:最近最少使用,緩存的元素有一個(gè)時(shí)間戳,當緩存容量滿(mǎn)了,而又需要騰出地方來(lái)緩存新的元素的時(shí)候,那么現有緩存元素中時(shí)間戳離當前時(shí)間最遠的元素將被清出緩存
<?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-3.0.xsd"> <description>ehcache</description> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehcache"/> </bean> <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:/ehcache.xml"/> </bean> </beans>
<import resource="classpath:/spring-ehcache.xml"/>
使用類(lèi)導入: @Resource private org.springframework.cache.ehcacheEhCacheCacheManager cacheManager; 從獲取cache Cache cache = cacheManager.getCache(“oneCache”); 存入cache cache.put(“key”, “value”); 從cache中獲取 ValueWrapper val = cache.get(“key”); String tempVal = (String)val.get();
到此這篇關(guān)于Java緩存ehcache的使用步驟的文章就介紹到這了,更多相關(guān)ehcache緩存的使用內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站