缓存工具 CacheUtils 增加 computeIfAbsent 简化方法

This commit is contained in:
thinkgem
2025-10-10 11:12:59 +08:00
parent 92d4161294
commit 3dd0bdcd62
6 changed files with 75 additions and 76 deletions

View File

@@ -28,8 +28,7 @@ public class CacheChatMemoryRepository implements ChatMemoryRepository {
@Override
public @NotNull List<Message> findByConversationId(@NotNull String conversationId) {
List<Message> all = CacheUtils.get(CMS_CHAT_MSG_CACHE, conversationId);
return all != null ? all : List.of();
return CacheUtils.computeIfAbsent(CMS_CHAT_MSG_CACHE, conversationId, k -> List.of());
}
@Override

View File

@@ -85,11 +85,7 @@ public class CmsAiChatService extends BaseService {
}
public Map<String, Map<String, Object>> getChatCacheMap() {
Map<String, Map<String, Object>> cache = CacheUtils.get(CMS_CHAT_CACHE, getChatCacheKey());
if (cache == null) {
cache = MapUtils.newHashMap();
}
return cache;
return CacheUtils.computeIfAbsent(CMS_CHAT_CACHE, getChatCacheKey(), k -> MapUtils.newHashMap());
}
/**

View File

@@ -120,8 +120,7 @@ public class ArticleService extends CrudService<ArticleDao, Article> {
public void updateExpiredWeight(Article article) {
// 更新过期的权重间隔为“6”个小时
Date updateExpiredWeightDate = CmsUtils.getCache("updateExpiredWeightDateByArticle");
if (updateExpiredWeightDate == null || (updateExpiredWeightDate != null
&& updateExpiredWeightDate.getTime() < System.currentTimeMillis())) {
if (updateExpiredWeightDate == null || updateExpiredWeightDate.getTime() < System.currentTimeMillis()) {
article.setWeightDate(new Date());
dao.updateExpiredWeight(article);
CmsUtils.putCache("updateExpiredWeightDateByArticle", DateUtils.addHours(new Date(), 6));

View File

@@ -20,10 +20,10 @@ import com.jeesite.modules.cms.service.CategoryService;
import com.jeesite.modules.cms.service.SiteService;
import org.springframework.ui.Model;
import jakarta.servlet.ServletContext;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* CmsUtils
@@ -69,13 +69,8 @@ public class CmsUtils {
* 获得站点列表
*/
public static List<Site> getSiteList() {
@SuppressWarnings("unchecked")
List<Site> siteList = (List<Site>) getCache("siteList");
if (siteList == null) {
siteList = Static.siteService.findList(new Site());
putCache("siteList", siteList);
}
return siteList;
return CmsUtils.computeIfAbsentCache("siteList", k ->
Static.siteService.findList(new Site()));
}
/**
@@ -83,23 +78,18 @@ public class CmsUtils {
* @param siteCode 站点编号
*/
public static List<Category> getMainNavList(String siteCode) {
@SuppressWarnings("unchecked")
List<Category> mainNavList = (List<Category>) getCache("mainNavList_" + siteCode);
if (mainNavList == null) {
return CmsUtils.computeIfAbsentCache("mainNavList_" + siteCode, k -> {
Category category = new Category();
category.setSite(new Site(siteCode));
category.setParent(new Category(Category.ROOT_CODE));
category.setInMenu(Global.SHOW);
mainNavList = Static.categoryService.findList(category);
putCache("mainNavList_" + siteCode, mainNavList);
}
return mainNavList;
return Static.categoryService.findList(category);
});
}
/**
* 获取栏目
* @param categoryCode 栏目编号
* @return
*/
public static Category getCategory(String categoryCode) {
return Static.categoryService.get(categoryCode);
@@ -237,8 +227,6 @@ public class CmsUtils {
/**
* 获得文章动态URL地址
* @param article
* @return url
*/
public static String getUrlDynamic(Article article) {
StringBuilder str = new StringBuilder();
@@ -259,8 +247,6 @@ public class CmsUtils {
/**
* 获得栏目动态URL地址
* @param category
* @return url
*/
public static String getUrlDynamic(Category category) {
StringBuilder str = new StringBuilder();
@@ -279,8 +265,6 @@ public class CmsUtils {
/**
* 获得站点动态URL地址
* @param site
* @return url
*/
public static String getUrlDynamic(Site site) {
StringBuilder str = new StringBuilder();
@@ -299,8 +283,6 @@ public class CmsUtils {
/**
* 获得栏目动态URL地址
* @param category
* @return url
*/
public static String getAdminUrlDynamic(Category category) {
StringBuilder str = new StringBuilder();
@@ -381,8 +363,6 @@ public class CmsUtils {
/**
* 从图片地址中去除ContextPath地址
* @param src
* @return
*/
public static String formatImageSrcToDb(String src) {
if (StringUtils.isBlank(src)) {
@@ -397,8 +377,6 @@ public class CmsUtils {
/**
* 从图片地址中加入ContextPath地址
* @param src
* @return
*/
public static String formatImageSrcToWeb(String src) {
if (StringUtils.isBlank(src)) {
@@ -413,8 +391,6 @@ public class CmsUtils {
/**
* 获取文章视图
* @param article
* @return
*/
public static String getArticleView(Article article) {
if (StringUtils.isBlank(article.getCustomContentView())) {
@@ -441,8 +417,6 @@ public class CmsUtils {
/**
* 视图配置属性设置
* @param model
* @param params
*/
public static void addViewConfigAttribute(Model model, String params) {
if (StringUtils.isNotBlank(params)) {
@@ -458,8 +432,6 @@ public class CmsUtils {
/**
* 视图配置属性设置
* @param model
* @param category
*/
public static void addViewConfigAttribute(Model model, Category category) {
List<Category> categoryList = ListUtils.newArrayList();
@@ -481,31 +453,67 @@ public class CmsUtils {
}
}
/**
* 获取站点服务
*/
public static SiteService getSiteService() {
return Static.siteService;
}
/**
* 获取栏目服务
*/
public static CategoryService getCategoryService() {
return Static.categoryService;
}
/**
* 获取文章服务
*/
public static ArticleService getArticleService() {
return Static.articleService;
}
/**
* 获取缓存值
* @param key 缓存键
*/
public static <V> V getCache(String key) {
return CacheUtils.get(CMS_CACHE, key);
}
/**
* 获取缓存值
* @param key 缓存键
* @param defaultValue 默认值
*/
public static <V> V getCache(String key, V defaultValue) {
V value = CacheUtils.get(CMS_CACHE, key);
return value != null ? value : defaultValue;
}
/**
* 设置缓存值
* @param key 缓存键
* @param value 缓存值
*/
public static void putCache(String key, Object value) {
CacheUtils.put(CMS_CACHE, key, value);
}
/**
* 获取缓存,如果不存在,则设置新值
* @param key 缓存键
* @param mappingFunction 新值
*/
public static <V> V computeIfAbsentCache(String key, Function<String, V> mappingFunction) {
return CacheUtils.computeIfAbsent(CMS_CACHE, key, mappingFunction);
}
/**
* 移除缓存值
* @param key 缓存键
*/
public static void removeCache(String key) {
CacheUtils.remove(CMS_CACHE, key);
}

View File

@@ -4,12 +4,12 @@
*/
package com.jeesite.modules.sys.utils;
import java.util.List;
import com.jeesite.common.utils.SpringUtils;
import com.jeesite.modules.sys.entity.Area;
import com.jeesite.modules.sys.service.AreaService;
import java.util.List;
/**
*
* @author ThinkGem
@@ -29,15 +29,10 @@ public class AreaUtils {
/**
* 获取所有区域列表(系统级别缓存)
* @return
*/
public static List<Area> getAreaAllList(){
List<Area> areaList = SysCacheUtils.get(CACHE_AREA_ALL_LIST);
if (areaList == null){
areaList = Static.areaService.findList(new Area());
SysCacheUtils.put(CACHE_AREA_ALL_LIST, areaList);
}
return areaList;
return SysCacheUtils.computeIfAbsentCache(CACHE_AREA_ALL_LIST, k ->
Static.areaService.findList(new Area()));
}
/**

View File

@@ -107,12 +107,8 @@ public class EmpUtils {
* @author ThinkGem
*/
public static List<EmployeeOffice> getEmployeeOfficeList(){
List<EmployeeOffice> list = UserUtils.getCache(CACHE_EMPLOYEE_OFFICE_LIST);
if (list == null){
list = Static.employeeService.findEmployeeOfficeList(getEmployee());
UserUtils.putCache(CACHE_EMPLOYEE_OFFICE_LIST, list);
}
return list;
return UserUtils.computeIfAbsentCache(CACHE_EMPLOYEE_OFFICE_LIST, k ->
Static.employeeService.findEmployeeOfficeList(getEmployee()));
}
/**
@@ -143,14 +139,19 @@ public class EmpUtils {
* @author ThinkGem
*/
public static List<Office> getOfficeAllList(){
List<Office> officeList = CorpUtils.getCache(CACHE_OFFICE_ALL_LIST);
if (officeList == null){
// List<Office> officeList = CorpUtils.getCache(CACHE_OFFICE_ALL_LIST);
// if (officeList == null){
// Office where = new Office();
// where.setStatus(Office.STATUS_NORMAL);
// officeList = Static.officeService.findList(where);
// CorpUtils.putCache(CACHE_OFFICE_ALL_LIST, officeList);
// }
// return officeList;
return CorpUtils.computeIfAbsentCache(CACHE_OFFICE_ALL_LIST, k -> {
Office where = new Office();
where.setStatus(Office.STATUS_NORMAL);
officeList = Static.officeService.findList(where);
CorpUtils.putCache(CACHE_OFFICE_ALL_LIST, officeList);
}
return officeList;
return Static.officeService.findList(where);
});
}
/**
@@ -304,14 +305,19 @@ public class EmpUtils {
* @author ThinkGem
*/
public static List<Company> getCompanyAllList(){
List<Company> companyList = CorpUtils.getCache(CACHE_COMPANY_ALL_LIST);
if (companyList == null){
// List<Company> companyList = CorpUtils.getCache(CACHE_COMPANY_ALL_LIST);
// if (companyList == null){
// Company where = new Company();
// where.setStatus(Office.STATUS_NORMAL);
// companyList = Static.companyService.findList(where);
// CorpUtils.putCache(CACHE_COMPANY_ALL_LIST, companyList);
// }
// return companyList;
return CorpUtils.computeIfAbsentCache(CACHE_COMPANY_ALL_LIST, k -> {
Company where = new Company();
where.setStatus(Office.STATUS_NORMAL);
companyList = Static.companyService.findList(where);
CorpUtils.putCache(CACHE_COMPANY_ALL_LIST, companyList);
}
return companyList;
return Static.companyService.findList(where);
});
}
/**
@@ -351,12 +357,8 @@ public class EmpUtils {
* @return
*/
public static List<EmployeePost> getEmployeePostList(){
List<EmployeePost> list = UserUtils.getCache(CACHE_EMPLOYEE_POST_LIST);
if (list == null){
list = getEmployeePostList(getEmployee().getEmpCode());
UserUtils.putCache(CACHE_EMPLOYEE_POST_LIST, list);
}
return list;
return UserUtils.computeIfAbsentCache(CACHE_EMPLOYEE_POST_LIST, k ->
getEmployeePostList(getEmployee().getEmpCode()));
}
/**