相关疑难解决方法(0)

Spring Redis - 从application.properties文件中读取配置

我使用Spring Redis的工作spring-data-redis与所有默认配置喜欢localhost默认port等.

现在我尝试通过在application.properties文件中配置它来进行相同的配置.但我无法弄清楚我应该如何创建完全符合我的属性值的bean.

Redis配置文件

@EnableRedisHttpSession
@Configuration
public class SpringSessionRedisConfiguration {

@Bean
JedisConnectionFactory connectionFactory() {
    return new JedisConnectionFactory();
}

@Autowired
@Bean
RedisCacheManager redisCacheManager(final StringRedisTemplate stringRedisTemplate) {
    return new RedisCacheManager(stringRedisTemplate);
}

@Autowired
@Bean
StringRedisTemplate template(final RedisConnectionFactory connectionFactory) {
    return new StringRedisTemplate(connectionFactory);
}
}
Run Code Online (Sandbox Code Playgroud)

application.properties中的标准参数

spring.redis.sentinel.master =大师

spring.redis.sentinel.nodes = 192.168.188.231:26379

spring.redis.password = 12345

我试过的,

  1. 我可以使用@PropertySource然后注入@Value并获取值.但是我不想这样做,因为这些属性不是由我定义的,而是来自Spring.
  2. 在本文档Spring Redis文档中,它只表示可以使用属性进行配置,但不显示具体示例.
  3. 我还经历了Spring Data Redis API类,并发现RedisProperties应该对我有所帮助,但仍然无法弄清楚究竟如何告诉Spring从属性文件中读取.

java configuration spring redis

20
推荐指数
3
解决办法
5万
查看次数

使用redis进行Spring启动缓存,密钥有\ xac\xed\x00\x05t\x00\x06

我想使用Spring缓存@Cacheable来管理缓存.真正的缓存是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);
}
Run Code Online (Sandbox Code Playgroud)

我有帖子请求之后

本地主机:8080 /后键=键和值=价值?

redis服务器看起来很奇怪

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"
Run Code Online (Sandbox Code Playgroud)

Spring缓存

奇怪,Redis的琴键与弹簧-数据jedis

如何将@ Cacheable的Serializer设置为StringRedisTemplate默认值:

public StringRedisTemplate() {
    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    setKeySerializer(stringSerializer);
    setValueSerializer(stringSerializer);
    setHashKeySerializer(stringSerializer);
    setHashValueSerializer(stringSerializer);
}
Run Code Online (Sandbox Code Playgroud)

我的application.properties:

spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
Run Code Online (Sandbox Code Playgroud)

的build.gradle

group 'io.freezhan'
version '1.0-SNAPSHOT'

buildscript { …
Run Code Online (Sandbox Code Playgroud)

java spring caching redis

6
推荐指数
2
解决办法
7942
查看次数

Spring Data RedisTemplate:序列化Value和HashValue

我尝试了以下教程:http: //javakart.blogspot.in/2012/12/spring-data-redis-hello-world-example.html

我的问题与此有关: 奇怪的redis键与弹簧数据Jedis

我能够使用StringRedisSerializer解决密钥和hashkeys.

<bean 
id="stringRedisSerializer" 
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

<bean 
id="redisTemplate" 
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory" 
p:keySerializer-ref="stringRedisSerializer"
p:hashKeySerializer-ref="stringRedisSerializer" 
/>
Run Code Online (Sandbox Code Playgroud)

但是我发现使用序列化程序获取值和hashvalue是一个问题.

我尝试添加这个:

p:valueSerializer-ref="stringRedisSerializer"
p:hashValueSerializer-ref="stringRedisSerializer"
Run Code Online (Sandbox Code Playgroud)

但是提示错误:"用户无法转换为java.lang.String"

任何人都可以分享如何使用序列化器的值/ hashvalue?

spring serializer redis

5
推荐指数
1
解决办法
1万
查看次数

Spring-data-redis ping 有效,密钥存在,没有返回数据

这是我使用 spring-data-redis 的第一个应用程序,我认为我很好地理解了这些概念(过去我曾多次将 JdbcTemplate 与 RDBMS-es 一起使用)。这就是发生的事情......

我已经使用 JedisConnectionFactory 设置了 RedisTemplate,并且能够成功 ping Redis 服务器。然而,我无法从服务器获得最简单的数据响应,而且我担心我错过了一些到目前为止我无法从文档中推测出来的基本内容。

这是我的 bean.xml 文件的 Redis 部分:

<!-- Redis DAO stuff -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.url}" p:port="${redis.port}" p:database="0" />
Run Code Online (Sandbox Code Playgroud)

这是我的 RedisDAO 类中的相关代码部分:

@Autowired
private RedisTemplate<String, Object> template;

public String getTestVal() {
    logger.debug("getTestVal() function called++++++++++++++++++++");
    template.getConnectionFactory().getConnection().select(0);
    logger.debug("22222222222222");
    String pingResult = template.getConnectionFactory().getConnection().ping();
    logger.debug("333333333333333");
    logger.debug("REDIS PING RESULT: " + pingResult);
    logger.debug("444444444444444");
    logger.debug("HasKey Result: " + template.hasKey("akey"));
    logger.debug("555555555555555");
    //NAC;P3_TZ_380002878
    Object testVal = template.opsForValue().get("akey");
    logger.debug("TestVal returned from REdis: " …
Run Code Online (Sandbox Code Playgroud)

spring redis spring-data

2
推荐指数
1
解决办法
7484
查看次数

标签 统计

redis ×4

spring ×4

java ×2

caching ×1

configuration ×1

serializer ×1

spring-data ×1