Following is what I want to do, I guess it is right:
def put_data(name = hash_name, key = hash_key, value = hash_data): import redis r = Redis.get_connection() ttl = datetime.today() + timedelta(hours = 72) r.hset(name = name, key = hash_key, value = hash_data) r.expire(name = hash_name, time = ttl)
This is how:
A key is passively expired simply when some client tries to access it, and the key is found to be timed out.,Redis keys are expired in two ways: a passive way, and an active way.,Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command.,The EXPIRE family of commands is able to associate an expire to a given key, at the cost of some additional memory used by the key. When a key has an expire set, Redis will make sure to remove the key when the specified amount of time elapsed.
EXPIRE key seconds[NX | XX | GT | LT]
You can easily model this pattern in Redis using the following strategy: every time the user does a page view you call the following commands:
MULTI RPUSH pagewviews.user:<userid> http://..... EXPIRE pagewviews.user:<userid> 60 EXEC
Having keys that will expire after a certain amount of time can be useful to handle the cleanup of cached data. If you look through other chapters, you won’t see the use of key expiration in Redis often (except in sections 6.2, 7.1, and 7.2). This is mostly due to the types of structures that are used; few of the commands we use offer the ability to set the expiration time of a key automatically. And with containers (LISTs, SETs, HASHes, and ZSETs), we can only expire entire keys, not individual items (this is also why we use ZSETs with timestamps in a few places).,You can see a few examples of using expiration times on keys in the next listing.,Table 3.13 shows the list of commands that we use to set and check the expiration times of keys in Redis.,By continuing to use this site, you consent to our updated privacy agreement. You can change your cookie settings at any time but parts of our site will not function correctly without them.
>>> conn.set('key', 'value') True >>> conn.get('key') 'value'
>>> conn.expire('key', 2) True >>> time.sleep(2) >>> conn.get('key')
>>> conn.set('key', 'value2') True
>>> conn.expire('key', 100); conn.ttl('key') True 100
Following is the basic syntax of Redis Expire command.,First, create a key in Redis and set some value in it.,Redis - Configuration,Redis - Client Connection
Following is the basic syntax of Redis Expire command.
redis 127.0 .0 .1: 6379 > Expire KEY_NAME TIME_IN_SECONDS
First, create a key in Redis and set some value in it.
redis 127.0 .0 .1: 6379 > SET tutorialspoint redis OK
Now, set timeout of the previously created key.
redis 127.0 .0 .1: 6379 > EXPIRE tutorialspoint 60(integer) 1