소스 검색

缓存修改

wangliang 1 년 전
부모
커밋
a5324463c6

+ 13 - 6
sop-api/src/main/java/com/qmth/sop/server/api/SysController.java

@@ -1,5 +1,6 @@
 package com.qmth.sop.server.api;
 
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -16,6 +17,7 @@ import com.qmth.sop.business.bean.result.*;
 import com.qmth.sop.business.cache.CommonCacheService;
 import com.qmth.sop.business.entity.*;
 import com.qmth.sop.business.service.*;
+import com.qmth.sop.business.util.CacheUtil;
 import com.qmth.sop.business.util.ImportExportUtil;
 import com.qmth.sop.common.annotation.OperationLog;
 import com.qmth.sop.common.contant.SystemConstant;
@@ -24,7 +26,6 @@ import com.qmth.sop.common.util.*;
 import io.swagger.annotations.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.cache.Cache;
 import org.springframework.cache.CacheManager;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
@@ -678,13 +679,19 @@ public class SysController {
     @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = Object.class) })
     @Aac(auth = false)
     public Result getCache(@ApiParam(value = "业务类型") @RequestParam(required = false) String cacheName) {
-        TBSession tbSession = (TBSession) cacheService.get(SystemConstant.SESSION, cacheName);
-        Cache cache = cacheManager.getCache(SystemConstant.SESSION);
+        //        TBSession tbSession = (TBSession) cacheService.get(SystemConstant.SESSION, cacheName);
+        //        Cache cache = cacheManager.getCache(SystemConstant.SESSION);
+        JSONArray jsonArray = CacheUtil.getAll();
+        Object o = CacheUtil.get(SystemConstant.SESSION + cacheName);
+        TBSession tbSession = (TBSession) commonCacheService.getUserSession(cacheName);
         Map<String, Object> map = new HashMap<>();
         map.put("tbsession", tbSession);
-        if (Objects.nonNull(cache)) {
-            map.put("cacheName", cache.get(cacheName, TBSession.class));
-        }
+        map.put("cacheUtil", o);
+        map.put("caches", jsonArray);
+        //        map.put("tbSession1", tbSession1);
+        //        if (Objects.nonNull(cache)) {
+        //            map.put("cacheName", cache.get(cacheName, TBSession.class));
+        //        }
         return ResultUtil.ok(map);
     }
 }

+ 79 - 42
sop-business/src/main/java/com/qmth/sop/business/cache/impl/CommonCacheServiceImpl.java

@@ -11,6 +11,7 @@ import com.qmth.sop.business.bean.result.MenuResult;
 import com.qmth.sop.business.cache.CommonCacheService;
 import com.qmth.sop.business.entity.*;
 import com.qmth.sop.business.service.*;
+import com.qmth.sop.business.util.CacheUtil;
 import com.qmth.sop.common.contant.SystemConstant;
 import com.qmth.sop.common.enums.ExceptionResultEnum;
 import com.qmth.sop.common.enums.PrivilegeEnum;
