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

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

    You could store key/values in Redis differently to achieve this, by just adding a prefix or namespace to your keys when you store them e.g. "hset_"

    • Get a key/value GET hset_key equals to HGET hset key

    • Add a key/value SET hset_key value equals to HSET hset key

    • Get all keys KEYS hset_* equals to HGETALL hset

    • Get all vals should be done in 2 ops, first get all keys KEYS hset_* then get the value for each key

    • Add a key/value with TTL or expire which is the topic of question:

     SET hset_key value
     EXPIRE hset_key
    

    Note: KEYS will lookup up for matching the key in the whole database which may affect on performance especially if you have big database.

    Note:

    • KEYS will lookup up for matching the key in the whole database which may affect on performance especially if you have big database. while SCAN 0 MATCH hset_* might be better as long as it doesn't block the server but still performance is an issue in case of big database.

    • You may create a new database for storing separately these keys that you want to expire especially if they are small set of keys.

    Thanks to @DanFarrell who highlighted the performance issue related to KEYS

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-30 21:36

    We had the same problem discussed here.

    We have a Redis hash, a key to hash entries (name/value pairs), and we needed to hold individual expiration times on each hash entry.

    We implemented this by adding n bytes of prefix data containing encoded expiration information when we write the hash entry values, we also set the key to expire at the time contained in the value being written.

    Then, on read, we decode the prefix and check for expiration. This is additional overhead, however, the reads are still O(n) and the entire key will expire when the last hash entry has expired.

    0 讨论(0)
  • 2020-11-30 21:45

    You could use the Redis Keyspace Notifications by using psubscribe and "__keyevent@<DB-INDEX>__:expired".

    With that, each time that a key will expire, you will get a message published on your redis connection.

    Regarding your question basically you create a temporary "normal" key using set with an expiration time in s/ms. It should match the name of the key that you wish to delete in your set.

    As your temporary key will be published to your redis connection holding the "__keyevent@0__:expired" when it expired, you can easily delete your key from your original set as the message will have the name of the key.

    A simple example in practice on that page : https://medium.com/@micah1powell/using-redis-keyspace-notifications-for-a-reminder-service-with-node-c05047befec3

    doc : https://redis.io/topics/notifications ( look for the flag xE)

    0 讨论(0)
  • 2020-11-30 21:46

    You can expire Redis hashes in ease, Eg using python

    import redis
    conn = redis.Redis('localhost')
    conn.hmset("hashed_user", {'name': 'robert', 'age': 32})
    conn.expire("hashed_user", 10)
    

    This will expire all child keys in hash hashed_user after 10 seconds

    same from redis-cli,

    127.0.0.1:6379> HMSET testt username wlc password P1pp0 age 34
    OK
    127.0.0.1:6379> hgetall testt
    1) "username"
    2) "wlc"
    3) "password"
    4) "P1pp0"
    5) "age"
    6) "34"
    127.0.0.1:6379> expire testt 10
    (integer) 1
    127.0.0.1:6379> hgetall testt
    1) "username"
    2) "wlc"
    3) "password"
    4) "P1pp0"
    5) "age"
    6) "34"
    

    after 10 seconds

    127.0.0.1:6379> hgetall testt
    (empty list or set)
    
    0 讨论(0)
提交回复
热议问题