使用redis进行Spring引导缓存,key有xacxedx00x05tx00x06 [英] Spring boot caching with redis,key have xacxedx00x05tx00x06

查看:40
本文介绍了使用redis进行Spring引导缓存,key有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"

Spring 缓存

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:

  1. RedisCacheManager 定义为您自己的 bean.

  1. 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