com.houkunlin.system.dict.starter.store.RedisDictStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of system-dict-starter Show documentation
Show all versions of system-dict-starter Show documentation
系统数据字典自动翻译成字典文本。可集合系统数据库中存储的用户数据字典,也可使用枚举做系统数据字典,主要用在返回数据给前端时自动把字典值翻译成字典文本信息;
The system data dictionary is automatically translated into dictionary text.
The user data dictionary stored in the system database can be aggregated, and the enumeration can also be used as the system data dictionary.
It is mainly used to automatically translate dictionary values into dictionary text information when returning data to the front end.
The newest version!
package com.houkunlin.system.dict.starter.store;
import com.houkunlin.system.dict.starter.DictUtil;
import com.houkunlin.system.dict.starter.bean.DictTypeVo;
import com.houkunlin.system.dict.starter.bean.DictValueVo;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.connection.RedisHashCommands;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.ObjectUtils;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* 当存在 Redis 环境时使用 Redis 来存储系统字典信息,否则使用本地存储。
*
* @author HouKunLin
*/
@Data
@RequiredArgsConstructor
public class RedisDictStore implements DictStore, InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(RedisDictStore.class);
private final RedisTemplate redisTemplate;
private final RemoteDict remoteDict;
/**
* Redis 批量数据写入时,每 1000 条字典数据提交一次
*/
private int batchSize = 1000;
@Override
public void store(final DictTypeVo dictType) {
final List children = dictType.getChildren();
if (children == null) {
removeDictType(dictType.getType());
} else {
redisTemplate.opsForValue().set(DictUtil.dictKey(dictType.getType()), dictType);
}
}
@Override
public void storeSystemDict(DictTypeVo dictType) {
final List children = dictType.getChildren();
if (children == null) {
redisTemplate.delete(DictUtil.dictSystemKey(dictType.getType()));
} else {
redisTemplate.opsForValue().set(DictUtil.dictSystemKey(dictType.getType()), dictType);
}
}
@Override
public void store(final Iterator iterator) {
HashOperations opsedForHash = redisTemplate.opsForHash();
iterator.forEachRemaining(valueVo -> {
String dictKeyHash = DictUtil.dictKeyHash(valueVo);
String value = ObjectUtils.getDisplayString(valueVo.getValue());
final String title = valueVo.getTitle();
if (title == null) {
opsedForHash.delete(dictKeyHash, value);
if (logger.isDebugEnabled()) {
logger.debug("[removeDictValue] 字典值文本被删除 {}#{}", dictKeyHash, value);
}
} else {
opsedForHash.put(dictKeyHash, value, title);
// @since 1.4.6 - START
final String dictParentKeyHash = DictUtil.dictParentKeyHash(valueVo);
final Object parentValue = valueVo.getParentValue();
if (parentValue == null) {
opsedForHash.delete(dictParentKeyHash, value);
} else {
opsedForHash.put(dictParentKeyHash, value, parentValue.toString());
}
// @since 1.4.6 - END
}
});
}
@Override
public void storeBatch(final Iterator iterator) {
RedisSerializer keySerializer = (RedisSerializer) redisTemplate.getKeySerializer();
RedisSerializer hashKeySerializer = (RedisSerializer) redisTemplate.getHashKeySerializer();
RedisSerializer hashValueSerializer = (RedisSerializer) redisTemplate.getHashValueSerializer();
redisTemplate.executePipelined((RedisCallback