• debing.sun's avatar
    Hash Field Expiration (#13303) · 7b9e9606
    debing.sun authored
    ## Background
    
    This PR introduces support for field-level expiration in Redis hashes. Previously, Redis supported expiration only at the key level, but this enhancement allows setting expiration times for individual fields within a hash.
    
    ## New commands
    * HEXPIRE
    * HEXPIREAT
    * HEXPIRETIME
    * HPERSIST
    * HPEXPIRE
    * HPEXPIREAT
    * HPEXPIRETIME
    * HPTTL
    * HTTL
    
    ## Short example
    from @moticless
    ```sh
    127.0.0.1:6379>  hset myhash f1 v1 f2 v2 f3 v3                                                   
    (integer) 3
    127.0.0.1:6379>  hpexpire myhash 10000 NX fields 2 f2 f3                                         
    1) (integer) 1
    2) (integer) 1
    127.0.0.1:6379>  hpttl myhash fields 3 f1 f2 f3                                                                                                                                                                         
    1) (integer) -1
    2) (integer) 9997
    3) (integer) 9997
    127.0.0.1:6379>  hgetall myhash  
    1) "f3"
    2) "v3"
    3) "f2"
    4) "v2"
    5) "f1"
    6) "v1"
    
    ... after 10 seconds ...
    
    127.0.0.1:6379>  hgetall myhash  
    1) "f1"
    2) "v1"
    127.0.0.1:6379>
    ```
    
    ## Expiration strategy
    1. Integrate active
        Redis periodically performs active expiration and deletion of hash keys that contain expired fields, with a maximum attempt limit.
    3. Lazy expiration
        When a client touches fields within a hash, Redis checks if the fields are expired. If a field is expired, it will be deleted. However, we do not delete expired fields during a traversal, we implicitly skip over them.
    
    ## RDB changes
    Add two new rdb type s`RDB_TYPE_HASH_METADATA` and `RDB_TYPE_HASH_LISTPACK_EX`.
    
    ## Notification
    1. Add `hpersist` notification for `HPERSIST` command.
    5. Add `hexpire` notification for `HEXPIRE`, `HEXPIREAT`, `HPEXPIRE` and `HPEXPIREAT` commands.
    
    ## Internal
    1. Add new data structure `ebuckets`, which is used to store TTL and keys, enabling quick retrieval of keys based on TTL.
    2. Add new data structure `mstr` like sds, which is used to store a string with TTL.
    
    This work was done by @moticless, @tezc, @ronen-kalish, @sundb, I just release it.
    7b9e9606
module.c 565 KB