Răsfoiți Sursa

优化业务分组处理逻辑

Woody 18 ore în urmă
părinte
comite
ee801d3901

+ 12 - 8
framework-group/src/main/java/com/chelvc/framework/group/GroupClient.java

@@ -22,35 +22,39 @@ public interface GroupClient {
     /**
      * 获取场景分组
      *
+     * @param id    用户ID
      * @param scene 场景标识
      * @return 分组标识
      */
-    @GetMapping("/group/{scene}")
-    Caps getGroup(@PathVariable("scene") String scene);
+    @GetMapping("/user/{id}/group/{scene}")
+    Caps getGroup(@PathVariable("id") Long id, @PathVariable("scene") String scene);
 
     /**
      * 批量获取场景分组
      *
+     * @param id     用户ID
      * @param scenes 场景标识集合
      * @return 场景/分组映射表
      */
-    @GetMapping("/groups")
-    Map<String, Caps> getGroups(@RequestParam("scenes") Collection<String> scenes);
+    @GetMapping("/user/{id}/groups")
+    Map<String, Caps> getGroups(@PathVariable("id") Long id, @RequestParam("scenes") Collection<String> scenes);
 
     /**
      * 设置场景分组
      *
+     * @param id    用户ID
      * @param scene 场景标识
      * @param group 分组标识
      */
-    @PostMapping("/group/{scene}/{group}")
-    void setGroup(@PathVariable("scene") String scene, @PathVariable("group") Caps group);
+    @PostMapping("/user/{id}/group/{scene}/{group}")
+    void setGroup(@PathVariable("id") Long id, @PathVariable("scene") String scene, @PathVariable("group") Caps group);
 
     /**
      * 批量设置场景分组
      *
+     * @param id     用户ID
      * @param groups 场景/分组映射表
      */
-    @PostMapping("/groups")
-    void setGroups(@RequestBody Map<String, Caps> groups);
+    @PostMapping("/user/{id}/groups")
+    void setGroups(@PathVariable("id") Long id, @RequestBody Map<String, Caps> groups);
 }

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

