redis in python looks at the remaining expiration time and USES the regular wildcard to batch delete key

  • 2020-11-26 18:52:29
  • OfStack

The specific code is as follows:


# -*- coding: utf-8 -*-
import redis
import datetime
'''
# 1. redis Two ways to set the expiration time 
expire The function sets the expiration time to 10 Seconds. 10 Seconds later, ex1 Will the failure 
expireat Set up the 1 At a specific time, 15 years 9 month 8 day 15 point 19 points 10 Seconds, after that time, ex2 Will fail 
 This will be returned if the expiration time is set True, Conversely return False
'''
pool = redis.ConnectionPool(host='192.168.3.128', port=6379, db=0)
r = redis.StrictRedis(connection_pool=pool)
extime = datetime.datetime(2018, 7, 25, 15, 19, 10)
print r.expire('ex1', 10)
print extime.strftime('%Y-%m-%d %H:%M:%S %f')
print r.expireat('ex2', extime) #  Can be specified datetime And time stamp 
'''2.  View the remaining expiration time ( seconds )'''
print r.ttl('ex1')
print r.ttl('ex2')
print r.pttl('ex1') #  ms 
'''3. redis Bulk delete (wildcard) '''
print r.delete(*r.keys('/vender*')) #  delete  /vender All of the beginning name
def main():
  pass
if __name__ == '__main__':
  main()

conclusion


Related articles: