For an already existing key, we can set its expiration time, after that time, when you visit again, key will not exist
There are two ways to set the expiration time, one is to specify how long the key will survive from the current time, the time unit has two, one is the second, the other is the millisecond
The second way is to specify the key to expire at a certain time, which needs to specify the date of the year and seconds, if you specify this time is earlier than the current time, then this key will immediately expire
Now, we now add two keys to Redis, Ex1 and EX2, respectively.
Then set their expiration time in two different ways
#Coding=utf-8" "Created on 2015-9-8 @author:" " ImportRedisImportdatetimeImportTime Pool=redis. ConnectionPool (host='192.168.1.126', port=6379,db=0) R= Redis. Strictredis (connection_pool=pool) Extime= Datetime.datetime (2015,9,8,15,19,10) PrintR.expire ('Ex1', 10) PrintExtime.strftime ('%y-%m-%d%h:%m:%s%f') PrintR.expireat ('EX2', Extime)
The expire function sets the expiration time to 10 seconds. After 10 seconds, the EX1 will expire.
Expireat set a specific time, September 8, 15 15:19 10 seconds, after this time, EX2 will expire
Returns true if the set expiration time succeeds, otherwise false
Python operation Redis Settings key expiration time