|
@@ -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);
|
|
|
}
|
|
|
}
|