zrevrange is not working in redis 4.0.10 server

140 views Asked by At

I have upgraded the Redis version to the latest version`4.6.7` in node.js application and the Redis server version is 4.0.10

zrevrange is deprecated in the latest Node-Redis version. I used zRange with the REV flag. It is working fine in Redis 6.x server but not in the Redis 4.x server.

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

It throws the syntax error like this [ErrorReply: ERR syntax error]

Is there any way to resolve it?

Thanks in Advance!

1

There are 1 answers

0
Simon Prickett On

Redis Server version 4 is very, very old and if possible you should upgrade to Redis 7 or 6.2 for a whole lot of good reasons - performance, security, features etc.

Node-Redis v4 allows you to run arbitrary commands with the sendCommand function. You can use this to call ZREVRANGE on your older Redis server like this by passing an array of strings to sendCommand representing the command you want to run as you would run it in 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();

Results of running this code:

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

Note that the response you get doesn't get transformed by Node Redis, so you'll get an array like response which is what the underlying Redis protocol response for this command looks like.