Skip to content

Commit

Permalink
feat: Redis 缓存存储只使用一个 RedisTemplate 对象,删除 StringRedisTemplate 的使用
Browse files Browse the repository at this point in the history
  • Loading branch information
houkunlin committed Sep 27, 2023
1 parent 0ed1080 commit 986c7ea
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ public class DictRedisConfiguration {
@Bean(DICT_REDIS_BEAN_NAME)
public RedisTemplate<String, DictTypeVo> dictTypeRedisTemplate(final RedisConnectionFactory connectionFactory) {
final RedisTemplate<String, DictTypeVo> redisTemplate = new RedisTemplate<>();
redisTemplate.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(DictTypeVo.class));
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setHashValueSerializer(RedisSerializer.string());
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/houkunlin/system/dict/starter/DictUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,21 @@ public static String dictParentKeyHash(DictValueVo value) {
/**
* Redis Dict Value Hash Key
*
* @param value
* @return key
* @since 1.5.0
*/
public static String dictKeyHash(String type, Object value) {
public static String dictKeyHash(String type) {
return VALUE_PREFIX + type;
}

/**
* 构建字典父级值缓存 KEY
*
* @param type 字典类型
* @param value 字典值
* @return 字典父级值缓存 KEY
* @since 1.5.0
*/
public static String dictParentKeyHash(String type, Object value) {
public static String dictParentKeyHash(String type) {
return PARENT_PREFIX + type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
Expand All @@ -21,11 +21,10 @@
*
* @author HouKunLin
*/
@AllArgsConstructor
@RequiredArgsConstructor
public class RedisDictStore implements DictStore, InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(RedisDictStore.class);
public final RedisTemplate<String, DictTypeVo> dictTypeRedisTemplate;
public final RedisTemplate<String, String> dictValueRedisTemplate;
private final RedisTemplate<String, DictTypeVo> redisTemplate;
private final RemoteDict remoteDict;

@Override
Expand All @@ -34,23 +33,23 @@ public void store(final DictTypeVo dictType) {
if (children == null) {
removeDictType(dictType.getType());
} else {
dictTypeRedisTemplate.opsForValue().set(DictUtil.dictKey(dictType.getType()), dictType);
redisTemplate.opsForValue().set(DictUtil.dictKey(dictType.getType()), dictType);
}
}

@Override
public void storeSystemDict(DictTypeVo dictType) {
final List<DictValueVo> children = dictType.getChildren();
if (children == null) {
dictTypeRedisTemplate.delete(DictUtil.dictSystemKey(dictType.getType()));
redisTemplate.delete(DictUtil.dictSystemKey(dictType.getType()));
} else {
dictTypeRedisTemplate.opsForValue().set(DictUtil.dictSystemKey(dictType.getType()), dictType);
redisTemplate.opsForValue().set(DictUtil.dictSystemKey(dictType.getType()), dictType);
}
}

@Override
public void store(final Iterator<DictValueVo> iterator) {
HashOperations<String, String, String> opsedForHash = dictValueRedisTemplate.<String, String>opsForHash();
HashOperations<String, String, String> opsedForHash = redisTemplate.opsForHash();

iterator.forEachRemaining(valueVo -> {
String dictKeyHash = DictUtil.dictKeyHash(valueVo);
Expand Down Expand Up @@ -78,28 +77,29 @@ public void store(final Iterator<DictValueVo> iterator) {

@Override
public void removeDictType(final String dictType) {
dictTypeRedisTemplate.delete(DictUtil.dictKey(dictType));
String dictKeyType = DictUtil.dictKey(dictType);
redisTemplate.delete(dictKeyType);
if (logger.isDebugEnabled()) {
logger.debug("[removeDictType] 字典类型被删除 {}", dictType);
logger.debug("[removeDictType] 字典类型被删除 {}", dictKeyType);
}
final String dictKeyHash = DictUtil.dictKeyHash(dictType, null);
dictValueRedisTemplate.delete(dictKeyHash);
final String dictKeyHashValue = DictUtil.dictKeyHash(dictType);
redisTemplate.delete(dictKeyHashValue);
if (logger.isDebugEnabled()) {
logger.debug("[removeDictType] 字典值文本被删除 {}", dictKeyHash);
logger.debug("[removeDictType] 字典值文本被删除 {}", dictKeyHashValue);
}
}

@Override
public Set<String> dictTypeKeys() {
final Set<String> keys = dictTypeRedisTemplate.keys(DictUtil.TYPE_PREFIX.concat("*"));
final Set<String> keys = redisTemplate.keys(DictUtil.TYPE_PREFIX.concat("*"));
assert keys != null;
final int length = DictUtil.TYPE_PREFIX.length();
return keys.stream().map(key -> key.substring(length)).collect(Collectors.toSet());
}

@Override
public Set<String> systemDictTypeKeys() {
final Set<String> keys = dictTypeRedisTemplate.keys(DictUtil.TYPE_SYSTEM_PREFIX.concat("*"));
final Set<String> keys = redisTemplate.keys(DictUtil.TYPE_SYSTEM_PREFIX.concat("*"));
assert keys != null;
final int length = DictUtil.TYPE_SYSTEM_PREFIX.length();
return keys.stream().map(key -> key.substring(length)).collect(Collectors.toSet());
Expand All @@ -110,7 +110,7 @@ public DictTypeVo getDictType(final String type) {
if (type == null) {
return null;
}
final DictTypeVo o = dictTypeRedisTemplate.opsForValue().get(DictUtil.dictKey(type));
final DictTypeVo o = redisTemplate.opsForValue().get(DictUtil.dictKey(type));
if (o != null) {
return o;
}
Expand All @@ -123,7 +123,7 @@ public String getDictText(final String type, final String value) {
if (type == null || value == null) {
return null;
}
final String o = dictValueRedisTemplate.<String, String>opsForHash().get(DictUtil.dictKeyHash(type, value), value);
final String o = redisTemplate.<String, String>opsForHash().get(DictUtil.dictKeyHash(type), value);
if (o != null) {
return o;
}
Expand All @@ -136,7 +136,7 @@ public String getDictParentValue(final String type, final String value) {
if (type == null || value == null) {
return null;
}
return dictValueRedisTemplate.<String, String>opsForHash().get(DictUtil.dictParentKeyHash(type, value), value);
return redisTemplate.<String, String>opsForHash().get(DictUtil.dictParentKeyHash(type), value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class RedisDictStoreConfiguration {
@ConditionalOnProperty(prefix = "system.dict", name = "store-type", havingValue = "AUTO", matchIfMissing = true)
@Bean
@ConditionalOnMissingBean
public DictStore dictStoreAuto(final RedisTemplate<String, DictTypeVo> redisTemplate1, final StringRedisTemplate redisTemplate2, final RemoteDict remoteDict) {
return new RedisDictStore(redisTemplate1, redisTemplate2, remoteDict);
public DictStore dictStoreAuto(final RedisTemplate<String, DictTypeVo> redisTemplate1, final RemoteDict remoteDict) {
return new RedisDictStore(redisTemplate1, remoteDict);
}

/**
Expand All @@ -38,7 +38,7 @@ public DictStore dictStoreAuto(final RedisTemplate<String, DictTypeVo> redisTemp
@ConditionalOnProperty(prefix = "system.dict", name = "store-type", havingValue = "REDIS")
@Bean
@ConditionalOnMissingBean
public DictStore dictStoreRedis(final RedisTemplate<String, DictTypeVo> redisTemplate1, final StringRedisTemplate redisTemplate2, final RemoteDict remoteDict) {
return new RedisDictStore(redisTemplate1, redisTemplate2, remoteDict);
public DictStore dictStoreRedis(final RedisTemplate<String, DictTypeVo> redisTemplate1, final RemoteDict remoteDict) {
return new RedisDictStore(redisTemplate1, remoteDict);
}
}

0 comments on commit 986c7ea

Please sign in to comment.