使用Spring + Jedis设置时,排序集如何存储在redis中?

问题描述 投票:0回答:1

我有一个 Spring 4.3.9 应用程序,带有 spring-data-redis (1.8.7) 和 jedis (2.9.0)。我可以使用以下代码轻松设置和检索 ZSET:

// Commented out -- but below line works fine too
// redisTemplate.opsForZSet().remove("score", userId);
Double scoreInRedis = redisTemplate.opsForZSet().score("score", userId);
redisTemplate.opsForZSet().add("score", userId, (double) score);

但是,当我进入 redis CLI 并尝试使用“score”键检索 ZSET 时,我没有得到任何返回。所以我尝试了以下命令:

ZCARD "score" <-- this should give number of items wi
(integer) 0
ZSCORE "score" userId <--> I use the actual number here for the userId
(nil)

其他命令如 ZREVRANGE 或 ZREVRANGEBYSCORE 都返回 (nil)。

我知道我的密钥已被设置,因为“信息密钥空间”显示密钥和过期时间之间的差异恰好为 1——这是我的分数 ZSET。如果我从 Spring 应用程序中删除 ZSET,则密钥和过期密钥的数量是相同的。所以我知道我的钥匙在某处。

老兄,我的 ZSET 呢?我如何通过 CLI 访问它?我可以轻松地继续开发,无需通过 CLI 访问,但我想了解我的情况。

spring redis jedis spring-data-redis
1个回答
0
投票

事实证明我错误地使用了

RedisTemplate<String, Long>
。我切换到基于
StringRedisTemplate
的 bean,神奇的是我的密钥现在在 CLI 上可见。

我仍然不确定使用另一个 bean 时我的密钥隐藏在哪里。

顺便说一句,我在处理此问题时遵循了这里的一些指导:https://medium.com/@smoothed9/redis-sorted-sets-with-spring-boot-and-redistemplate-66931e2e1b86

© www.soinside.com 2019 - 2024. All rights reserved.