Python获取Redis所有Key以及内容的方法

时间:2022-11-09 13:13:54

一、获取所有Key

?
1
2
3
4
5
6
7
8
9
10
# -*- encoding: UTF-8 -*-
__author__ = "Sky"
import redis
 
pool=redis.ConnectionPool(host='127.0.0.1',port=6379,db=0)
r = redis.StrictRedis(connection_pool=pool)
 
keys = r.keys()
print type(keys)
print keys

运行结果:

?
1
2
<type 'list'>
['fad', '1', '2']

二、获取所有内容

?
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
import redis
 
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
 
pipe = r.pipeline()
pipe_size = 100000
 
len = 0
key_list = []
print r.pipeline()
keys = r.keys()
for key in keys:
  key_list.append(key)
  pipe.get(key)
  if len < pipe_size:
    len += 1
  else:
    for (k, v) in zip(key_list, pipe.execute()):
      print k, v
    len = 0
    key_list = []
 
for (k, v) in zip(key_list, pipe.execute()):
  print k, v

运行结果:

?
1
2
3
fad fda
1 e
2 f

以上这篇Python获取Redis所有Key以及内容的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/konglongaa/article/details/78946095