使用Redis进行Spring Boot缓存,密钥为\ xac \ xed \ x00 \ x05t \ x00 \ x06 [英] Spring boot caching with redis,key have \xac\xed\x00\x05t\x00\x06
问题描述
我想使用Spring cache @Cacheable来管理缓存.而真正的缓存是redis.
I want to use Spring cache @Cacheable to manager cache. And the real cache is redis.
我的代码如下:
@PostMapping("/post")
@CachePut(value = "abc", key = "#key")
public String putInRedis(@RequestParam String key, @RequestParam String value) {
saveInDB(key, value);
return value;
}
@GetMapping("/get")
@Cacheable(value = "abc", key = "#key")
public String queryRedis(@RequestParam String key) {
return findByKey(key);
}
收到发帖请求后
localhost:8080/post?key = key& value = value
localhost:8080/post?key=key&value=value
redis服务器出现一个奇怪的密钥
the redis server appear a weird key
127.0.0.1:6379> keys *
1) "abc:\xac\xed\x00\x05t\x00\x03key"
127.0.0.1:6379> GET "abc:\xac\xed\x00\x05t\x00\x03key"
"\xac\xed\x00\x05t\x00\x05value"
weird-redis-key-with-spring-data-jedis
如何将@Cacheable的Serializer设置为StringRedisTemplate默认值:
how to set @Cacheable's Serializer like StringRedisTemplate default:
public StringRedisTemplate() {
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
setKeySerializer(stringSerializer);
setValueSerializer(stringSerializer);
setHashKeySerializer(stringSerializer);
setHashValueSerializer(stringSerializer);
}
我的application.properties:
my application.properties:
spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
build.gradle
build.gradle
group 'io.freezhan'
version '1.0-SNAPSHOT'
buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.13'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}
apply plugin: 'java'
apply plugin: 'spring-boot'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-data-redis")
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile 'org.projectlombok:lombok:1.16.10'
testCompile("junit:junit")
}
推荐答案
Spring的缓存功能允许使用不同的缓存实现.Redis是其中之一.可以与 RedisCacheManager
类一起使用.
The caching - feature of Spring allows to use different cache - implementations. One of them is Redis. It can be used with the class RedisCacheManager
. The Spring documentation says:
如果Redis可用并已配置,则会自动配置
RedisCacheManager
.
这是我建议影响Redis的方法-缓存-集成:
This is the approach that I propose to influence the Redis - caching - integration:
-
将
RedisCacheManager
定义为自己的bean.
Define the
RedisCacheManager
as bean on your own.
将 RedisTemplate
传递给 RedisCacheManager
的构造函数.
我找到了一个示例为此,可以使用编程配置在Internet上进行.还有一个使用基于XML的配置的示例.
I found an example for this on the Internet using a programmmatic configuration. There is also an example using XML-based configuration.
这篇关于使用Redis进行Spring Boot缓存,密钥为\ xac \ xed \ x00 \ x05t \ x00 \ x06的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!