From 0ed1080a2472857bcdda116a0ffd57ee1afcc23d Mon Sep 17 00:00:00 2001 From: HouKunLin Date: Wed, 27 Sep 2023 17:47:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BC=93=E5=AD=98=E5=88=B0=20Redis=20?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E5=AD=98=E5=82=A8=E5=88=B0=20Redis=20Hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/dict/starter/DictUtil.java | 46 +++++++++++++++++++ .../dict/starter/store/RedisDictStore.java | 35 +++++++------- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/houkunlin/system/dict/starter/DictUtil.java b/src/main/java/com/houkunlin/system/dict/starter/DictUtil.java index 80b4f38..0766bba 100644 --- a/src/main/java/com/houkunlin/system/dict/starter/DictUtil.java +++ b/src/main/java/com/houkunlin/system/dict/starter/DictUtil.java @@ -227,6 +227,52 @@ public static String dictParentKey(String type, Object value) { return PARENT_PREFIX + type + ":" + value; } + /** + * Redis Dict Value Hash Key + * + * @param value + * @return key + * @since 1.5.0 + */ + public static String dictKeyHash(DictValueVo value) { + return VALUE_PREFIX + value.getDictType(); + } + + /** + * 构建字典父级值缓存 KEY + * Redis Dict Value Hash Key + * + * @param value 字典值对象 + * @return 字典父级值缓存 KEY + * @since 1.5.0 + */ + public static String dictParentKeyHash(DictValueVo value) { + return PARENT_PREFIX + value.getDictType(); + } + + /** + * Redis Dict Value Hash Key + * + * @param value + * @return key + * @since 1.5.0 + */ + public static String dictKeyHash(String type, Object value) { + return VALUE_PREFIX + type; + } + + /** + * 构建字典父级值缓存 KEY + * + * @param type 字典类型 + * @param value 字典值 + * @return 字典父级值缓存 KEY + * @since 1.5.0 + */ + public static String dictParentKeyHash(String type, Object value) { + return PARENT_PREFIX + type; + } + /** * 转换列表的对象中含有字典文本翻译注解的字段信息 * diff --git a/src/main/java/com/houkunlin/system/dict/starter/store/RedisDictStore.java b/src/main/java/com/houkunlin/system/dict/starter/store/RedisDictStore.java index 9ec433a..98ccf6d 100644 --- a/src/main/java/com/houkunlin/system/dict/starter/store/RedisDictStore.java +++ b/src/main/java/com/houkunlin/system/dict/starter/store/RedisDictStore.java @@ -7,8 +7,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; +import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.ValueOperations; +import org.springframework.util.ObjectUtils; import java.util.Iterator; import java.util.List; @@ -49,24 +50,26 @@ public void storeSystemDict(DictTypeVo dictType) { @Override public void store(final Iterator iterator) { - final ValueOperations opsForValue = dictValueRedisTemplate.opsForValue(); + HashOperations opsedForHash = dictValueRedisTemplate.opsForHash(); + iterator.forEachRemaining(valueVo -> { - final String dictKey = DictUtil.dictKey(valueVo); + String dictKeyHash = DictUtil.dictKeyHash(valueVo); + String value = ObjectUtils.getDisplayString(valueVo.getValue()); final String title = valueVo.getTitle(); if (title == null) { - dictValueRedisTemplate.delete(dictKey); + opsedForHash.delete(dictKeyHash, value); if (logger.isDebugEnabled()) { - logger.debug("[removeDictValue] 字典值文本被删除 {}", dictKey); + logger.debug("[removeDictValue] 字典值文本被删除 {}#{}", dictKeyHash, value); } } else { - opsForValue.set(dictKey, title); + opsedForHash.put(dictKeyHash, value, title); // @since 1.4.6 - START - final String dictParentKey = DictUtil.dictParentKey(valueVo); + final String dictParentKeyHash = DictUtil.dictParentKeyHash(valueVo); final Object parentValue = valueVo.getParentValue(); if (parentValue == null) { - dictValueRedisTemplate.delete(dictParentKey); + opsedForHash.delete(dictParentKeyHash, value); } else { - opsForValue.set(dictParentKey, parentValue.toString()); + opsedForHash.put(dictParentKeyHash, value, parentValue.toString()); } // @since 1.4.6 - END } @@ -79,12 +82,10 @@ public void removeDictType(final String dictType) { if (logger.isDebugEnabled()) { logger.debug("[removeDictType] 字典类型被删除 {}", dictType); } - final String prefix = DictUtil.VALUE_PREFIX.concat(dictType); - final Set keys = dictValueRedisTemplate.keys(prefix + ":*"); - logger.debug("[removeDictType] 字典值文本被删除 {}", keys); - assert keys != null; - if (!keys.isEmpty()) { - dictValueRedisTemplate.delete(keys); + final String dictKeyHash = DictUtil.dictKeyHash(dictType, null); + dictValueRedisTemplate.delete(dictKeyHash); + if (logger.isDebugEnabled()) { + logger.debug("[removeDictType] 字典值文本被删除 {}", dictKeyHash); } } @@ -122,7 +123,7 @@ public String getDictText(final String type, final String value) { if (type == null || value == null) { return null; } - final String o = dictValueRedisTemplate.opsForValue().get(DictUtil.dictKey(type, value)); + final String o = dictValueRedisTemplate.opsForHash().get(DictUtil.dictKeyHash(type, value), value); if (o != null) { return o; } @@ -135,7 +136,7 @@ public String getDictParentValue(final String type, final String value) { if (type == null || value == null) { return null; } - return dictValueRedisTemplate.opsForValue().get(DictUtil.dictParentKey(type, value)); + return dictValueRedisTemplate.opsForHash().get(DictUtil.dictParentKeyHash(type, value), value); } @Override