使用redis进行Spring引导缓存,key有xacxedx00x05tx00x06 [英] Spring boot caching with redis,key have xacxedx00x05tx00x06
问题描述
我想使用 Spring 缓存 @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 服务器出现一个奇怪的key
the redis server appear a weird key
127.0.0.1:6379> keys *
1) "abc:xacxedx00x05tx00x03key"
127.0.0.1:6379> GET "abc:xacxedx00x05tx00x03key"
"xacxedx00x05tx00x05value"
weird-redis-key-with-spring-data-jedis一个>
如何像StringRedisTemplate一样设置@Cacheable的Serializer默认:
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
类一起使用.Spring 文档 说:
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
将自动配置.
If Redis is available and configured, the
RedisCacheManager
is auto-configured.
这是我建议的影响 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 上使用编程配置.还有一个 example 使用基于 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引导缓存,key有xacxedx00x05tx00x06的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!