Java緩存ehcache的使用步驟
一、pom.xml
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.4</version> </dependency>
二、編寫ehcache.xml
<?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" />
<!-- 磁盤緩存位置 -->
<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>
三、參數(shù)簡介
| maxElementsInMemory | 緩存中允許創(chuàng)建的最大對象數(shù) |
| eternal | 緩存中對象是否為永久的,如果是,超時設(shè)置將被忽略,對象從不過期。 |
| timeToIdleSeconds | 緩存數(shù)據(jù)空閑的最大時間,也就是說如果有一個緩存有多久沒有被訪問就會被銷毀, 如果該值是 0 就意味著元素可以停頓無窮長的時間。 |
| timeToLiveSeconds | 緩存數(shù)據(jù)存活的時間,緩存對象最大的的存活時間,超過這個時間就會被銷毀, 這只能在元素不是永久駐留時有效,如果該值是0就意味著元素可以停頓無窮長的時間。 |
| overflowToDisk | 內(nèi)存不足時,是否啟用磁盤緩存。 |
| memoryStoreEvictionPolicy | 緩存滿了之后的淘汰算法。 |
| peerDiscovery | 方式:atutomatic 為自動 ;manual 手動 |
| mulicastGroupAddress | 廣播組地址:192.1.1.1 |
| mulicastGroupPort | 廣播組端口:10001; |
| timeToLive | 是指搜索范圍:0是同一臺服務(wù)器,1是同一個子網(wǎng),32是指同一站點,64是指同一塊地域,128是同一塊大陸; |
| hostName | 主機名或者ip,用來接受或者發(fā)送信息的接口 |
四、Ehcache的緩存數(shù)據(jù)淘汰策略
FIFO:先進先出
LFU:最少被使用,緩存的元素有一個hit屬性,hit值最小的將會被清出緩存。
LRU:最近最少使用,緩存的元素有一個時間戳,當(dāng)緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那么現(xiàn)有緩存元素中時間戳離當(dāng)前時間最遠的元素將被清出緩存
五、編寫spring-ehcache.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-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>
六、與Spring整合,導(dǎo)入到spring配置文件
<import resource="classpath:/spring-ehcache.xml"/>
七、Java Source code
使用類導(dǎo)入: @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緩存的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在Mybatis中使用自定義緩存ehcache的方法
- SpringBoot2 整合Ehcache組件,輕量級緩存管理的原理解析
- Spring Boot集成Ehcache緩存解決方式
- SpringBoot中Shiro緩存使用Redis、Ehcache的方法
- 使用ehcache三步搞定springboot緩存的方法示例
- 詳解Spring Boot Oauth2緩存UserDetails到Ehcache
- spring-boot整合ehcache實現(xiàn)緩存機制的方法
- Spring Boot緩存實戰(zhàn) EhCache示例
- Java Ehcache緩存框架入門級使用實例
- 詳解SpringBoot緩存的實例代碼(EhCache 2.x 篇)
- Spring+EHcache緩存實例詳解
- 詳解Spring MVC 集成EHCache緩存
相關(guān)文章
SpringBoot+EasyPoi實現(xiàn)excel導(dǎo)出功能
最新小編遇到這樣一個需求,根據(jù)檢索條件查詢列表并將結(jié)果導(dǎo)出到excel,實現(xiàn)過程也非常簡單,感興趣的朋友跟隨小編一起看看吧2021-09-09
java實現(xiàn)裝飾器模式(Decorator Pattern)
這篇文章主要為大家詳細介紹了java實現(xiàn)裝飾器模式Decorator Pattern,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
Springmvc ResponseBody響應(yīng)json數(shù)據(jù)實現(xiàn)過程
這篇文章主要介紹了Springmvc ResponseBody響應(yīng)json數(shù)據(jù)實現(xiàn)過程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
一文帶你搞懂Java中Synchronized和Lock的原理與使用
這篇文章主要為大家詳細介紹了Java中Synchronized和Lock的原理與使用,文中的示例代碼講解詳細,對我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2023-04-04

