diff --git a/spring-boot-demo-cache-redis/README.md b/spring-boot-demo-cache-redis/README.md index 0dabb950c..2f3d58e8a 100644 --- a/spring-boot-demo-cache-redis/README.md +++ b/spring-boot-demo-cache-redis/README.md @@ -148,6 +148,18 @@ public class RedisConfig { template.setConnectionFactory(redisConnectionFactory); return template; } + + /** + * 配置使用注解的时候缓存配置,默认是序列化反序列化的形式,加上此配置则为 json 形式 + */ + @Bean + public CacheManager cacheManager(RedisConnectionFactory factory) { + // 配置序列化 + RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); + RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); + + return RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build(); + } } ``` @@ -327,7 +339,7 @@ public class UserServiceTest extends SpringBootDemoCacheRedisApplicationTests { */ @Test public void getAfterSave() { - userService.saveOrUpdate(new User(4L, "user4")); + userService.saveOrUpdate(new User(4L, "测试中文")); User user = userService.get(4L); log.debug("【user】= {}", user); diff --git a/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/cache/redis/config/RedisConfig.java b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/cache/redis/config/RedisConfig.java index cfd94048a..da777d0b6 100644 --- a/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/cache/redis/config/RedisConfig.java +++ b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/cache/redis/config/RedisConfig.java @@ -2,12 +2,17 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.io.Serializable; @@ -41,4 +46,16 @@ public RedisTemplate redisCacheTemplate(LettuceConnectionF template.setConnectionFactory(redisConnectionFactory); return template; } + + /** + * 配置使用注解的时候缓存配置,默认是序列化反序列化的形式,加上此配置则为 json 形式 + */ + @Bean + public CacheManager cacheManager(RedisConnectionFactory factory) { + // 配置序列化 + RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); + RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); + + return RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build(); + } } diff --git a/spring-boot-demo-cache-redis/src/test/java/com/xkcoding/cache/redis/service/UserServiceTest.java b/spring-boot-demo-cache-redis/src/test/java/com/xkcoding/cache/redis/service/UserServiceTest.java index 9d15219df..617594882 100644 --- a/spring-boot-demo-cache-redis/src/test/java/com/xkcoding/cache/redis/service/UserServiceTest.java +++ b/spring-boot-demo-cache-redis/src/test/java/com/xkcoding/cache/redis/service/UserServiceTest.java @@ -44,7 +44,7 @@ public void getTwice() { */ @Test public void getAfterSave() { - userService.saveOrUpdate(new User(4L, "user4")); + userService.saveOrUpdate(new User(4L, "测试中文")); User user = userService.get(4L); log.debug("【user】= {}", user); @@ -62,4 +62,4 @@ public void deleteUser() { userService.delete(1L); } -} \ No newline at end of file +}