redis-cli INFO SERVER
# Server
redis_version:7.0.5
...
So in node caching some user data the problem having is that the ‘EX’ instead of setting as the expiration time in seconds is puting as field have the code as follow:
const resultCached = await redisClient.hset(
accessToken,
'user',
JSON.stringify({
'username' : username,
'passHash' : passHash,
'loginToken': accessTokenHash
}),
'EX',
7200
);
command to return all fields of this hash are can see the problem as returns "EX" as a field
redis-cli HGETALL '$2a$08$a9dv5bFwxsP0Z0eVUuw9XOOwNq85MvP5XHZbpw1cCgfvjlqeqQs0G'
1) "EX"
2) "7200"
3) "user"
4) "{"username":"xpto1","passHash":"$2a$08$MeJDVEAw.JKvuKKlC84gMuy0/dZZ/BOsoZSphxuPJZU.1Ro6W6tA.","loginToken":"$2a$08$5Pqd6WDZaw6.D71kbqciA.Bz1USDVzPYGqbnQ4BnQcDhZXwofL6La"}"
Wrong this should’t be a field but a config of the expiration of this key/data
~$ redis-cli HGET '$2a$08$a9dv5bFwxsP0Z0eVUuw9XOOwNq85MvP5XHZbpw1cCgfvjlqeqQs0G' 'EX'
"7200"
correct:
$ redis-cli HGET '$2a$08$a9dv5bFwxsP0Z0eVUuw9XOOwNq85MvP5XHZbpw1cCgfvjlqeqQs0G' 'user'
""username":"xpto1","passHash":"$2a$08$MeJDVEAw.JKvuKKlC84gMuy0/dZZ/BOsoZSphxuPJZU.1Ro6W6tA.","loginToken":"$2a$08$5Pqd6WDZaw6.D71kbqciA.Bz1USDVzPYGqbnQ4BnQcDhZXwofL6La"}"
my question what is the correct format to recongize as config param ("EX") expiration and not a extra field of this key (note in case of set
it works well as expected only in hset
having the problem)
2
Answers
Using a separate command
expire
works well still would prefer to make in single command if possible.You can run the
HSET
thenEX
commands in a single network round trip like this which will optimize things somewhat:In node-redis v4 and up, this will use Redis pipelining to send both commands to the server in the same round trip. https://redis.io/docs/manual/pipelining/
You can’t do this with a single command as there isn’t a
HSETEX
or equivalent command.