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)文章
怎樣使用PowerMockito 測(cè)試靜態(tài)方法
這篇文章主要介紹了使用PowerMockito 測(cè)試靜態(tài)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07如何將Java枚舉名稱作為注解的屬性值實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了如何將Java枚舉名稱作為注解的屬性值實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Java中HttpServletRequestWrapper的使用與原理詳解
這篇文章主要介紹了Java中HttpServletRequestWrapper的使用與原理詳解,HttpServletRequestWrapper 實(shí)現(xiàn)了 HttpServletRequest 接口,可以讓開(kāi)發(fā)人員很方便的改造發(fā)送給 Servlet 的請(qǐng)求,需要的朋友可以參考下2024-01-01消息隊(duì)列 RabbitMQ 與 Spring 整合使用的實(shí)例代碼
本篇文章主要介紹了消息隊(duì)列 RabbitMQ 與 Spring 整合使用的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Character.UnicodeBlock中cjk的說(shuō)明詳解
這篇文章主要為大家詳細(xì)介紹了Character.UnicodeBlock中cjk的說(shuō)明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09