缓存工具 CacheUtils 增加 computeIfAbsent 简化方法
This commit is contained in:
@@ -28,8 +28,7 @@ public class CacheChatMemoryRepository implements ChatMemoryRepository {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull List<Message> findByConversationId(@NotNull String conversationId) {
|
public @NotNull List<Message> findByConversationId(@NotNull String conversationId) {
|
||||||
List<Message> all = CacheUtils.get(CMS_CHAT_MSG_CACHE, conversationId);
|
return CacheUtils.computeIfAbsent(CMS_CHAT_MSG_CACHE, conversationId, k -> List.of());
|
||||||
return all != null ? all : List.of();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -85,11 +85,7 @@ public class CmsAiChatService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Map<String, Object>> getChatCacheMap() {
|
public Map<String, Map<String, Object>> getChatCacheMap() {
|
||||||
Map<String, Map<String, Object>> cache = CacheUtils.get(CMS_CHAT_CACHE, getChatCacheKey());
|
return CacheUtils.computeIfAbsent(CMS_CHAT_CACHE, getChatCacheKey(), k -> MapUtils.newHashMap());
|
||||||
if (cache == null) {
|
|
||||||
cache = MapUtils.newHashMap();
|
|
||||||
}
|
|
||||||
return cache;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -120,8 +120,7 @@ public class ArticleService extends CrudService<ArticleDao, Article> {
|
|||||||
public void updateExpiredWeight(Article article) {
|
public void updateExpiredWeight(Article article) {
|
||||||
// 更新过期的权重,间隔为“6”个小时
|
// 更新过期的权重,间隔为“6”个小时
|
||||||
Date updateExpiredWeightDate = CmsUtils.getCache("updateExpiredWeightDateByArticle");
|
Date updateExpiredWeightDate = CmsUtils.getCache("updateExpiredWeightDateByArticle");
|
||||||
if (updateExpiredWeightDate == null || (updateExpiredWeightDate != null
|
if (updateExpiredWeightDate == null || updateExpiredWeightDate.getTime() < System.currentTimeMillis()) {
|
||||||
&& updateExpiredWeightDate.getTime() < System.currentTimeMillis())) {
|
|
||||||
article.setWeightDate(new Date());
|
article.setWeightDate(new Date());
|
||||||
dao.updateExpiredWeight(article);
|
dao.updateExpiredWeight(article);
|
||||||
CmsUtils.putCache("updateExpiredWeightDateByArticle", DateUtils.addHours(new Date(), 6));
|
CmsUtils.putCache("updateExpiredWeightDateByArticle", DateUtils.addHours(new Date(), 6));
|
||||||
|
|||||||
@@ -20,10 +20,10 @@ import com.jeesite.modules.cms.service.CategoryService;
|
|||||||
import com.jeesite.modules.cms.service.SiteService;
|
import com.jeesite.modules.cms.service.SiteService;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
|
||||||
import jakarta.servlet.ServletContext;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CmsUtils
|
* CmsUtils
|
||||||
@@ -69,13 +69,8 @@ public class CmsUtils {
|
|||||||
* 获得站点列表
|
* 获得站点列表
|
||||||
*/
|
*/
|
||||||
public static List<Site> getSiteList() {
|
public static List<Site> getSiteList() {
|
||||||
@SuppressWarnings("unchecked")
|
return CmsUtils.computeIfAbsentCache("siteList", k ->
|
||||||
List<Site> siteList = (List<Site>) getCache("siteList");
|
Static.siteService.findList(new Site()));
|
||||||
if (siteList == null) {
|
|
||||||
siteList = Static.siteService.findList(new Site());
|
|
||||||
putCache("siteList", siteList);
|
|
||||||
}
|
|
||||||
return siteList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,23 +78,18 @@ public class CmsUtils {
|
|||||||
* @param siteCode 站点编号
|
* @param siteCode 站点编号
|
||||||
*/
|
*/
|
||||||
public static List<Category> getMainNavList(String siteCode) {
|
public static List<Category> getMainNavList(String siteCode) {
|
||||||
@SuppressWarnings("unchecked")
|
return CmsUtils.computeIfAbsentCache("mainNavList_" + siteCode, k -> {
|
||||||
List<Category> mainNavList = (List<Category>) getCache("mainNavList_" + siteCode);
|
|
||||||
if (mainNavList == null) {
|
|
||||||
Category category = new Category();
|
Category category = new Category();
|
||||||
category.setSite(new Site(siteCode));
|
category.setSite(new Site(siteCode));
|
||||||
category.setParent(new Category(Category.ROOT_CODE));
|
category.setParent(new Category(Category.ROOT_CODE));
|
||||||
category.setInMenu(Global.SHOW);
|
category.setInMenu(Global.SHOW);
|
||||||
mainNavList = Static.categoryService.findList(category);
|
return Static.categoryService.findList(category);
|
||||||
putCache("mainNavList_" + siteCode, mainNavList);
|
});
|
||||||
}
|
|
||||||
return mainNavList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取栏目
|
* 获取栏目
|
||||||
* @param categoryCode 栏目编号
|
* @param categoryCode 栏目编号
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static Category getCategory(String categoryCode) {
|
public static Category getCategory(String categoryCode) {
|
||||||
return Static.categoryService.get(categoryCode);
|
return Static.categoryService.get(categoryCode);
|
||||||
@@ -237,8 +227,6 @@ public class CmsUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得文章动态URL地址
|
* 获得文章动态URL地址
|
||||||
* @param article
|
|
||||||
* @return url
|
|
||||||
*/
|
*/
|
||||||
public static String getUrlDynamic(Article article) {
|
public static String getUrlDynamic(Article article) {
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
@@ -259,8 +247,6 @@ public class CmsUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得栏目动态URL地址
|
* 获得栏目动态URL地址
|
||||||
* @param category
|
|
||||||
* @return url
|
|
||||||
*/
|
*/
|
||||||
public static String getUrlDynamic(Category category) {
|
public static String getUrlDynamic(Category category) {
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
@@ -279,8 +265,6 @@ public class CmsUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得站点动态URL地址
|
* 获得站点动态URL地址
|
||||||
* @param site
|
|
||||||
* @return url
|
|
||||||
*/
|
*/
|
||||||
public static String getUrlDynamic(Site site) {
|
public static String getUrlDynamic(Site site) {
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
@@ -299,8 +283,6 @@ public class CmsUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得栏目动态URL地址
|
* 获得栏目动态URL地址
|
||||||
* @param category
|
|
||||||
* @return url
|
|
||||||
*/
|
*/
|
||||||
public static String getAdminUrlDynamic(Category category) {
|
public static String getAdminUrlDynamic(Category category) {
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
@@ -381,8 +363,6 @@ public class CmsUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 从图片地址中去除ContextPath地址
|
* 从图片地址中去除ContextPath地址
|
||||||
* @param src
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static String formatImageSrcToDb(String src) {
|
public static String formatImageSrcToDb(String src) {
|
||||||
if (StringUtils.isBlank(src)) {
|
if (StringUtils.isBlank(src)) {
|
||||||
@@ -397,8 +377,6 @@ public class CmsUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 从图片地址中加入ContextPath地址
|
* 从图片地址中加入ContextPath地址
|
||||||
* @param src
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static String formatImageSrcToWeb(String src) {
|
public static String formatImageSrcToWeb(String src) {
|
||||||
if (StringUtils.isBlank(src)) {
|
if (StringUtils.isBlank(src)) {
|
||||||
@@ -413,8 +391,6 @@ public class CmsUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文章视图
|
* 获取文章视图
|
||||||
* @param article
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static String getArticleView(Article article) {
|
public static String getArticleView(Article article) {
|
||||||
if (StringUtils.isBlank(article.getCustomContentView())) {
|
if (StringUtils.isBlank(article.getCustomContentView())) {
|
||||||
@@ -441,8 +417,6 @@ public class CmsUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 视图配置属性设置
|
* 视图配置属性设置
|
||||||
* @param model
|
|
||||||
* @param params
|
|
||||||
*/
|
*/
|
||||||
public static void addViewConfigAttribute(Model model, String params) {
|
public static void addViewConfigAttribute(Model model, String params) {
|
||||||
if (StringUtils.isNotBlank(params)) {
|
if (StringUtils.isNotBlank(params)) {
|
||||||
@@ -458,8 +432,6 @@ public class CmsUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 视图配置属性设置
|
* 视图配置属性设置
|
||||||
* @param model
|
|
||||||
* @param category
|
|
||||||
*/
|
*/
|
||||||
public static void addViewConfigAttribute(Model model, Category category) {
|
public static void addViewConfigAttribute(Model model, Category category) {
|
||||||
List<Category> categoryList = ListUtils.newArrayList();
|
List<Category> categoryList = ListUtils.newArrayList();
|
||||||
@@ -481,31 +453,67 @@ public class CmsUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取站点服务
|
||||||
|
*/
|
||||||
public static SiteService getSiteService() {
|
public static SiteService getSiteService() {
|
||||||
return Static.siteService;
|
return Static.siteService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取栏目服务
|
||||||
|
*/
|
||||||
public static CategoryService getCategoryService() {
|
public static CategoryService getCategoryService() {
|
||||||
return Static.categoryService;
|
return Static.categoryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文章服务
|
||||||
|
*/
|
||||||
public static ArticleService getArticleService() {
|
public static ArticleService getArticleService() {
|
||||||
return Static.articleService;
|
return Static.articleService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存值
|
||||||
|
* @param key 缓存键
|
||||||
|
*/
|
||||||
public static <V> V getCache(String key) {
|
public static <V> V getCache(String key) {
|
||||||
return CacheUtils.get(CMS_CACHE, key);
|
return CacheUtils.get(CMS_CACHE, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存值
|
||||||
|
* @param key 缓存键
|
||||||
|
* @param defaultValue 默认值
|
||||||
|
*/
|
||||||
public static <V> V getCache(String key, V defaultValue) {
|
public static <V> V getCache(String key, V defaultValue) {
|
||||||
V value = CacheUtils.get(CMS_CACHE, key);
|
V value = CacheUtils.get(CMS_CACHE, key);
|
||||||
return value != null ? value : defaultValue;
|
return value != null ? value : defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置缓存值
|
||||||
|
* @param key 缓存键
|
||||||
|
* @param value 缓存值
|
||||||
|
*/
|
||||||
public static void putCache(String key, Object value) {
|
public static void putCache(String key, Object value) {
|
||||||
CacheUtils.put(CMS_CACHE, key, 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) {
|
public static void removeCache(String key) {
|
||||||
CacheUtils.remove(CMS_CACHE, key);
|
CacheUtils.remove(CMS_CACHE, key);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
*/
|
*/
|
||||||
package com.jeesite.modules.sys.utils;
|
package com.jeesite.modules.sys.utils;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.jeesite.common.utils.SpringUtils;
|
import com.jeesite.common.utils.SpringUtils;
|
||||||
import com.jeesite.modules.sys.entity.Area;
|
import com.jeesite.modules.sys.entity.Area;
|
||||||
import com.jeesite.modules.sys.service.AreaService;
|
import com.jeesite.modules.sys.service.AreaService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author ThinkGem
|
* @author ThinkGem
|
||||||
@@ -29,15 +29,10 @@ public class AreaUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取所有区域列表(系统级别缓存)
|
* 获取所有区域列表(系统级别缓存)
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static List<Area> getAreaAllList(){
|
public static List<Area> getAreaAllList(){
|
||||||
List<Area> areaList = SysCacheUtils.get(CACHE_AREA_ALL_LIST);
|
return SysCacheUtils.computeIfAbsentCache(CACHE_AREA_ALL_LIST, k ->
|
||||||
if (areaList == null){
|
Static.areaService.findList(new Area()));
|
||||||
areaList = Static.areaService.findList(new Area());
|
|
||||||
SysCacheUtils.put(CACHE_AREA_ALL_LIST, areaList);
|
|
||||||
}
|
|
||||||
return areaList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -107,12 +107,8 @@ public class EmpUtils {
|
|||||||
* @author ThinkGem
|
* @author ThinkGem
|
||||||
*/
|
*/
|
||||||
public static List<EmployeeOffice> getEmployeeOfficeList(){
|
public static List<EmployeeOffice> getEmployeeOfficeList(){
|
||||||
List<EmployeeOffice> list = UserUtils.getCache(CACHE_EMPLOYEE_OFFICE_LIST);
|
return UserUtils.computeIfAbsentCache(CACHE_EMPLOYEE_OFFICE_LIST, k ->
|
||||||
if (list == null){
|
Static.employeeService.findEmployeeOfficeList(getEmployee()));
|
||||||
list = Static.employeeService.findEmployeeOfficeList(getEmployee());
|
|
||||||
UserUtils.putCache(CACHE_EMPLOYEE_OFFICE_LIST, list);
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -143,14 +139,19 @@ public class EmpUtils {
|
|||||||
* @author ThinkGem
|
* @author ThinkGem
|
||||||
*/
|
*/
|
||||||
public static List<Office> getOfficeAllList(){
|
public static List<Office> getOfficeAllList(){
|
||||||
List<Office> officeList = CorpUtils.getCache(CACHE_OFFICE_ALL_LIST);
|
// List<Office> officeList = CorpUtils.getCache(CACHE_OFFICE_ALL_LIST);
|
||||||
if (officeList == null){
|
// 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();
|
Office where = new Office();
|
||||||
where.setStatus(Office.STATUS_NORMAL);
|
where.setStatus(Office.STATUS_NORMAL);
|
||||||
officeList = Static.officeService.findList(where);
|
return Static.officeService.findList(where);
|
||||||
CorpUtils.putCache(CACHE_OFFICE_ALL_LIST, officeList);
|
});
|
||||||
}
|
|
||||||
return officeList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -304,14 +305,19 @@ public class EmpUtils {
|
|||||||
* @author ThinkGem
|
* @author ThinkGem
|
||||||
*/
|
*/
|
||||||
public static List<Company> getCompanyAllList(){
|
public static List<Company> getCompanyAllList(){
|
||||||
List<Company> companyList = CorpUtils.getCache(CACHE_COMPANY_ALL_LIST);
|
// List<Company> companyList = CorpUtils.getCache(CACHE_COMPANY_ALL_LIST);
|
||||||
if (companyList == null){
|
// 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();
|
Company where = new Company();
|
||||||
where.setStatus(Office.STATUS_NORMAL);
|
where.setStatus(Office.STATUS_NORMAL);
|
||||||
companyList = Static.companyService.findList(where);
|
return Static.companyService.findList(where);
|
||||||
CorpUtils.putCache(CACHE_COMPANY_ALL_LIST, companyList);
|
});
|
||||||
}
|
|
||||||
return companyList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -351,12 +357,8 @@ public class EmpUtils {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static List<EmployeePost> getEmployeePostList(){
|
public static List<EmployeePost> getEmployeePostList(){
|
||||||
List<EmployeePost> list = UserUtils.getCache(CACHE_EMPLOYEE_POST_LIST);
|
return UserUtils.computeIfAbsentCache(CACHE_EMPLOYEE_POST_LIST, k ->
|
||||||
if (list == null){
|
getEmployeePostList(getEmployee().getEmpCode()));
|
||||||
list = getEmployeePostList(getEmployee().getEmpCode());
|
|
||||||
UserUtils.putCache(CACHE_EMPLOYEE_POST_LIST, list);
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user