Skip to content

Commit

Permalink
feat: 缓存到 Redis 改为存储到 Redis Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
houkunlin committed Sep 27, 2023
1 parent f92d422 commit 0ed1080
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
46 changes: 46 additions & 0 deletions src/main/java/com/houkunlin/system/dict/starter/DictUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
* 转换列表的对象中含有字典文本翻译注解的字段信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,24 +50,26 @@ public void storeSystemDict(DictTypeVo dictType) {

@Override
public void store(final Iterator<DictValueVo> iterator) {
final ValueOperations<String, String> opsForValue = dictValueRedisTemplate.opsForValue();
HashOperations<String, String, String> opsedForHash = dictValueRedisTemplate.<String, String>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
}
Expand All @@ -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<String> 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);
}
}

Expand Down Expand Up @@ -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.<String, String>opsForHash().get(DictUtil.dictKeyHash(type, value), value);
if (o != null) {
return o;
}
Expand All @@ -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.<String, String>opsForHash().get(DictUtil.dictParentKeyHash(type, value), value);
}

@Override
Expand Down

0 comments on commit 0ed1080

Please sign in to comment.