![](/img/trans.png)
[英]Equivalent of redis-cli MEMORY USAGE <key> in Spring Boot using redisTemplate
[英]Can retrieve values manually set in redis-cli but unable to set new keys through Redis Reactive in Spring Boot
我正在使用 Spring Webflux + Reactive Redis,我的目標是使用 Redis 作為文件緩存。
起初我試圖設置一個 ~100MB ByteBuffer 的密鑰,但沒有奏效。 我仔細檢查了調試器,以確保文件確實被讀入 memory,而且確實如此。 我想“也許 Redis 不喜歡“大”字符串? 所以我嘗試了下面的代碼,仍然沒有骰子。 以為可能是 ACL 相關問題,但我檢查了默認用戶可以訪問所有內容。 “也許 Spring 無法訪問 Redis?” 不,我在 redis-cli 中檢查了 MONITOR output 並且收到了 GET 命令,但沒有任何 SET 命令的跡象。 有什么建議么?
這是我的 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()));
}
application.properties 中的相關設置:
spring.redis.host=localhost
spring.redis.password=<password>
spring.redis.port=6379
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"
編輯:忘了提到沒有堆棧跟蹤,也沒有任何類型的錯誤是 output 到控制台。
反應式的第一條規則:在您訂閱之前什么都不會發生。
如果您從命令式的角度來看,您的代碼看起來不錯,但從反應式的角度來看它是不正確的。
您看不到 set 命令的影響的原因是沒有訂閱它。
您需要做的是將這兩個語句連接在一起,如下所示:
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())));
}
這樣 Spring 將在內部訂閱生成的Mono
,后者又將訂閱鏈中的所有操作符/步驟(包括設置命令)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.