I'm looking to use the Hexpire command to have a particular field in a hash expire after a given amount of time. Suppose I have a hash with a key of user:123. What is the syntax to create an id field and have this field expire after say 30 seconds? I am using node-redis as my library and redis server version 7.4.1. Thanks for any help!
#Hexpire
2 messages · Page 1 of 1 (latest)
Here ya go:
import { createClient } from "redis";
const redis = await createClient().connect();
/* using pipelining */
redis.hSet("user:123", "id", "123");
await redis.hExpire("user:123", ["id"], 60);
/* using transactions */
await redis
.multi()
.hSet("user:123", "id", "123")
.hExpire("user:123", ["id"], 60)
.exec();
Note that the array of fields can be replaced with a single value if you only need to expire a single field.