Springboot集成Ehcache3實(shí)現(xiàn)本地緩存的配置方法
如果只需要在單個(gè)應(yīng)用程序中使用本地緩存,則可以選擇Ehcache;它支持內(nèi)存和磁盤存儲(chǔ),這里不以注解方式演示,通過自己實(shí)現(xiàn)緩存管理者靈活控制緩存的讀寫;
1、引入相關(guān)依賴
<!-- ehcache3集成start --> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.10.8</version> </dependency> <dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> </dependency> <!-- ehcache3集成end -->
2、修改yml配置
spring:
cache:
type: jcache
jcache:
config: classpath:cache/ehcache.xml3、配置ehcache.xml文件
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.ehcache.org/v3"
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd">
<!-- 緩存持久化配置: 定義磁盤緩存位置 -->
<persistence directory="E:/project_home/limit_control/cache/light-element-mybatis"/>
<!-- 緩存模板: 未填寫緩存名時(shí)使用的默認(rèn)緩存,同時(shí)也可被繼承 -->
<cache-template name="defaultCache">
<key-type>java.lang.String</key-type>
<value-type>java.lang.Object</value-type>
<resources>
<heap unit="MB">64</heap>
<offheap unit="MB">128</offheap>
</resources>
</cache-template>
<!-- 緩存列表: 自定義緩存配置 -->
<!-- 不過期 -->
<cache alias="EXPIRE_NONE" uses-template="defaultCache"/>
<!-- 24小時(shí)過期 -->
<cache alias="EXPIRE_24_HOURS" uses-template="defaultCache">
<expiry>
<ttl unit="hours">24</ttl>
</expiry>
</cache>
<!-- 30分鐘過期 -->
<cache alias="EXPIRE_30_MINUTES" uses-template="defaultCache">
<expiry>
<ttl unit="minutes">30</ttl>
</expiry>
</cache>
</config>4、編寫緩存策略枚舉
public enum CacheStrategy {
EXPIRE_30_MINUTES,
EXPIRE_24_HOURS,
EXPIRE_NONE
}5、編寫緩存管理者,來控制緩存的增刪改查
import com.alibaba.fastjson.JSON;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import java.util.List;
/**
* ehcache3緩存管理者
*/
@Configuration
@EnableCaching
public class EhCacheManager {
private static CacheManager cacheManager;
public EhCacheManager(CacheManager cacheManager) {
EhCacheManager.cacheManager = cacheManager;
}
/**
* 獲取默認(rèn)緩存
*
* @return
*/
public static Cache getDefaultCache() {
return getCache("EXPIRE_24_HOURS");
}
/**
* 獲取指定緩存
*
* @param cacheName 緩存名稱
* @return
*/
public static Cache getCache(String cacheName) {
if (cacheManager == null) {
return null;
}
return cacheManager.getCache(cacheName);
}
/**
* 獲取緩存內(nèi)容(對(duì)象)
*
* @param cacheName 緩存名稱
* @param key 鍵
* @param clazz<T> class類型
* @return
*/
public static <T extends Object> T getObjValue(String cacheName, String key, Class<T> clazz) {
Object o = getValue(cacheName, key);
if (o == null) {
return null;
}
T t = (T) JSON.parseObject(JSON.toJSONString(o), clazz);
return t;
}
/**
* 獲取緩存內(nèi)容(集合)
*
* @param cacheName 緩存名稱
* @param key 鍵
* @param clazz<T> class類型
* @return
*/
public static <T extends Object> List<T> getListValue(String cacheName, String key, Class<T> clazz) {
Object o = getValue(cacheName, key);
if (o == null) {
return null;
}
List<T> ts = JSON.parseArray(JSON.toJSONString(o), clazz);
return ts;
}
/**
* 獲取緩存內(nèi)容
*
* @param cacheName
* @param key
* @return
*/
private static Object getValue(String cacheName, String key) {
Cache cache = getCache(cacheName);
if (cache == null && cache.get(key) == null) {
return null;
}
Cache.ValueWrapper valueWrapper = cache.get(key);
if (valueWrapper == null) {
return null;
}
Object o = valueWrapper.get();
if (o == null) {
return null;
}
return o;
}
/**
* 新增或修改緩存數(shù)據(jù)
*
* @param cacheName 緩存名稱
* @param key 鍵
* @param value 值
*/
public static void put(String cacheName, String key, Object value) {
Cache cache = getCache(cacheName);
if (cache == null) {
return;
}
cache.put(key, value);
}
/**
* 刪除緩存數(shù)據(jù)
*
* @param cacheName 緩存名稱
* @param key 鍵
*/
public static void del(String cacheName, String key) {
Cache cache = getCache(cacheName);
if (cache == null) {
return;
}
cache.evict(key);
}
}6、編寫controller進(jìn)行簡(jiǎn)單測(cè)試
import cn.hutool.core.collection.CollectionUtil;
import com.yx.light.element.mybatis.cache.CacheStrategy;
import com.yx.light.element.mybatis.cache.EhCacheManager;
import com.yx.light.element.mybatis.mapper.entity.GroupHeader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping(value = "/index")
@Slf4j
public class IndexController {
@GetMapping(value = "/list")
public List<GroupHeader> list() {
List<GroupHeader> listValue = EhCacheManager.getListValue(CacheStrategy.EXPIRE_30_MINUTES.name(), "list", GroupHeader.class);
if (CollectionUtil.isEmpty(listValue)) {
log.info("集合緩存不存在或已過期,查詢數(shù)據(jù)庫(kù)!");
//模擬查庫(kù)
List<GroupHeader> objects = new ArrayList<>();
for (int i = 0; i < 5; i++) {
GroupHeader groupHeader = new GroupHeader();
groupHeader.setGroupCode("aaaaa-" + i);
groupHeader.setGroupName("多個(gè)對(duì)象" + i);
objects.add(groupHeader);
}
listValue = objects;
EhCacheManager.put(CacheStrategy.EXPIRE_30_MINUTES.name(), "list", listValue);
log.info("集合數(shù)據(jù)加載到緩存!");
} else {
log.info("從集合緩存中直接獲取數(shù)據(jù)!");
}
return listValue;
}
@GetMapping(value = "/one")
public GroupHeader one() {
GroupHeader objValue = EhCacheManager.getObjValue(CacheStrategy.EXPIRE_30_MINUTES.name(), "obj", GroupHeader.class);
if (objValue == null) {
log.info("對(duì)象緩存不存在或已過期,查詢數(shù)據(jù)庫(kù)!");
//模擬查庫(kù)
GroupHeader groupHeader = new GroupHeader();
groupHeader.setGroupCode("aaaaa");
groupHeader.setGroupName("單個(gè)對(duì)象");
objValue = groupHeader;
EhCacheManager.put(CacheStrategy.EXPIRE_30_MINUTES.name(), "obj", groupHeader);
log.info("對(duì)象數(shù)據(jù)加載到緩存!");
} else {
log.info("從對(duì)象緩存中直接獲取數(shù)據(jù)!");
}
return objValue;
}
@GetMapping(value = "/del")
public void del() {
log.info("清理對(duì)象緩存!");
EhCacheManager.del(CacheStrategy.EXPIRE_30_MINUTES.name(), "obj");
}
}7、分別調(diào)用接口查看日志打印

到此這篇關(guān)于Springboot集成Ehcache3實(shí)現(xiàn)本地緩存的文章就介紹到這了,更多相關(guān)Springboot集成Ehcache3內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深度解析Java 21中虛擬線程的工作原理與實(shí)際應(yīng)用
Java 21的發(fā)布標(biāo)志著Java并發(fā)編程的一個(gè)重要里程碑,本文將深入探討虛擬線程的工作原理,優(yōu)勢(shì)以及在實(shí)際項(xiàng)目中的應(yīng)用,文中的示例代碼講解詳細(xì),有需要的小伙伴可以了解下2025-09-09
關(guān)于Spring多數(shù)據(jù)源TransactionManager沖突的解決方案
這篇文章主要介紹了關(guān)于Spring多數(shù)據(jù)源TransactionManager沖突的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
關(guān)于java.math.BigDecimal比較大小問題
這篇文章主要介紹了關(guān)于java.math.BigDecimal比較大小問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

