-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_set_ttl_by_pattern.py
63 lines (54 loc) · 1.25 KB
/
redis_set_ttl_by_pattern.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
给指定模式的key设置ttl
'''
__author__ = "liaojiacan"
import redis
import argparse
from random import randint
parser = argparse.ArgumentParser()
parser.add_argument(
"--host",
type=str,
required=True,
help="Redis host.")
parser.add_argument(
"--port",
type=int,
default="6379",
help="Redis port.")
parser.add_argument(
"--database",
type=int,
default="0",
help="Redis database.")
parser.add_argument(
'pattern',
help="key pattern for scan."
)
parser.add_argument(
'ttl',
metavar='ttl',
type=int,
nargs='+',
help="ttl range "
)
argv = parser.parse_args()
ttl_range = argv.ttl
ttl_min = ttl_range[0]
ttl_max = ttl_range[0] if len(ttl_range) == 1 else ttl_range[1]
r = redis.Redis(host=argv.host, port=argv.port,db=argv.database, decode_responses=True)
cursor_number = 0
pipe = r.pipeline()
while True:
cursor_number, keys = r.execute_command(
'scan', cursor_number, "match", argv.pattern, "count", 5000)
for key in keys:
ttl = randint(ttl_min, ttl_max)
pipe.expire(key,ttl)
print("EXPIRE ", key , ttl)
if len(keys) > 0 :
pipe.execute()
if cursor_number == '0':
break