zrevrange在redis 4.0.10服务器中不工作

uyto3xhc  于 9个月前  发布在  Redis
关注(0)|答案(1)|浏览(84)

我已经在node.js应用程序中将Redis版本升级到最新版本4.6.7,Redis服务器版本为4.0.10
zrevrange在最新的Node-Redis版本中被弃用。我使用zRange和REV标志。它在Redis 6.x服务器上工作正常,但在Redis 4.x服务器上却不行。

client.zRange( key , startIndex , stopIndex , {'REV': true} );

它抛出如下语法错误[ErrorReply: ERR syntax error]
有什么办法解决吗?
提前感谢!

xuo3flqw

xuo3flqw1#

Redis Server版本4非常非常旧,如果可能的话,你应该升级到Redis 7或6.2,因为有很多好的理由-性能,安全性,功能等。
Node-Redis v4允许您使用sendCommand函数运行任意命令。你可以在旧版Redis服务器上调用ZREVRANGE,方法是向sendCommand传递一个字符串数组,表示你想在redis-cli中运行的命令:
package.json

{
  "type": "module",
  "main": "index.js",
  "dependencies": {
    "redis": "^4.6.8"
  }
}

index.js

import { createClient } from 'redis';

const client = createClient();

await client.connect();

await client.zAdd('mysortedset', [
  {
    score: 99,
    value: 'Ninety Nine'
  },
  {
    score: 100,
    value: 'One Hundred'
  },
  {
    score: 101,
    value: 'One Hundred and One'
  }
]);

const response = await client.sendCommand(['ZREVRANGE', 'mysortedset', '0', '-1', 'WITHSCORES']);

console.log(response);

await client.quit();

运行此代码的结果:

$ node index.js
[
  'One Hundred and One',
  '101',
  'One Hundred',
  '100',
  'Ninety Nine',
  '99'
]

注意,你得到的响应不会被Node Redis转换,所以你会得到一个类似数组的响应,这就是这个命令的底层Redis协议响应的样子。

相关问题