具體代碼如下所示:
# -*- coding: utf-8 -*-import redisimport datetime'''# 1. redis設(shè)置過期時(shí)間的兩種方式expire函數(shù)設(shè)置過期時(shí)間為10秒。10秒后,ex1將會(huì)失效expireat設(shè)置一個(gè)具體的時(shí)間,15年9月8日15點(diǎn)19分10秒,過了這個(gè)時(shí)間,ex2將失效如果設(shè)置過期時(shí)間成功會(huì)返回True,反之返回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) # 可指定datetime和時(shí)間戳'''2. 查看剩余過期時(shí)間(秒)'''print r.ttl('ex1')print r.ttl('ex2')print r.pttl('ex1') # 毫秒'''3. redis批量刪除(通配符)'''print r.delete(*r.keys('/vender*')) # 刪除 /vender開頭的所有namedef main(): passif __name__ == '__main__': main()
總結(jié)
以上所述是小編給大家介紹的python中redis查看剩余過期時(shí)間及用正則通配符批量刪除key的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林站長站網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選