【问题标题】:Can retrieve values manually set in redis-cli but unable to set new keys through Redis Reactive in Spring Boot可以检索在 redis-cli 中手动设置的值,但无法通过 Spring Boot 中的 Redis Reactive 设置新键
【发布时间】:2021-04-16 22:13:27
【问题描述】:

我正在使用 Spring Webflux + Reactive Redis,我的目标是使用 Redis 作为文件缓存。

一开始我试图设置一个 ~100MB ByteBuffer 的密钥,但没有成功。我仔细检查了调试器,以确保文件确实被读入内存,而且确实如此。我想“也许 Redis 不喜欢“大”字符串?所以我尝试了下面的代码,仍然没有骰子。以为可能是 ACL 相关问题,但我检查了默认用户可以访问所有内容。 “也许Spring无法访问Redis?”不,我检查了 redis-cli 中的 MONITOR 输出,并且接收到了 GET 命令,但没有任何 SET 命令的迹象。有什么建议吗?

这是我的控制器:

@RequestMapping(value = "/prime", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Mono<String> prime() {
    reactiveStringCommands.set(ByteBuffer.wrap("testkey2".getBytes()), ByteBuffer.wrap("test".getBytes()));

    return reactiveStringCommands.get(ByteBuffer.wrap("testkey".getBytes())).map(bb -> new String(bb.array()));
}

application.properties 中的相关设置:

spring.redis.host=localhost
spring.redis.password=<password>
spring.redis.port=6379

redis-cli 输出(testkey 是在 CLI 中手动设置的,没有 testkey2 的迹象):

127.0.0.1:6379> keys *
1) "testkey"
127.0.0.1:6379> ACL list
1) "user default on #<password> ~* +@all"
127.0.0.1:6379> monitor
OK
1610406175.250768 [0 172.17.0.1:39104] "GET" "testkey"

编辑:忘了提到没有堆栈跟踪,也没有任何类型的错误输出到控制台。

【问题讨论】:

  • 请发布您得到的堆栈跟踪,而不是仅仅说 .it didn’t work
  • @Toerktumlare 我没有包含它们,因为我没有得到任何东西,控制台输出中没有错误。抱歉,我应该在主帖中提到这一点

标签: java spring spring-boot spring-webflux spring-data-redis-reactive


【解决方案1】:

响应式的第一条规则:在您订阅之前什么都不会发生。

如果您从命令式的角度来看,您的代码看起来不错,但从反应式的角度来看它是不正确的。

您看不到 set 命令的影响的原因是没有订阅它。

您需要做的是将这两个语句连接在一起,如下所示:

public Mono<String> prime() {
    return reactiveStringCommands.set(ByteBuffer.wrap("testkey2".getBytes()), ByteBuffer.wrap("test".getBytes()))
            .then(reactiveStringCommands.get(ByteBuffer.wrap("testkey".getBytes())).map(bb -> new String(bb.array())));
}

这样,Spring 将在内部订阅生成的Mono,而Mono 又将订阅链中的所有操作符/步骤(包括 set 命令)。

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2020-07-04
    相关资源
    最近更新 更多