node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

Unable to call expireTime(), expireAt() not working as expected,

Open tylert93 opened this issue 2 years ago • 3 comments

Description

In my code I want a particular to key to expire at the end of the day. I do this as follows:

const todayEnd = new Date().setHours(23, 59, 59, 999); await redis.expireAt(MY_KEY, todayEnd);

However the key does not expire at the end of the day as expected. I also get as error when I try to query the expiry time of my key as follows:

const expiryTime = await redis.expireTime(waveForecastDataKey);

which gives me the following error:

[ErrorReply: ERR unknown command EXPIRETIME, with args beginning with: MY_KEY, ]

I get the same error if I set an expiry time in seconds with the expire() function. This method however does cause the key to expire in the expected number of seconds.

Node.js Version

18.16.1

Redis Server Version

6.2.6

Node Redis Version

^4.6.7

Platform

macOS

Logs

[ErrorReply: ERR unknown command `EXPIRETIME`, with args beginning with: `waveForecastDataKey`, ]

tylert93 avatar Aug 08 '23 12:08 tylert93

Try formatting it in the "raw Redis commands" manner. I had the same problem with EXPIRE, and using the raw syntax worked for me.

From the docs:

There is built-in support for all of the out-of-the-box Redis commands. They are exposed using the raw Redis command names (HSET, HGETALL, etc.) and a friendlier camel-cased version (hSet, hGetAll, etc.):

// raw Redis commands await client.HSET('key', 'field', 'value'); await client.HGETALL('key');

// friendly JavaScript commands await client.hSet('key', 'field', 'value'); await client.hGetAll('key');

drewholowaty avatar Aug 08 '23 21:08 drewholowaty

Unfortunately I get the same error when using the raw EXPIRETIME command.

I am able to get a timestamp response when using the ttl function, however this always seems to wrong compared to what was set.

tylert93 avatar Aug 09 '23 07:08 tylert93

  1. EXPIRETIME was added in Redis 7.0... You can use the TTL command instead (which will return the remaining TTL for the key).
  2. I didn't play with it yet (I'll do it later), but I'm pretty sure that if the node server and the Redis server clocks are not synchronized (even if it's just a timezone difference) the expiration time will be off by the offset.

leibale avatar Aug 09 '23 12:08 leibale