Redis python中HashMap的过期



我想在redis中存储以下哈希映射:

"data": {
       "name": "XYZ",
       "age": 22,
       "address": "a-z"
}  

我想让这整个哈希映射(包含给定的键)及时过期,比如72小时。

如何在python中使用ttl/expire函数?

谢谢,

以下是我想做的,我想这是对的:

def put_data(name, key, value):  
    import redis  
    r = Redis.get_connection()  
    ttl = datetime.today() + timedelta(hours=72)  
    r.hset(name=name, key=key, value=value)  
    r.expire(name=name, time=ttl)

是这样的:

redis_client.expire(your_key, time_in_seconds)

查看文档

假设您只想存储这些值而不是您在文章中显示的顶级字典您将使用hmset然后使用expire设置ttl或expire设置hashmap上的过期时间

最新更新