詳解SpringBoot+Ehcache使用示例
摘要
本文介紹了SpringBoot中配置Ehcache、自定義get/set方式,并實(shí)際使用緩存的過程。
概念
Ehcache是一種Java緩存框架,支持多種緩存策略,如:
- 最近最少使用LRU:當(dāng)緩存達(dá)到最大容量時(shí),會將最近最少使用的元素淘汰。
- 最少最近使用LFU:根據(jù)元素被訪問的頻率來淘汰元素,最少被訪問的元素會被優(yōu)先淘汰。
- 先進(jìn)先出FIFO:按元素進(jìn)入緩存的時(shí)間順序淘汰元素,先進(jìn)入的元素會被優(yōu)先淘汰。
內(nèi)存與磁盤持久化存儲:
Ehcache支持將數(shù)據(jù)存儲在內(nèi)存中,以實(shí)現(xiàn)快速訪問。當(dāng)內(nèi)存不足時(shí),Ehcache可以將數(shù)據(jù)交換到磁盤,確保緩存數(shù)據(jù)不會因?yàn)閮?nèi)存限制而丟失。磁盤存儲路徑可以通過配置靈活指定,即使在系統(tǒng)重啟后也能恢復(fù)緩存數(shù)據(jù)。提供多種持久化策略,包括本地臨時(shí)交換localTempSwap和自定義持久化實(shí)現(xiàn)。
配置靈活性:
可通過配置ehcache.xml文件,放在啟動(dòng)類資源目錄里,可自定義緩存策略,LRU,diskExpiryThreadIntervalSeconds。
編碼示例
引入依賴:
pom.xml指定ehcache坐標(biāo)并排除slf4j
<!-- Ehcache 坐標(biāo) -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.9.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
配置ehcache.xml文件:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir/ehcache"/> <!-- 定義磁盤存儲路徑,將緩存數(shù)據(jù)存儲在臨時(shí)目錄下 -->
<!--
參數(shù)說明:
maxElementsInMemory 內(nèi)存中最多存儲的元素?cái)?shù)量
eternal 是否為永生緩存,false表示元素有生存和閑置時(shí)間
timeToIdleSeconds 元素閑置 120 秒后失效
timeToLiveSeconds 元素存活 120 秒后失效
maxElementsOnDisk 磁盤上最多存儲的元素?cái)?shù)量
diskExpiryThreadIntervalSeconds 磁盤清理線程運(yùn)行間隔
memoryStoreEvictionPolic 內(nèi)存存儲的淘汰策略,采用 LRU(最近最少使用)
-->
<!-- defaultCache:echcache 的默認(rèn)緩存策略-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- 自定義緩存策略 這里的name是用于緩存名指定時(shí) 需要對應(yīng)緩存名添加-->
<cache name="cacheName"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
配置文件:
application.yml指定spring的緩存文件路徑
spring:
cache:
type: ehcache
ehcache:
config: classpath:ehcache.xml
自定義緩存get/set方式:
利用CacheManager處理緩存:
package org.coffeebeans.ehcache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* <li>ClassName: EhCacheService </li>
* <li>Author: OakWang </li>
*/
@Service
public class EhCacheService {
@Autowired
private CacheManager cacheManager;
/**
* 根據(jù)名稱獲取緩存數(shù)據(jù)
* @param cacheName cache名稱
* @param keyName cache中對應(yīng)key名稱
* @return cache數(shù)據(jù)
*/
public List<?> getCacheData(String cacheName, String keyName){
Cache cache = cacheManager.getCache(cacheName);
if (!Objects.isNull(cache)) {
Element element = cache.get(keyName);
if (!Objects.isNull(element)) {
return (List<?>) element.getObjectValue();
}
}
return new ArrayList<>();
}
/**
* 往緩存中存放數(shù)據(jù) 名字區(qū)分
* @param cacheName cache名稱
* @param keyName cache中對應(yīng)key名稱
* @param dataList 存放數(shù)據(jù)
*/
public void putCacheData(String cacheName, String keyName, List<?> dataList){
Cache cache = cacheManager.getCache(cacheName);
if (Objects.isNull(cache)){
cache = new Cache(cacheName,1000,true,false,0,0);
}
Element newElement = new Element(keyName, dataList);
cache.put(newElement);
}
}
啟動(dòng)類加注解:
@EnableCaching開啟程序緩存
package org.coffeebeans;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
/**
* <li>ClassName: EhCacheApplication </li>
* <li>Author: OakWang </li>
*/
@Slf4j
@SpringBootApplication
@EnableCaching//開啟程序緩存
public class EhCacheApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(EhCacheApplication.class);
springApplication.run(args);
log.info("EhCacheApplication start success!");
}
}
編輯測試類:
package org.coffeebeans.ehcache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
/**
* <li>ClassName: TestEhCache </li>
* <li>Author: OakWang </li>
*/
@Service
public class TestEhCache {
@Autowired
private EhCacheService ehCacheService;
public void testEhCache() {
ehCacheService.putCacheData("cacheName", "keyName", Collections.singletonList("value"));
List<?> cacheData = ehCacheService.getCacheData("cacheName", "keyName");
System.out.println("獲取緩存數(shù)據(jù):" + cacheData.toString());
}
}
測試結(jié)果:

