How to “EXPIRE” the “HSET” child key in redis?

前端 未结 11 1958
予麋鹿
予麋鹿 2020-11-30 21:10

I need to expire all keys in redis hash, which are older than 1 month.

11条回答
  •  有刺的猬
    2020-11-30 21:22

    Regarding a NodeJS implementation, I have added a custom expiryTime field in the object I save in the HASH. Then after a specific period time, I clear the expired HASH entries by using the following code:

    client.hgetall(HASH_NAME, function(err, reply) {
        if (reply) {
            Object.keys(reply).forEach(key => {
                if (reply[key] && JSON.parse(reply[key]).expiryTime < (new Date).getTime()) {
                    client.hdel(HASH_NAME, key);
                }
            })
        }
    });
    

提交回复
热议问题