Pārlūkot izejas kodu

优化缓存配置

zhouhao 7 gadi atpakaļ
vecāks
revīzija
8bc3ffda36

+ 2 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/CacheConstants.java

@@ -28,6 +28,8 @@ import org.hswebframework.web.authorization.AuthenticationManager;
 public interface CacheConstants {
     String MENU_CACHE_NAME = "hsweb-menu-";
 
+    String USER_MENU_CACHE_NAME = "hsweb-user-menu-";
+
     String USER_CACHE_NAME = "user-";
 
     String USER_AUTH_CACHE_NAME = AuthenticationManager.USER_AUTH_CACHE_NAME;

+ 32 - 9
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthorizationSettingService.java

@@ -39,6 +39,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheConfig;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.cache.annotation.Caching;
 import org.springframework.stereotype.Service;
 
 import java.util.*;
@@ -53,7 +54,9 @@ import static org.hswebframework.web.commons.entity.DataStatus.STATUS_ENABLED;
 import static org.hswebframework.web.entity.authorization.AuthorizationSettingDetailEntity.*;
 import static org.hswebframework.web.entity.authorization.AuthorizationSettingEntity.settingFor;
 import static org.hswebframework.web.entity.authorization.AuthorizationSettingEntity.type;
+import static org.hswebframework.web.service.authorization.simple.CacheConstants.MENU_CACHE_NAME;
 import static org.hswebframework.web.service.authorization.simple.CacheConstants.USER_AUTH_CACHE_NAME;
+import static org.hswebframework.web.service.authorization.simple.CacheConstants.USER_MENU_CACHE_NAME;
 
 /**
  * 默认的服务实现
@@ -102,7 +105,12 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
     }
 
     @Override
-    @CacheEvict(allEntries = true)
+    @Caching(
+            evict = {
+                    @CacheEvict(allEntries = true),
+                    @CacheEvict(cacheNames = USER_MENU_CACHE_NAME,allEntries = true)
+            }
+    )
     public String saveOrUpdate(AuthorizationSettingEntity entity) {
         AuthorizationSettingEntity old = select(entity.getType(), entity.getSettingFor());
         if (old != null) {
@@ -113,7 +121,12 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
     }
 
     @Override
-    @CacheEvict(allEntries = true)
+    @Caching(
+            evict = {
+                    @CacheEvict(allEntries = true),
+                    @CacheEvict(cacheNames = USER_MENU_CACHE_NAME,allEntries = true)
+            }
+    )
     public String insert(AuthorizationSettingEntity entity) {
         tryValidateProperty(select(entity.getType(), entity.getSettingFor()) == null, AuthorizationSettingEntity.settingFor, "存在相同的配置!");
         entity.setStatus(STATUS_ENABLED);
@@ -138,7 +151,12 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
     }
 
     @Override
-    @CacheEvict(allEntries = true)
+    @Caching(
+            evict = {
+                    @CacheEvict(allEntries = true),
+                    @CacheEvict(cacheNames = USER_MENU_CACHE_NAME,allEntries = true)
+            }
+    )
     public int updateByPk(String id, AuthorizationSettingEntity entity) {
         int size = super.updateByPk(id, entity);
         if (entity.getMenus() != null) {
@@ -166,7 +184,12 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
     }
 
     @Override
-    @CacheEvict(allEntries = true)
+    @Caching(
+            evict = {
+                    @CacheEvict(allEntries = true),
+                    @CacheEvict(cacheNames = USER_MENU_CACHE_NAME,allEntries = true)
+            }
+    )
     public int deleteByPk(String id) {
         Objects.requireNonNull(id, "id can not be null");
         authorizationSettingMenuService.deleteBySettingId(id);
@@ -184,7 +207,7 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
         Stream<Map.Entry<String, List<SettingInfo>>> settingInfoStream = settingInfo.entrySet().stream();
         //大于1 使用并行处理
         if (settingInfo.size() > 1) {
-            settingInfoStream.parallel();
+            settingInfoStream = settingInfoStream.parallel();
         }
         return settingInfoStream
                 .map(entry ->
@@ -199,14 +222,14 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
     }
 
     @Override
-    @Cacheable(key = "'user-menu-list:'+#userId")
+    @Cacheable(cacheNames = USER_MENU_CACHE_NAME, key = "'user-menu-list:'+#userId")
     public List<UserMenuEntity> getUserMenuAsList(String userId) {
         if (null == userId) {
-            return null;
+            return Collections.emptyList();
         }
         UserEntity userEntity = userService.selectByPk(userId);
         if (userEntity == null) {
-            return null;
+            return Collections.emptyList();
         }
         List<AuthorizationSettingEntity> entities = getUserSetting(userId);
         if (entities.isEmpty()) {
@@ -262,7 +285,7 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
     }
 
     @Override
-    @Cacheable(key = "'menu-tree:'+#userId")
+    @Cacheable(cacheNames = USER_MENU_CACHE_NAME, key = "'menu-tree:'+#userId")
     public List<UserMenuEntity> getUserMenuAsTree(String userId) {
         return TreeSupportEntity.list2tree(getUserMenuAsList(userId), UserMenuEntity::setChildren,
                 (Predicate<UserMenuEntity>) menuEntity ->

+ 3 - 5
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleMenuService.java

@@ -33,8 +33,6 @@ import org.springframework.stereotype.Service;
 import java.util.List;
 
 /**
- * TODO 完成注释
- *
  * @author zhouhao
  */
 @Service("menuService")
@@ -64,7 +62,7 @@ public class SimpleMenuService
     @Caching(
             evict = {
                     @CacheEvict(allEntries = true),
-                    @CacheEvict(cacheNames = CacheConstants.MENU_CACHE_NAME, allEntries = true)
+                    @CacheEvict(cacheNames = CacheConstants.USER_MENU_CACHE_NAME, allEntries = true)
             }
     )
     public int updateByPk(String id, MenuEntity entity) {
@@ -75,7 +73,7 @@ public class SimpleMenuService
     @Caching(
             evict = {
                     @CacheEvict(allEntries = true),
-                    @CacheEvict(cacheNames = CacheConstants.MENU_CACHE_NAME, allEntries = true)
+                    @CacheEvict(cacheNames = CacheConstants.USER_MENU_CACHE_NAME, allEntries = true)
             }
     )
     public int updateByPk(List<MenuEntity> data) {
@@ -101,7 +99,7 @@ public class SimpleMenuService
     @Caching(
             evict = {
                     @CacheEvict(allEntries = true),
-                    @CacheEvict(cacheNames = CacheConstants.MENU_CACHE_NAME, allEntries = true)
+                    @CacheEvict(cacheNames = CacheConstants.USER_MENU_CACHE_NAME, allEntries = true)
             }
     )
     public int deleteByPk(String id) {