@@ -37,6 +38,7 @@ import java.util.stream.Collectors;
  */
 @Service
 public class CommonCacheServiceImpl implements CommonCacheService {
+
     private final static Logger log = LoggerFactory.getLogger(CommonCacheServiceImpl.class);
 
     @Resource
@@ -79,7 +81,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     @Override
-//    @Cacheable(value = SystemConstant.USER_ACCOUNT_CACHE, key = "#p0", unless = "#result == null")
+    //    @Cacheable(value = SystemConstant.USER_ACCOUNT_CACHE, key = "#p0", unless = "#result == null")
     public SysUser userCache(Long userId) {
         return userCacheCommon(userId);
     }
@@ -91,7 +93,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     @Override
-//    @CachePut(value = SystemConstant.USER_ACCOUNT_CACHE, key = "#p0", condition = "#result != null")
+    //    @CachePut(value = SystemConstant.USER_ACCOUNT_CACHE, key = "#p0", condition = "#result != null")
     public SysUser updateUserCache(Long userId) {
         cacheService.evict(SystemConstant.USER_ACCOUNT_CACHE, String.valueOf(userId));
         return userCacheCommon(userId);
@@ -122,7 +124,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @param userId
      */
     @Override
-//    @CacheEvict(value = SystemConstant.USER_ACCOUNT_CACHE, key = "#p0")
+    //    @CacheEvict(value = SystemConstant.USER_ACCOUNT_CACHE, key = "#p0")
     public void removeUserCache(Long userId) {
         cacheService.evict(SystemConstant.USER_ACCOUNT_CACHE, String.valueOf(userId));
     }
@@ -136,7 +138,8 @@ public class CommonCacheServiceImpl implements CommonCacheService {
     private SysConfig sysConfigCacheCommon(String key) {
         SysConfig sysConfig = (SysConfig) cacheService.get(SystemConstant.SYS_CONFIG_CACHE, key);
         if (Objects.isNull(sysConfig)) {
-            sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfigKey, key));
+            sysConfig = sysConfigService.getOne(
+                    new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfigKey, key));
             if (Objects.nonNull(sysConfig)) {
                 cacheService.put(SystemConstant.SYS_CONFIG_CACHE, key, sysConfig);
             }
@@ -151,7 +154,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     @Override
-//    @Cacheable(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0", unless = "#result == null")
+    //    @Cacheable(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0", unless = "#result == null")
     public SysConfig addSysConfigCache(String key) {
         return sysConfigCacheCommon(key);
     }
@@ -163,7 +166,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     @Override
-//    @CachePut(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0", condition = "#result != null")
+    //    @CachePut(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0", condition = "#result != null")
     public SysConfig updateSysConfigCache(String key) {
         cacheService.evict(SystemConstant.SYS_CONFIG_CACHE, key);
         return sysConfigCacheCommon(key);
@@ -175,7 +178,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @param key
      */
     @Override
-//    @CacheEvict(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0")
+    //    @CacheEvict(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0")
     public void removeSysConfigCache(String key) {
         cacheService.evict(SystemConstant.SYS_CONFIG_CACHE, key);
     }
@@ -188,7 +191,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      */
     @Override
     public void updateSysConfigCacheForDb(String key, SysConfig sysConfig) {
-//        cacheManager.getCache(SystemConstant.SYS_CONFIG_CACHE).put(key, sysConfig);
+        //        cacheManager.getCache(SystemConstant.SYS_CONFIG_CACHE).put(key, sysConfig);
         cacheService.put(SystemConstant.SYS_CONFIG_CACHE, key, sysConfig);
     }
 
@@ -202,7 +205,8 @@ public class CommonCacheServiceImpl implements CommonCacheService {
     private SysConfig sysConfigCacheCommon(Long orgId, String key) {
         SysConfig sysConfig = (SysConfig) cacheService.get(SystemConstant.SYS_CONFIG_CACHE, orgId + "_" + key);
         if (Objects.isNull(sysConfig)) {
-            sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getOrgId, orgId).eq(SysConfig::getConfigKey, key));
+            sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getOrgId, orgId)
+                    .eq(SysConfig::getConfigKey, key));
             if (Objects.nonNull(sysConfig)) {
                 cacheService.put(SystemConstant.SYS_CONFIG_CACHE, orgId + "_" + key, sysConfig);
             }
@@ -218,7 +222,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     @Override
-//    @Cacheable(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0 + '-' + #p1", unless = "#result == null")
+    //    @Cacheable(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0 + '-' + #p1", unless = "#result == null")
     public SysConfig addSysConfigCache(Long orgId, String key) {
         return sysConfigCacheCommon(orgId, key);
     }
@@ -231,7 +235,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     @Override
-//    @CachePut(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0 + '-' + #p1", condition = "#result != null")
+    //    @CachePut(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0 + '-' + #p1", condition = "#result != null")
     public SysConfig updateSysConfigCache(Long orgId, String key) {
         cacheService.evict(SystemConstant.SYS_CONFIG_CACHE, orgId + "_" + key);
         return sysConfigCacheCommon(orgId, key);
@@ -244,7 +248,7 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @param key
      */
     @Override
-//    @CacheEvict(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0 + '-' + #p1")
+    //    @CacheEvict(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0 + '-' + #p1")
     public void removeSysConfigCache(Long orgId, String key) {
         cacheService.evict(SystemConstant.SYS_CONFIG_CACHE, orgId + "_" + key);
     }
@@ -257,7 +261,8 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      */
     @Override
     public void setUserSession(String sessionId, Object o) {
-        cacheService.put(SystemConstant.SESSION, sessionId, o);
+        CacheUtil.set(SystemConstant.SESSION + sessionId, o, 60 * 24);
+        //        cacheService.put(SystemConstant.SESSION, sessionId, o);
     }
 
     /**
@@ -268,7 +273,8 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      */
     @Override
     public Object getUserSession(String sessionId) {
-        return cacheService.get(SystemConstant.SESSION, sessionId);
+        return CacheUtil.get(SystemConstant.SESSION + sessionId);
+        //        return cacheService.get(SystemConstant.SESSION, sessionId);
     }
 
     /**
@@ -278,7 +284,8 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      */
     @Override
     public void deleteUserSession(String sessionId) {
-        cacheService.evict(SystemConstant.SESSION, sessionId);
+        CacheUtil.delete(SystemConstant.SESSION + sessionId);
+        //        cacheService.evict(SystemConstant.SESSION, sessionId);
     }
 
     /**
@@ -302,22 +309,33 @@ public class CommonCacheServiceImpl implements CommonCacheService {
                     for (Long l : roleIds) {
                         sysRolePrivilegeList.addAll(this.rolePrivilegeCache(l));
                     }
-                    Set<Long> privilegeIds = sysRolePrivilegeList.stream().map(s -> s.getPrivilegeId()).collect(Collectors.toSet());
-                    List<SysRole> sysRoleList = sysRoleService.list(new QueryWrapper<SysRole>().lambda().in(SysRole::getId, roleIds).eq(SysRole::getEnable, true));
-                    int count = Objects.nonNull(sysRoleList) && sysRoleList.size() > 0 ? (int) sysRoleList.stream().filter(s -> Objects.equals(s.getName(), RoleTypeEnum.ADMIN.getDesc())).count() : 0;
+                    Set<Long> privilegeIds = sysRolePrivilegeList.stream().map(s -> s.getPrivilegeId())
+                            .collect(Collectors.toSet());
+                    List<SysRole> sysRoleList = sysRoleService.list(
+                            new QueryWrapper<SysRole>().lambda().in(SysRole::getId, roleIds)
+                                    .eq(SysRole::getEnable, true));
+                    int count = Objects.nonNull(sysRoleList) && sysRoleList.size() > 0 ?
+                            (int) sysRoleList.stream()
+                                    .filter(s -> Objects.equals(s.getName(), RoleTypeEnum.ADMIN.getDesc())).count() :
+                            0;
                     QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
                     if (count > 0) {//超级系统管理员
-                        sysPrivilegeQueryWrapper.lambda().eq(SysPrivilege::getType, PrivilegeEnum.URL).eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
+                        sysPrivilegeQueryWrapper.lambda().eq(SysPrivilege::getType, PrivilegeEnum.URL)
+                                .eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
                         List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
-                        authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()));
+                        authBean = new AuthBean(sysRoleList,
+                                sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()));
                     } else {
                         SysOrg org = Objects.nonNull(user.getOrgId()) ? this.orgCache(user.getOrgId()) : null;
                         List<SysPrivilege> sysPrivilegeList = new ArrayList<>();
                         if (privilegeIds.size() > 0) {
-                            sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, privilegeIds).eq(SysPrivilege::getType, PrivilegeEnum.URL).eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
+                            sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, privilegeIds)
+                                    .eq(SysPrivilege::getType, PrivilegeEnum.URL)
+                                    .eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
                             sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
                         }
-                        authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()), org);
+                        authBean = new AuthBean(sysRoleList,
+                                sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()), org);
                     }
                 }
                 if (Objects.nonNull(authBean)) {
@@ -754,7 +772,8 @@ public class CommonCacheServiceImpl implements CommonCacheService {
     protected BasicSchool schoolCacheCommon(String code) {
         BasicSchool basicSchool = (BasicSchool) cacheService.get(SystemConstant.SCHOOL_CODE_CACHE, code);
         if (Objects.isNull(basicSchool)) {
-            basicSchool = basicSchoolService.getOne(new QueryWrapper<BasicSchool>().lambda().eq(BasicSchool::getCode, code));
+            basicSchool = basicSchoolService.getOne(
+                    new QueryWrapper<BasicSchool>().lambda().eq(BasicSchool::getCode, code));
             if (Objects.nonNull(basicSchool)) {
                 cacheService.put(SystemConstant.SCHOOL_CODE_CACHE, code, basicSchool);
             }
@@ -796,18 +815,20 @@ public class CommonCacheServiceImpl implements CommonCacheService {
                     sysRolePrivilegeList.addAll(this.rolePrivilegeCache(s.getRoleId()));
                 }
             }
-            Set<Long> privilegeIds = sysRolePrivilegeList.stream().map(s -> s.getPrivilegeId()).collect(Collectors.toSet());
+            Set<Long> privilegeIds = sysRolePrivilegeList.stream().map(s -> s.getPrivilegeId())
+                    .collect(Collectors.toSet());
             QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
-            sysPrivilegeQueryWrapper.lambda()
-                    .eq(SysPrivilege::getEnable, true).eq(SysPrivilege::getFrontDisplay, true)
+            sysPrivilegeQueryWrapper.lambda().eq(SysPrivilege::getEnable, true).eq(SysPrivilege::getFrontDisplay, true)
                     .orderByAsc(SysPrivilege::getSequence);
 
             if (!privilegeIds.isEmpty()) {
                 sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, privilegeIds);
             }
             List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
-            List<MenuDto> menuDtoList = GsonUtil.fromJson(JacksonUtil.parseJson(sysPrivilegeList), new TypeToken<List<MenuDto>>() {
-            }.getType());
+            List<MenuDto> menuDtoList = GsonUtil.fromJson(JacksonUtil.parseJson(sysPrivilegeList),
+                    new TypeToken<List<MenuDto>>() {
+
+                    }.getType());
             LinkedMultiValueMap<Long, MenuDto> linkedMultiValueMap = new LinkedMultiValueMap<>();
             List<MenuPrivilegeDto> menuPrivilegeDtoList = new ArrayList<>();
             for (MenuDto m : menuDtoList) {
@@ -864,9 +885,11 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     protected List<TDTableProp> tablePropCacheCommon(Long widgetId) {
-        List<TDTableProp> tablePropList = (List<TDTableProp>) cacheService.get(SystemConstant.TABLE_PROP_CACHE, String.valueOf(widgetId));
+        List<TDTableProp> tablePropList = (List<TDTableProp>) cacheService.get(SystemConstant.TABLE_PROP_CACHE,
+                String.valueOf(widgetId));
         if (CollectionUtils.isEmpty(tablePropList)) {
-            tablePropList = tdTablePropService.list(new QueryWrapper<TDTableProp>().lambda().eq(TDTableProp::getWidgetId, widgetId));
+            tablePropList = tdTablePropService.list(
+                    new QueryWrapper<TDTableProp>().lambda().eq(TDTableProp::getWidgetId, widgetId));
             if (!CollectionUtils.isEmpty(tablePropList)) {
                 cacheService.put(SystemConstant.TABLE_PROP_CACHE, String.valueOf(widgetId), tablePropList);
             }
@@ -881,7 +904,8 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     protected TDFormWidget formWidgetCacheCommon(Long id) {
-        TDFormWidget tdFormWidget = (TDFormWidget) cacheService.get(SystemConstant.FORM_WIDGET_CACHE, String.valueOf(id));
+        TDFormWidget tdFormWidget = (TDFormWidget) cacheService.get(SystemConstant.FORM_WIDGET_CACHE,
+                String.valueOf(id));
         if (Objects.isNull(tdFormWidget)) {
             tdFormWidget = tdFormWidgetService.getById(id);
             if (Objects.nonNull(tdFormWidget)) {
@@ -897,13 +921,18 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     protected List<FormWidgetResult> formWidgetCacheCommon() {
-        List<FormWidgetResult> formWidgetResultList = (List<FormWidgetResult>) cacheService.get(SystemConstant.FORM_WIDGET_CACHE, SystemConstant.FORM_WIDGET_CACHE);
+        List<FormWidgetResult> formWidgetResultList = (List<FormWidgetResult>) cacheService.get(
+                SystemConstant.FORM_WIDGET_CACHE, SystemConstant.FORM_WIDGET_CACHE);
         if (CollectionUtils.isEmpty(formWidgetResultList)) {
-            List<TDFormWidget> tdFormWidgetList = tdFormWidgetService.list(new QueryWrapper<TDFormWidget>().select(" DISTINCT code "));
+            List<TDFormWidget> tdFormWidgetList = tdFormWidgetService.list(
+                    new QueryWrapper<TDFormWidget>().select(" DISTINCT code "));
             if (!CollectionUtils.isEmpty(tdFormWidgetList)) {
-                formWidgetResultList = GsonUtil.fromJson(JacksonUtil.parseJson(tdFormWidgetList), new TypeToken<List<FormWidgetResult>>() {
-                }.getType());
-                cacheService.put(SystemConstant.FORM_WIDGET_CACHE, SystemConstant.FORM_WIDGET_CACHE, formWidgetResultList);
+                formWidgetResultList = GsonUtil.fromJson(JacksonUtil.parseJson(tdFormWidgetList),
+                        new TypeToken<List<FormWidgetResult>>() {
+
+                        }.getType());
+                cacheService.put(SystemConstant.FORM_WIDGET_CACHE, SystemConstant.FORM_WIDGET_CACHE,
+                        formWidgetResultList);
             }
         }
         return formWidgetResultList;
@@ -916,9 +945,12 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     protected Set<String> privilegeUrlCacheCommon(PrivilegePropertyEnum privilegePropertyEnum) {
-        Set<String> privilegeUrlSet = (Set<String>) cacheService.get(SystemConstant.PRIVILEGE_URL_CACHE, privilegePropertyEnum.name());
+        Set<String> privilegeUrlSet = (Set<String>) cacheService.get(SystemConstant.PRIVILEGE_URL_CACHE,
+                privilegePropertyEnum.name());
         if (CollectionUtils.isEmpty(privilegeUrlSet)) {
-            List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(new QueryWrapper<SysPrivilege>().lambda().eq(SysPrivilege::getType, PrivilegeEnum.URL).eq(SysPrivilege::getProperty, privilegePropertyEnum).eq(SysPrivilege::getEnable, true));
+            List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(
+                    new QueryWrapper<SysPrivilege>().lambda().eq(SysPrivilege::getType, PrivilegeEnum.URL)
+                            .eq(SysPrivilege::getProperty, privilegePropertyEnum).eq(SysPrivilege::getEnable, true));
             if (!CollectionUtils.isEmpty(sysPrivilegeList)) {
                 privilegeUrlSet = sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet());
                 cacheService.put(SystemConstant.PRIVILEGE_URL_CACHE, privilegePropertyEnum.name(), privilegeUrlSet);
@@ -951,9 +983,12 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     protected List<SysUserRole> userRoleCacheCommon(Long userId) {
-        List<SysUserRole> sysUserRoleList = (List<SysUserRole>) cacheService.get(SystemConstant.USER_ROLE_PRIVILEGE_CACHE, String.valueOf(userId));
+        List<SysUserRole> sysUserRoleList = (List<SysUserRole>) cacheService.get(
+                SystemConstant.USER_ROLE_PRIVILEGE_CACHE, String.valueOf(userId));
         if (CollectionUtils.isEmpty(sysUserRoleList)) {
-            sysUserRoleList = sysUserRoleService.list(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, userId).eq(SysUserRole::getEnable, true));
+            sysUserRoleList = sysUserRoleService.list(
+                    new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, userId)
+                            .eq(SysUserRole::getEnable, true));
             if (!CollectionUtils.isEmpty(sysUserRoleList)) {
                 cacheService.put(SystemConstant.USER_ROLE_PRIVILEGE_CACHE, String.valueOf(userId), sysUserRoleList);
             }
@@ -968,9 +1003,11 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * @return
      */
     protected List<SysRolePrivilege> rolePrivilegeCacheCommon(Long roleId) {
-        List<SysRolePrivilege> sysRolePrivilegeList = (List<SysRolePrivilege>) cacheService.get(SystemConstant.ROLE_PRIVILEGE_CACHE, String.valueOf(roleId));
+        List<SysRolePrivilege> sysRolePrivilegeList = (List<SysRolePrivilege>) cacheService.get(
+                SystemConstant.ROLE_PRIVILEGE_CACHE, String.valueOf(roleId));
         if (CollectionUtils.isEmpty(sysRolePrivilegeList)) {
-            sysRolePrivilegeList = sysRolePrivilegeService.list(new QueryWrapper<SysRolePrivilege>().lambda().eq(SysRolePrivilege::getRoleId, roleId));
+            sysRolePrivilegeList = sysRolePrivilegeService.list(
+                    new QueryWrapper<SysRolePrivilege>().lambda().eq(SysRolePrivilege::getRoleId, roleId));
             if (!CollectionUtils.isEmpty(sysRolePrivilegeList)) {
                 cacheService.put(SystemConstant.ROLE_PRIVILEGE_CACHE, String.valueOf(roleId), sysRolePrivilegeList);
             }

+ 149 - 0
sop-business/src/main/java/com/qmth/sop/business/util/CacheUtil.java

@@ -0,0 +1,149 @@
+package com.qmth.sop.business.util;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.Expiry;
+import org.checkerframework.checker.index.qual.NonNegative;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import static java.util.Objects.isNull;
+
+/**
+ * @Description: CAFFEINE缓存工具类
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2023/12/27
+ */
+public class CacheUtil {
+
+    private final static Logger log = LoggerFactory.getLogger(CacheUtil.class);
+
+    /**
+     * 不设置过期时长
+     */
+    public static final long NOT_EXPIRE = Long.MAX_VALUE;
+
+    public static final Cache<String, CacheObject<?>> CAFFEINE = Caffeine.newBuilder()
+            .expireAfter(new Expiry<String, CacheObject<?>>() {
+
+                @Override
+                public long expireAfterCreate(@NonNull String key, @NonNull CacheObject<?> value, long currentTime) {
+                    return value.expire;
+                }
+
+                @Override
+                public long expireAfterUpdate(@NonNull String key, @NonNull CacheObject<?> value, long currentTime,
+                        @NonNegative long currentDuration) {
+                    return value.expire;
+                }
+
+                @Override
+                public long expireAfterRead(@NonNull String key, @NonNull CacheObject<?> value, long currentTime,
+                        @NonNegative long currentDuration) {
+                    return value.expire;
+                }
+            }).initialCapacity(100).maximumSize(1024).build();
+
+    private static class CacheObject<T> {
+
+        T data;
+
+        long expire;
+
+        public CacheObject(T data, long second) {
+            this.data = data;
+            this.expire = TimeUnit.SECONDS.toNanos(second);
+        }
+    }
+
+    public static <T> void set(String key, T value, long expire) {
+        CacheObject<T> cacheObject = new CacheObject<>(value, expire);
+        CAFFEINE.put(key, cacheObject);
+    }
+
+    public static void set(String key, Object value) {
+        set(key, value, NOT_EXPIRE);
+    }
+
+    @SuppressWarnings("unchecked")
+    public static <T> T get(String key) {
+        CacheObject<?> cacheObject = CAFFEINE.getIfPresent(key);
+        if (isNull(cacheObject)) {
+            return null;
+        }
+        return (T) cacheObject.data;
+    }
+
+    public static void delete(String key) {
+        CAFFEINE.invalidate(key);
+    }
+
+    public static void delete(Collection<String> keys) {
+        CAFFEINE.invalidateAll(keys);
+    }
+
+    public static JSONArray getAll() {
+        Map map = CAFFEINE.asMap();
+        JSONArray jsonArray = new JSONArray();
+        for (Object key : map.keySet()) {
+            CacheObject cacheObject = (CacheObject) map.get(key);
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put(key.toString(), cacheObject.data + ";" + TimeUnit.SECONDS.toMinutes(cacheObject.expire));
+            jsonArray.add(jsonObject);
+        }
+        return jsonArray;
+    }
+
+    //    public static void main(String[] args) throws InterruptedException {
+    //        CacheUtil.set("A", 1, 2 * 60);
+    //        CacheUtil.set("B", 2, 5 * 60);
+    //        CacheUtil.set("C", 3, 8 * 60);
+    //
+    //        Map map = CAFFEINE.asMap();
+    //        for (Object key : map.keySet()) {
+    //            CacheObject cacheObject = (CacheObject) map.get(key);
+    //            System.out.println(key + "," + cacheObject.data + "," + TimeUnit.SECONDS.toMinutes(cacheObject.expire));
+    //        }
+    //        Object a = CacheUtil.get("A");
+    //        Object b = CacheUtil.get("B");
+    //        Object c = CacheUtil.get("C");
+    //        System.out.println(a);
+    //        System.out.println(b);
+    //        System.out.println(c);
+    //        System.out.println("-----------------");
+    //        Thread.sleep(2000);
+    //
+    //        a = CacheUtil.get("A");
+    //        b = CacheUtil.get("B");
+    //        c = CacheUtil.get("C");
+    //        System.out.println(a);
+    //        System.out.println(b);
+    //        System.out.println(c);
+    //        System.out.println("-----------------");
+    //        Thread.sleep(5000);
+    //        a = CacheUtil.get("A");
+    //        b = CacheUtil.get("B");
+    //        c = CacheUtil.get("C");
+    //        System.out.println(a);
+    //        System.out.println(b);
+    //        System.out.println(c);
+    //        System.out.println("-----------------");
+    //        Thread.sleep(1000 * 120);
+    //        a = CacheUtil.get("A");
+    //        b = CacheUtil.get("B");
+    //        c = CacheUtil.get("C");
+    //        System.out.println(a);
+    //        System.out.println(b);
+    //        System.out.println(c);
+    //        System.out.println("-----------------");
+    //    }
+}