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

