diff --git a/src/main/java/com/houkunlin/system/dict/starter/DictRedisConfiguration.java b/src/main/java/com/houkunlin/system/dict/starter/DictRedisConfiguration.java index dd32d36..b9e99e4 100644 --- a/src/main/java/com/houkunlin/system/dict/starter/DictRedisConfiguration.java +++ b/src/main/java/com/houkunlin/system/dict/starter/DictRedisConfiguration.java @@ -37,9 +37,13 @@ public class DictRedisConfiguration { @Bean(DICT_REDIS_BEAN_NAME) public RedisTemplate dictTypeRedisTemplate(final RedisConnectionFactory connectionFactory) { final RedisTemplate 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; } 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 0766bba..9e37679 100644 --- a/src/main/java/com/houkunlin/system/dict/starter/DictUtil.java +++ b/src/main/java/com/houkunlin/system/dict/starter/DictUtil.java @@ -253,11 +253,10 @@ 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; } @@ -265,11 +264,10 @@ public static String dictKeyHash(String type, Object value) { * 构建字典父级值缓存 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; } 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 98ccf6d..2617a87 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 @@ -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; @@ -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 dictTypeRedisTemplate; - public final RedisTemplate dictValueRedisTemplate; + private final RedisTemplate redisTemplate; private final RemoteDict remoteDict; @Override @@ -34,7 +33,7 @@ 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); } } @@ -42,15 +41,15 @@ public void store(final DictTypeVo dictType) { public void storeSystemDict(DictTypeVo dictType) { final List 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 iterator) { - HashOperations opsedForHash = dictValueRedisTemplate.opsForHash(); + HashOperations opsedForHash = redisTemplate.opsForHash(); iterator.forEachRemaining(valueVo -> { String dictKeyHash = DictUtil.dictKeyHash(valueVo); @@ -78,20 +77,21 @@ public void store(final Iterator 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 dictTypeKeys() { - final Set keys = dictTypeRedisTemplate.keys(DictUtil.TYPE_PREFIX.concat("*")); + final Set 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()); @@ -99,7 +99,7 @@ public Set dictTypeKeys() { @Override public Set systemDictTypeKeys() { - final Set keys = dictTypeRedisTemplate.keys(DictUtil.TYPE_SYSTEM_PREFIX.concat("*")); + final Set 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()); @@ -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; } @@ -123,7 +123,7 @@ public String getDictText(final String type, final String value) { if (type == null || value == null) { return null; } - final String o = dictValueRedisTemplate.opsForHash().get(DictUtil.dictKeyHash(type, value), value); + final String o = redisTemplate.opsForHash().get(DictUtil.dictKeyHash(type), value); if (o != null) { return o; } @@ -136,7 +136,7 @@ public String getDictParentValue(final String type, final String value) { if (type == null || value == null) { return null; } - return dictValueRedisTemplate.opsForHash().get(DictUtil.dictParentKeyHash(type, value), value); + return redisTemplate.opsForHash().get(DictUtil.dictParentKeyHash(type), value); } @Override diff --git a/src/main/java/com/houkunlin/system/dict/starter/store/RedisDictStoreConfiguration.java b/src/main/java/com/houkunlin/system/dict/starter/store/RedisDictStoreConfiguration.java index b5d2103..6f74fe6 100644 --- a/src/main/java/com/houkunlin/system/dict/starter/store/RedisDictStoreConfiguration.java +++ b/src/main/java/com/houkunlin/system/dict/starter/store/RedisDictStoreConfiguration.java @@ -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 redisTemplate1, final StringRedisTemplate redisTemplate2, final RemoteDict remoteDict) { - return new RedisDictStore(redisTemplate1, redisTemplate2, remoteDict); + public DictStore dictStoreAuto(final RedisTemplate redisTemplate1, final RemoteDict remoteDict) { + return new RedisDictStore(redisTemplate1, remoteDict); } /** @@ -38,7 +38,7 @@ public DictStore dictStoreAuto(final RedisTemplate redisTemp @ConditionalOnProperty(prefix = "system.dict", name = "store-type", havingValue = "REDIS") @Bean @ConditionalOnMissingBean - public DictStore dictStoreRedis(final RedisTemplate redisTemplate1, final StringRedisTemplate redisTemplate2, final RemoteDict remoteDict) { - return new RedisDictStore(redisTemplate1, redisTemplate2, remoteDict); + public DictStore dictStoreRedis(final RedisTemplate redisTemplate1, final RemoteDict remoteDict) { + return new RedisDictStore(redisTemplate1, remoteDict); } }