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

前端 未结 11 2065
予麋鹿
予麋鹿 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:33

    You can. Here is an example.

    redis 127.0.0.1:6379> hset key f1 1
    (integer) 1
    redis 127.0.0.1:6379> hset key f2 2
    (integer) 1
    redis 127.0.0.1:6379> hvals key
    1) "1"
    2) "1"
    3) "2"
    redis 127.0.0.1:6379> expire key 10
    (integer) 1
    redis 127.0.0.1:6379> hvals key
    1) "1"
    2) "1"
    3) "2"
    redis 127.0.0.1:6379> hvals key
    1) "1"
    2) "1"
    3) "2"
    redis 127.0.0.1:6379> hvals key
    

    Use EXPIRE or EXPIREAT command.

    If you want to expire specific keys in the hash older then 1 month. This is not possible. Redis expire command is for all keys in the hash. If you set daily hash key, you can set a keys time to live.

    hset key-20140325 f1 1
    expire key-20140325 100
    hset key-20140325 f1 2
    

提交回复
热议问题