首页 > 其他 > 详细

针对永久不过期的key 批量设置过期时间

时间:2019-10-09 15:10:13      阅读:112      评论:0      收藏:0      [点我收藏+]
问题需求:
  redis内存暴增,后来发现有很多设置永久不过期。

解决:查找出来之后针对前缀批量设置过期时间 (过期时间与开发沟通 保证服务不受影响)

来源于网上杨一的代码 正好解决了我遇到的问题 在这里记录一下。

1
# encoding: utf-8 2 3 import redis 4 import random 5 import string 6 import time 7 pool = redis.ConnectionPool(host=127.0.0.1, port=6379, password="123456", db=0) 8 r = redis.Redis(connection_pool=pool) 9 10 def random_str(): 11 return ‘‘.join(random.choice(string.ascii_letters + string.digits) for _ in range(7)) 12 13 14 def init_keys(): 15 start_time = time.time() 16 for i in xrange(0, 20): 17 key_name = dba_+str(i) 18 value_name = random_str() 19 r.set(key_name, value_name) 20 print initial keys successfully,use time:, time.time() - start_time 21 22 23 def del_keys_without_pipe(): 24 start_time = time.time() 25 result_length = 0 26 for key in r.scan_iter(match=rawdata_lastStatus_0015000200*, count=1000): 27 r.delete(key) 28 result_length += 1 29 print "normal ways end at:", time.time() - start_time 30 print "normal ways delete numbers:", result_length 31 32 33 def del_keys_with_pipe(): 34 start_time = time.time() 35 result_length = 0 36 pipe = r.pipeline() 37 for key in r.scan_iter(match=rawdata_*_otherTrades, count=1000): 38 pipe.expire(key,120) 39 result_length += 1 40 if result_length % 1000 == 0: 41 pipe.execute() 42 time.sleep(1) 43 pip_time = time.time() 44 print "use pipeline scan time ", time.time() - start_time 45 pipe.execute() 46 47 print "use pipeline end at:", time.time() - pip_time 48 print "use pipeline ways delete numbers:", result_length 49 50 51 def main(): 52 # init_keys() 53 # del_keys_without_pipe() 54 # init_keys() 55 del_keys_with_pipe() 56 57 58 if __name__ == __main__: 59 main()

 

针对永久不过期的key 批量设置过期时间

原文:https://www.cnblogs.com/soilge/p/11641301.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!