@@ -102,7 +102,7 @@ public class GroupProcessInterceptor {
         // 保存场景分组,以最终结果为准
         Map<String, Caps> stores;
         try {
-            stores = this.store.set(values);
+            stores = this.store.setGroups(values);
             stores.forEach(GroupContextHolder::setGroup);
         } catch (Exception e) {
             log.error("Group storing failed", e);
@@ -143,7 +143,7 @@ public class GroupProcessInterceptor {
         // 获取已存储分组结果
         Set<String> scenes = matches.stream().map(Group::scene).collect(Collectors.toSet());
         try {
-            Map<String, Caps> values = this.store.get(scenes);
+            Map<String, Caps> values = this.store.getGroups(scenes);
             if (ObjectUtils.notEmpty(values)) {
                 values.forEach((scene, value) -> {
                     if (value != null) {

+ 31 - 8
framework-group/src/main/java/com/chelvc/framework/group/GroupStore.java

@@ -19,8 +19,8 @@ public interface GroupStore {
      * @param scene 场景标识
      * @return 分组标识
      */
-    default Caps get(String scene) {
-        return this.get(SessionContextHolder.getId(), scene);
+    default Caps getGroup(String scene) {
+        return this.getGroup(SessionContextHolder.getId(), scene);
     }
 
     /**
@@ -30,7 +30,7 @@ public interface GroupStore {
      * @param scene 场景标识
      * @return 分组标识
      */
-    Caps get(Long id, String scene);
+    Caps getGroup(Long id, String scene);
 
     /**
      * 批量获取场景分组
@@ -38,8 +38,8 @@ public interface GroupStore {
      * @param scenes 分组标识集合
      * @return 场景/分组映射表
      */
-    default Map<String, Caps> get(Collection<String> scenes) {
-        return this.get(SessionContextHolder.getId(), scenes);
+    default Map<String, Caps> getGroups(Collection<String> scenes) {
+        return this.getGroups(SessionContextHolder.getId(), scenes);
     }
 
     /**
@@ -49,7 +49,7 @@ public interface GroupStore {
      * @param scenes 分组标识集合
      * @return 场景/分组映射表
      */
-    Map<String, Caps> get(Long id, Collection<String> scenes);
+    Map<String, Caps> getGroups(Long id, Collection<String> scenes);
 
     /**
      * 设置场景分组,如果分组不存在则设置分组,否则忽略
@@ -58,7 +58,19 @@ public interface GroupStore {
      * @param group 分组标识
      * @return 分组标识
      */
-    Caps set(String scene, Caps group);
+    default Caps setGroup(String scene, Caps group) {
+        return this.setGroup(SessionContextHolder.getId(), scene, group);
+    }
+
+    /**
+     * 设置场景分组,如果分组不存在则设置分组,否则忽略
+     *
+     * @param id    用户ID
+     * @param scene 场景标识
+     * @param group 分组标识
+     * @return 分组标识
+     */
+    Caps setGroup(Long id, String scene, Caps group);
 
     /**
      * 批量设置场景分组,如果分组不存在则设置分组,否则忽略
@@ -66,5 +78,16 @@ public interface GroupStore {
      * @param groups 分组/场景映射表
      * @return 分组/场景映射表
      */
-    Map<String, Caps> set(Map<String, Caps> groups);
+    default Map<String, Caps> setGroups(Map<String, Caps> groups) {
+        return this.setGroups(SessionContextHolder.getId(), groups);
+    }
+
+    /**
+     * 批量设置场景分组,如果分组不存在则设置分组,否则忽略
+     *
+     * @param id     用户ID
+     * @param groups 分组/场景映射表
+     * @return 分组/场景映射表
+     */
+    Map<String, Caps> setGroups(Long id, Map<String, Caps> groups);
 }

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

@@ -7,7 +7,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
-import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.common.model.Caps;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
@@ -53,13 +52,14 @@ public class RedisGroupStore implements GroupStore {
     /**
      * 设置场景分组,如果存在则使用原值,否则更新
      *
-     * @param key   缓存标识
+     * @param id    用户ID
      * @param scene 场景标识
      * @param group 分组标识
      * @return 分组标识
      */
-    private Caps setnx(String key, String scene, Caps group) {
+    private Caps setnx(Long id, String scene, Caps group) {
         // 更新场景分组缓存
+        String key = this.key(id);
         RedisTemplate<String, Object> template = RedisContextHolder.getDefaultTemplate();
         List<Object> results = template.executePipelined(new SessionCallback<Object>() {
             @Override
@@ -76,7 +76,7 @@ public class RedisGroupStore implements GroupStore {
 
         // 如果缓存更新成功则持久化分组信息
         if (Boolean.TRUE.equals(results.get(0))) {
-            this.client.setGroup(scene, group);
+            this.client.setGroup(id, scene, group);
             return group;
         }
 
@@ -88,12 +88,13 @@ public class RedisGroupStore implements GroupStore {
     /**
      * 批量设置场景分组,如果存在则使用原值,否则更新
      *
-     * @param key    缓存标识
+     * @param id     用户ID
      * @param groups 场景/分组映射表
      * @return 场景/分组映射表
      */
-    private Map<String, Caps> setnx(String key, Map<String, Caps> groups) {
+    private Map<String, Caps> setnx(Long id, Map<String, Caps> groups) {
         // 批量更新场景分组缓存
+        String key = this.key(id);
         Map<String, Caps> stored = Maps.newLinkedHashMap(groups);
         RedisTemplate<String, Object> template = RedisContextHolder.getDefaultTemplate();
         List<Object> results = template.executePipelined(new SessionCallback<Object>() {
@@ -124,7 +125,7 @@ public class RedisGroupStore implements GroupStore {
 
         // 持久化缓存更新成功的场景分组
         if (ObjectUtils.notEmpty(stored)) {
-            this.client.setGroups(stored);
+            this.client.setGroups(id, stored);
         }
 
         // 重新加载已存在缓存的场景分组
@@ -139,7 +140,7 @@ public class RedisGroupStore implements GroupStore {
     }
 
     @Override
-    public Caps get(Long id, @NonNull String scene) {
+    public Caps getGroup(Long id, @NonNull String scene) {
         if (id == null) {
             return null;
         }
@@ -155,14 +156,14 @@ public class RedisGroupStore implements GroupStore {
         RedisTemplate<String, Object> template = RedisContextHolder.getDefaultTemplate();
         HashOperations<String, String, String> operations = template.opsForHash();
         group = StringUtils.ifEmpty(operations.get(key, scene), Caps::valueOf);
-        if (group == null && (group = this.client.getGroup(scene)) != null) {
+        if (group == null && (group = this.client.getGroup(id, scene)) != null) {
             RedisHashHolder.set(template, key, scene, group, this.timeout);
         }
         return group;
     }
 
     @Override
-    public Map<String, Caps> get(Long id, @NonNull Collection<String> scenes) {
+    public Map<String, Caps> getGroups(Long id, @NonNull Collection<String> scenes) {
         if (id == null || ObjectUtils.isEmpty(scenes)) {
             return Collections.emptyMap();
         }
@@ -200,7 +201,7 @@ public class RedisGroupStore implements GroupStore {
 
         // 重新加载场景分组为null的数据
         if (ObjectUtils.notEmpty(nones)) {
-            Map<String, Caps> reloads = this.client.getGroups(nones);
+            Map<String, Caps> reloads = this.client.getGroups(id, nones);
             if (ObjectUtils.notEmpty(reloads)) {
                 groups.putAll(reloads);
                 RedisHashHolder.set(template, key, reloads, this.timeout);
@@ -210,25 +211,20 @@ public class RedisGroupStore implements GroupStore {
     }
 
     @Override
-    public Caps set(@NonNull String scene, @NonNull Caps group) {
-        Long id = SessionContextHolder.getId();
-        return id == null ? group : this.setnx(this.key(id), scene, group);
+    public Caps setGroup(Long id, @NonNull String scene, @NonNull Caps group) {
+        return id == null ? group : this.setnx(id, scene, group);
     }
 
     @Override
-    public Map<String, Caps> set(@NonNull Map<String, Caps> groups) {
-        Long id = SessionContextHolder.getId();
+    public Map<String, Caps> setGroups(Long id, @NonNull Map<String, Caps> groups) {
         if (id == null || ObjectUtils.isEmpty(groups)) {
             return groups;
-        }
-
-        String key = this.key(id);
-        if (groups.size() == 1) {
+        } else 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());
+            Caps group = this.setnx(id, scene, entry.getValue());
             return group == entry.getValue() ? groups : ImmutableMap.of(scene, group);
         }
-        return this.setnx(key, groups);
+        return this.setnx(id, groups);
     }
 }

+ 1 - 1
framework-group/src/main/java/com/chelvc/framework/group/context/GroupContextHolder.java

@@ -116,7 +116,7 @@ public final class GroupContextHolder {
         }
         if ((group = session.getGroup(scene)) == null) {
             GroupStore store = getStore();
-            group = ObjectUtils.ifNull(store.get(scene), Caps.NONE);
+            group = ObjectUtils.ifNull(store.getGroup(scene), Caps.NONE);
             session.setGroup(scene, group);
         }
         return group == Caps.NONE ? null : group;