總結(jié)
以上我們了解了SpringBoot中配置Ehcache、自定義get/set方式,并實(shí)際使用緩存的過程。
到此這篇關(guān)于詳解SpringBoot+Ehcache使用示例的文章就介紹到這了,更多相關(guān)SpringBoot+Ehcache使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot集成Ehcache3實(shí)現(xiàn)本地緩存的配置方法
- SpringBoot 使用 Ehcache 作為緩存的操作方法
- springboot整合單機(jī)緩存ehcache的實(shí)現(xiàn)
- SpringBoot淺析緩存機(jī)制之Ehcache?2.x應(yīng)用
- SpringBoot緩存Ehcache的使用詳解
- SpringBoot整合Ehcache3的實(shí)現(xiàn)步驟
- SpringBoot中使用Ehcache的詳細(xì)教程
- SpringBoot2 整合Ehcache組件,輕量級緩存管理的原理解析
- SpringBoot手動(dòng)使用EhCache的方法示例
相關(guān)文章
Java?Cookie與Session實(shí)現(xiàn)會話跟蹤詳解
session的工作原理和cookie非常類似,在cookie中存放一個(gè)sessionID,真實(shí)的數(shù)據(jù)存放在服務(wù)器端,客戶端每次發(fā)送請求的時(shí)候帶上sessionID,服務(wù)端根據(jù)sessionID進(jìn)行數(shù)據(jù)的響應(yīng)2022-11-11
解決@CachePut設(shè)置的key值無法與@CacheValue的值匹配問題
這篇文章主要介紹了解決@CachePut設(shè)置的key的值無法與@CacheValue的值匹配問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Struts2實(shí)現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)
這篇文章主要介紹了Struts2實(shí)現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)的相關(guān)資料,需要的朋友可以參考下2016-06-06
spring項(xiàng)目如何配置多數(shù)據(jù)源(已上生產(chǎn),親測有效)
這篇文章主要介紹了spring項(xiàng)目如何配置多數(shù)據(jù)源(已上生產(chǎn),親測有效),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
詳解Java中NullPointerException的處理方法
這篇文章將帶大家來單獨(dú)看一個(gè)很常見的異常--空指針異常,這個(gè)可以說是每個(gè)Java程序員都必知的異常,所以我們不得不單獨(dú)學(xué)習(xí)一下,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-08-08
mybatis-parameterType傳入map條件方式
這篇文章主要介紹了mybatis-parameterType傳入map條件方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
java.exe和javaw.exe的區(qū)別及使用方法
這篇文章主要介紹了java.exe和javaw.exe的區(qū)別及使用方法,需要的朋友可以參考下2014-04-04
springboot如何查找配置文件路徑的順序和其優(yōu)先級別
此文是在工作中遇到的關(guān)于springboot配置文件的問題,在網(wǎng)上查閱資料和自己測試之后記錄的,以便日后查閱。希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

