Explorar o código

优化获取用户分组逻辑

Woody hai 3 semanas
pai
achega
4e264ff7d9

+ 25 - 2
framework-group/src/main/java/com/chelvc/framework/group/GroupStore.java

@@ -3,6 +3,7 @@ package com.chelvc.framework.group;
 import java.util.Collection;
 import java.util.Map;
 
+import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.common.model.Caps;
 
 /**
@@ -18,15 +19,37 @@ public interface GroupStore {
      * @param scene 场景标识
      * @return 分组标识
      */
-    Caps get(String scene);
+    default Caps get(String scene) {
+        return this.get(SessionContextHolder.getId(), scene);
+    }
+
+    /**
+     * 获取场景分组
+     *
+     * @param id    用户ID
+     * @param scene 场景标识
+     * @return 分组标识
+     */
+    Caps get(Long id, String scene);
+
+    /**
+     * 批量获取场景分组
+     *
+     * @param scenes 分组标识集合
+     * @return 场景/分组映射表
+     */
+    default Map<String, Caps> get(Collection<String> scenes) {
+        return this.get(SessionContextHolder.getId(), scenes);
+    }
 
     /**
      * 批量获取场景分组
      *
+     * @param id     用户ID
      * @param scenes 分组标识集合
      * @return 场景/分组映射表
      */
-    Map<String, Caps> get(Collection<String> scenes);
+    Map<String, Caps> get(Long id, Collection<String> scenes);
 
     /**
      * 设置场景分组,如果分组不存在则设置分组,否则忽略

+ 18 - 15
framework-group/src/main/java/com/chelvc/framework/group/RedisGroupStore.java

@@ -41,13 +41,13 @@ public class RedisGroupStore implements GroupStore {
     }
 
     /**
-     * 获取Redis用户分组标识
+     * 获取用户分组标识
      *
+     * @param id 用户ID
      * @return Redis标识
      */
-    private String key() {
-        Long id = SessionContextHolder.getId();
-        return id == null ? null : ("group:" + id);
+    private String key(Long id) {
+        return "group:" + id;
     }
 
     /**
@@ -139,9 +139,8 @@ public class RedisGroupStore implements GroupStore {
     }
 
     @Override
-    public Caps get(@NonNull String scene) {
-        String key = this.key();
-        if (StringUtils.isEmpty(key)) {
+    public Caps get(Long id, @NonNull String scene) {
+        if (id == null) {
             return null;
         }
 
@@ -152,6 +151,7 @@ public class RedisGroupStore implements GroupStore {
         }
 
         // 获取分组标识,如果缓存不存在则重新加载分组并更新缓存
+        String key = this.key(id);
         RedisTemplate<String, Object> template = RedisContextHolder.getDefaultTemplate();
         HashOperations<String, String, String> operations = template.opsForHash();
         group = StringUtils.ifEmpty(operations.get(key, scene), Caps::valueOf);
@@ -162,9 +162,8 @@ public class RedisGroupStore implements GroupStore {
     }
 
     @Override
-    public Map<String, Caps> get(@NonNull Collection<String> scenes) {
-        String key = this.key();
-        if (StringUtils.isEmpty(key) || ObjectUtils.isEmpty(scenes)) {
+    public Map<String, Caps> get(Long id, @NonNull Collection<String> scenes) {
+        if (id == null || ObjectUtils.isEmpty(scenes)) {
             return Collections.emptyMap();
         }
 
@@ -181,6 +180,7 @@ public class RedisGroupStore implements GroupStore {
         }
 
         // 批量获取缓存分组映射
+        String key = this.key(id);
         RedisTemplate<String, Object> template = RedisContextHolder.getDefaultTemplate();
         if (ObjectUtils.notEmpty(nones)) {
             HashOperations<String, String, String> operations = template.opsForHash();
@@ -211,16 +211,19 @@ public class RedisGroupStore implements GroupStore {
 
     @Override
     public Caps set(@NonNull String scene, @NonNull Caps group) {
-        String key = this.key();
-        return StringUtils.isEmpty(key) ? group : this.setnx(key, scene, group);
+        Long id = SessionContextHolder.getId();
+        return id == null ? group : this.setnx(this.key(id), scene, group);
     }
 
     @Override
     public Map<String, Caps> set(@NonNull Map<String, Caps> groups) {
-        String key = this.key();
-        if (StringUtils.isEmpty(key) || ObjectUtils.isEmpty(groups)) {
+        Long id = SessionContextHolder.getId();
+        if (id == null || ObjectUtils.isEmpty(groups)) {
             return groups;
-        } else if (groups.size() == 1) {
+        }
+
+        String key = this.key(id);
+        if (groups.size() == 1) {
             Map.Entry<String, Caps> entry = groups.entrySet().iterator().next();
             String scene = entry.getKey();
             Caps group = this.setnx(key, scene, entry.getValue());