简体   繁体   English

可以检索在 redis-cli 中手动设置的值,但无法通过 Redis Reactive in Spring Boot 设置新密钥

[英]Can retrieve values manually set in redis-cli but unable to set new keys through Redis Reactive in Spring Boot

I'm using Spring Webflux + Reactive Redis, my goal is to use Redis as a file cache.我正在使用 Spring Webflux + Reactive Redis,我的目标是使用 Redis 作为文件缓存。

I was trying to set a key with a ~100MB ByteBuffer at first, didn't work.起初我试图设置一个 ~100MB ByteBuffer 的密钥,但没有奏效。 I double-checked with the debugger to make sure the file was actually being read into memory, and surely it was.我仔细检查了调试器,以确保文件确实被读入 memory,而且确实如此。 I thought "maybe Redis doesn't like "big" Strings?"我想“也许 Redis 不喜欢“大”字符串? so I tried with the code below, still no dice.所以我尝试了下面的代码,仍然没有骰子。 Thought maybe it could be an ACL related issue, but I checked and the default user has access to everything.以为可能是 ACL 相关问题,但我检查了默认用户可以访问所有内容。 "Maybe Spring can't access Redis?" “也许 Spring 无法访问 Redis?” nope, I checked the MONITOR output in redis-cli and the GET command is being received just fine, no signs of the SET command though.不,我在 redis-cli 中检查了 MONITOR output 并且收到了 GET 命令,但没有任何 SET 命令的迹象。 Any suggestions?有什么建议么?

Here's my controller:这是我的 controller:

@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()));
}

Relevant settings in application.properties: application.properties 中的相关设置:

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

redis-cli output (testkey was manually set in the CLI, no signs of testkey2): redis-cli output(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"

Edit: Forgot to mention that there's no stack traces nor any type of error being output to the console.编辑:忘了提到没有堆栈跟踪,也没有任何类型的错误是 output 到控制台。

Number one rule of reactive: nothing happens until you subscribe.反应式的第一条规则:在您订阅之前什么都不会发生。

While your code looks fine if you are looking at it from imperative perspective but it is not correct from a reactive point of view.如果您从命令式的角度来看,您的代码看起来不错,但从反应式的角度来看它是不正确的。

The reason you don't see the impact of the set command is that there is nothing subscribing to it.您看不到 set 命令的影响的原因是没有订阅它。

What you need to do is to join these two statements together like below:您需要做的是将这两个语句连接在一起,如下所示:

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

This way Spring will internally subscribe to the resulting Mono which in turn will subscribe to all the operators/steps (including the set command) in the chain.这样 Spring 将在内部订阅生成的Mono ,后者又将订阅链中的所有操作符/步骤(包括设置命令)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM