forked from JoneXiong/PyRedisAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_api.py
125 lines (115 loc) · 3.6 KB
/
redis_api.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# coding=utf-8
import redis
import config
client = None
server_ip = None
db_index = None
def connect(*args, **kwargs):
""" 连接Redis数据库,参数和redis-py的Redis类一样 """
global client
client = redis.Redis(*args, **kwargs)
def get_client(*args, **kwargs):
global server_ip
global db_index
if args or kwargs:
if server_ip!=None and db_index!=None:
if kwargs['host']==server_ip and kwargs['db']==db_index:
pass
else:
print 'switch conn...'
connect(*args, **kwargs)
server_ip = kwargs['host']
db_index = kwargs['db']
else:
print 'init conn...'
connect(*args, **kwargs)
server_ip = kwargs['host']
db_index = kwargs['db']
global client
if client:
return client
else:
connect(host='127.0.0.1', port=6379)
return client
def get_tmp_client(*args, **kwargs):
from redis import Redis
return Redis(*args, **kwargs)
def get_all_keys_dict(client=None):
if client:
m_all = client.keys()
else:
m_all = get_client().keys()
m_all.sort()
me = {}
for key in m_all:
if len(key)>100:
continue
key_levels = key.split(config.base['seperator'])
cur = me
for lev in key_levels:
if cur.has_key(lev):
if cur.keys()==0:
cur[lev] = {lev:{}}#append(lev)
else:
cur[lev] = {}
cur = cur[lev]
return me
def get_all_keys_tree(client=None,key='*', cursor=0):
client = client or get_client()
key = key or '*'
if key=='*':
next_cursor,m_all = client.scan(cursor=cursor, match=key, count=config.scan_batch)
else:
key = '*%s*'%key
next_cursor,m_all = 0, client.keys(key)
m_all.sort()
me = {'root':{"pId": "0" ,"id": "root","name":"","count":0, "open":True}}
counter = 0
for key in m_all:
counter +=1
if counter>config.scan_batch:
break
if len(key)>100:
continue
key_levels = key.split(config.base['seperator'])
pre = 'root'
for lev in key_levels:
id = (pre!='root' and '%s%s%s'%(pre,config.base['seperator'],lev) or lev)
if me.has_key(id):
pre = id
else:
tar = {"pId": pre,"id": id,"name":lev,"count":0}
me[id] = tar
me[pre]["count"]+= 1
pre = id
ret = me.values()
for e in ret:
child_len = e['count']
fullkey = e['id']
if child_len==0:
m_len = 0
if config.show_key_self_count:
m_type = client.type(fullkey)
if not m_type:return
if m_type=='hash':
m_len = client.hlen(fullkey)
elif m_type=='list':
m_len = client.llen(fullkey)
elif m_type=='set':
m_len = len(client.smembers(fullkey))
elif m_type=='zset':
m_len = len(client.zrange(fullkey,0,-1))
else:
m_len = child_len
e['name'] = "%s <font color='#BFBFBF'>(%s)</font>"%(e['name'],m_len)
val_list = me.values()
val_list.append( (next_cursor,len(m_all) ) )
return val_list
def check_connect(host,port, password=None):
from redis import Connection
try:
conn = Connection(host=host, port=port, password=password)
conn.connect()
return True
except:
return False