redispython源文件_Python redis模块详解

在使用 Redis、Codis 时,我们经常需要做一些批量操作,通过连接数据库批量对 key 进行操作:

常见的场景:

1.批量扫描key

2.获取info信息

3.获取client信息

4.设置配置参数

5.redis定期扫描

批量初始化redis 客户端:

1 fromredis import Redis2 def setExpiredKeys():3 try:4 if redis_pass == 'none':5 redisclient = Redis(host=redis_host, port=redis_port,db=0)6 else:7 redisclient = Redis(host=redis_host, port=redis_port, password=redis_pass)8 # 初始化 redis 客户端:redisclient9 # info = redisclient.info()10

11 except Exception ase:12 raise e13 if __name__ == '__main__':14 redis_host = '39.196.9.99'

15 redis_port = 6001

16 redis_pass = '123456'

17 setExpiredKeys()

详解:

1. Redis.info() 源代码:

返回结果:包含相关信息的字典

1 def info(self, section=None):2 """3 Returns a dictionary containing information about the Redis server4 """5 if section isNone:6 return self.execute_command('INFO')7 else:8 return self.execute_command('INFO', section)

1 info =redisclient.info() # 返回 Redis Server Info的所有信息2 info = redisclient.info('Keyspace') # 返回指定section的信息

1 output:2 {'db0': {'keys': 20005, 'expires': 5322, 'avg_ttl': 84036169}}

2. Redis.get() 源代码

获取key的value

1 def get(self, name):2 """3 Return the value at key ``name``, or None if the key doesn't exist

4 """5 return self.execute_command('GET', name)

1 info = redisclient.get('name') # 返回value:'Jack'

3. Redis.client_list() 源代码

1 def client_list(self, _type=None):2 """3 Returns a list of currently connected clients.4 If type of client specified, only that type will be returned.5 :param _type: optional. one of the client types (normal, master,6 replica, pubsub)7 """8 "Returns a list of currently connected clients"

9 if _type isnot None:10 client_types = ('normal', 'master', 'replica', 'pubsub')11 if str(_type).lower() not inclient_types:12 raise DataError("CLIENT LIST _type must be one of %r" %(13 client_types,))14 return self.execute_command('CLIENT LIST', b'TYPE', _type)15 return self.execute_command('CLIENT LIST')

1 info = redisclient.client_list() # 获取client列表

1 output:2 [3 {'id': '1319', 'addr': '127.0.0.1:48670', 'fd': '7', 'name': '', 'age': '5263', 'idle': '444', 'flags': 'N', 'db': '0', 'sub': '0', 'psub': '0', 'multi': '-1', 'qbuf': '0', 'qbuf-free': '0', 'obl': '0', 'oll': '0', 'omem': '0', 'events': 'r', 'cmd': 'client'},4 {'id': '1329', 'addr': '113.46.252.27:54863', 'fd': '8', 'name': '', 'age': '1', 'idle': '0', 'flags': 'N', 'db': '0', 'sub': '0', 'psub': '0', 'multi': '-1', 'qbuf': '0', 'qbuf-free': '32768', 'obl': '0', 'oll': '0', 'omem': '0', 'events': 'r', 'cmd': 'client'}5 ]

4. Redis.config_ 配置参数相关源代码:

1 def config_get(self, pattern="*"):2 "Return a dictionary of configuration based on the ``pattern``"

3 return self.execute_command('CONFIG GET', pattern)4

5 def config_set(self, name, value):6 "Set config item ``name`` with ``value``"

7 return self.execute_command('CONFIG SET', name, value)8 def config_rewrite(self):9 "Rewrite config file with the minimal change to reflect running config"

10 return self.execute_command('CONFIG REWRITE')

举例:内存扩容

1 info = redisclient.config_get('maxmemory')2 info1 = redisclient.config_set('maxmemory','100MB')3 info2 = redisclient.config_rewrite()

1 output:2 info : {'maxmemory': '104857600'}3 info1: True4 info2: b'OK'

5. Redis.scan_iter() 源代码

1 def scan_iter(self, match=None, count=None):2 """3 Make an iterator using the SCAN command so that the client doesn't

4 need to remember the cursor position.5 ``match`` allows forfiltering the keys by pattern6 ``count`` allows forhint the minimum number of returns7 """8 cursor = '0'

9 while cursor != 0:10 cursor, data = self.scan(cursor=cursor, match=match, count=count)11 for item indata:12 yield item

1 redisclient.scan_iter(count=500): # 每次扫描并返回500个key,

以上是Redis模块中常用的几个函数,能够有效提高工作效率!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值