针对永久不过期的key 批量设置过期时间
2024-08-21 10:15:35
问题需求:
redis内存暴增,后来发现有很多设置永久不过期。 解决:查找出来之后针对前缀批量设置过期时间 (过期时间与开发沟通 保证服务不受影响) 来源于网上杨一的代码 正好解决了我遇到的问题 在这里记录一下。 1 # encoding: utf-8 import redis
import random
import string
import time
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password="", db=0)
r = redis.Redis(connection_pool=pool) def random_str():
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(7)) def init_keys():
start_time = time.time()
for i in xrange(0, 20):
key_name = 'dba_'+str(i)
value_name = random_str()
r.set(key_name, value_name)
print 'initial keys successfully,use time:', time.time() - start_time def del_keys_without_pipe():
start_time = time.time()
result_length = 0
for key in r.scan_iter(match='rawdata_lastStatus_0015000200*', count=1000):
r.delete(key)
result_length += 1
print "normal ways end at:", time.time() - start_time
print "normal ways delete numbers:", result_length def del_keys_with_pipe():
start_time = time.time()
result_length = 0
pipe = r.pipeline()
for key in r.scan_iter(match='rawdata_*_otherTrades', count=1000):
pipe.expire(key,120)
result_length += 1
if result_length % 1000 == 0:
pipe.execute()
time.sleep(1)
pip_time = time.time()
print "use pipeline scan time ", time.time() - start_time
pipe.execute() print "use pipeline end at:", time.time() - pip_time
print "use pipeline ways delete numbers:", result_length def main():
# init_keys()
# del_keys_without_pipe()
# init_keys()
del_keys_with_pipe() if __name__ == '__main__':
main()
最新文章
- C#窗体文件的操作
- theano报一种float类型错误的处理办法
- Writing Your Own jQuery Plugins
- 使用Java正则表达式提取字符串中的数字一例
- node.js模块之Buffer模块
- Asp.net Mvc 自定义Session (二)
- NSUserDefaults偶尔/有时候保存数据会失败/失效
- pg_dump实例详解(备份postgresql和greenplum数据库)
- iscc2016 pwn部分writeup
- php时间戳与时间转换
- sql定时自动备份(定时作业)
- Redis数据类型之Hash(二)
- 透过源码看看Redis中如何计算QPS
- java 中的 instanceof
- RubyMine 2017.3.2破解教程
- Node.js实战项目学习系列(3) CommonJS 模块化规范
- [PA2014]Iloczyn
- 初识linux------用户和用户组
- Android MediaScanner 总纲
- python六剑客:map()、lambda()、filter()、reduce()、推导类表、切片