当前位置:   article > 正文

Java中redis设置过期时间_Redis的hashes如何设置过期时间

java jedis hash expire

你需要的不是答案,你需要的是鼓励。:)

Unfortunately, no. Redis' "containers" (i.e. lists, hashes, sets and sorted sets) do not support per-member expiry, although this functionality has been requested many times in the past.

You can, however, implement your own logic to achieve that result. There are several possible approaches to address this - here's one example. Instead of using a set, use a sorted set (ZSET) and set each member's score to its expiry time using epoch values. This type of workflow could be implemented using a Lua script for example. To add members use something like:

redis.call('zadd', KEYS[1], os.time()+ARGV[1], ARGV[2])

and EVAL it using '1 a 60 1' and '1 a 120 2' as arguments, per your example. To actually "expire" the items from the set, you'll need to delete them once their time has passed. You can do that either by implementing a periodical process that scans your list or upon accessing it. For example, the following Lua can be used to expire members:

redis.call('zremrangebyscore', KEYS[1], '-inf', os.time())

and EVAL it using '1 a' as arguments per your example.

EDIT: How to achieve the above using Python

import time

import redis

def add(r, key, ttl, member):

r.zadd(key, member, int(time.time()+ttl))

def expire(r, key):

r.zremrangebyscore(key, '-inf', int(time.time()))

...

r = redis.Redis()

add(r, 'a', 1, 60)

add(r, 'a', 2, 120)

periodically or before every operation do

expire(r, 'a')

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/695101
推荐阅读
相关标签
  

闽ICP备14008679号