wfansh 4 mesiacov pred
rodič
commit
a41e6dddbd

+ 4 - 2
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/redis/CacheConfig.java

@@ -15,6 +15,7 @@ import org.springframework.cache.interceptor.CacheAspectSupport;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Primary;
+import org.springframework.data.redis.cache.CacheKeyPrefix;
 import org.springframework.data.redis.cache.RedisCacheConfiguration;
 import org.springframework.data.redis.cache.RedisCacheWriter;
 import org.springframework.data.redis.connection.RedisConnectionFactory;
@@ -60,14 +61,15 @@ public class CacheConfig {
     /** 注册{@link CacheManager},支持在{@link Cacheable#cacheNames()}中设置Redis TTL */
     @Bean(name = TTL_CACHE_MANAGER)
     public CacheManager ttlCacheManager(RedisConnectionFactory connectionFactory) {
+        // 解决缓存对象转换异常问题
         ObjectMapper objectMapper = new ObjectMapper();
-        // 解决缓存对象转换异常的问题
         objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
         objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
 
-        // 配置序列化(解决乱码的问题),并配置缓存默认TTL
+        // 配置缓存默认TTL,以及键值序列化方式 - 解决乱码的问题
         RedisCacheConfiguration cacheConfiguration =
                 RedisCacheConfiguration.defaultCacheConfig()
+                        .computePrefixWith(CacheKeyPrefix.simple())
                         .entryTtl(Duration.ofHours(DEFAULT_TTL_HOURS))
                         .serializeKeysWith(
                                 SerializationPair.fromSerializer(new StringRedisSerializer()))

+ 4 - 16
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/redis/TTLCacheManager.java

@@ -1,14 +1,11 @@
 package org.jeecg.modules.redis;
 
-import static org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
-
 import lombok.extern.slf4j.Slf4j;
 
 import org.apache.commons.lang3.StringUtils;
 import org.jeecg.modules.adweb.dmp.service.impl.GACountryReportServiceImpl;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.data.redis.cache.*;
-import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
 
 import java.time.Duration;
 import java.util.Date;
@@ -26,28 +23,19 @@ import java.util.Date;
 public class TTLCacheManager extends RedisCacheManager {
 
     public TTLCacheManager(
-            RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
-        super(cacheWriter, defaultCacheConfiguration);
+            RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) {
+        super(cacheWriter, cacheConfiguration);
     }
 
     @Override
     protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfiguration) {
-        // 修改缓存key和value值的序列方式
-        cacheConfiguration =
-                cacheConfiguration
-                        .computePrefixWith(cacheName -> cacheName + ":")
-                        .serializeValuesWith(
-                                SerializationPair.fromSerializer(
-                                        new GenericJackson2JsonRedisSerializer()));
-
         final int idx = StringUtils.lastIndexOf(name, '#');
         if (idx > -1) {
-            final String ttl = StringUtils.substring(name, idx + 1);
+            long ttl = Long.parseLong(StringUtils.substring(name, idx + 1));
             name = StringUtils.substring(name, 0, idx);
 
             // 设置TTL
-            cacheConfiguration =
-                    cacheConfiguration.entryTtl(Duration.ofSeconds(Long.parseLong(ttl)));
+            cacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(ttl));
         }
 
         return super.createRedisCache(name, cacheConfiguration);