龙空技术网

python操作redis——keys

Pygo 147

前言:

今天看官们对“pythonfromkeys”大约比较着重,你们都需要了解一些“pythonfromkeys”的相关知识。那么小编也在网络上汇集了一些有关“pythonfromkeys””的相关资讯,希望各位老铁们能喜欢,咱们一起来了解一下吧!

前面介绍了Linux客户端redis的keys操作以及连接认证的三个设置,那么python客户端怎么操作呢,我们来复现一下。

python 需要先 pip install redis

from redis import Redisif __name__ == '__main__':client=Redis(host='192.168.174.129',port=6666)# 各种操作……client.close()

注意上面返回的值类型是byte型:

keys=client.keys()print(keys)# [b'a3c', b'aBc', b'abc', b'a8c', b'abbc']print(type(keys[0]))# <class 'bytes'>

如果想返回值是字符串,可在连接参数中加一个decode_responses=True:

client=Redis(host='192.168.174.129',port=6666,decode_responses=True)

1. keys

keys = client.keys('a[A-C1-9]c')print(keys)# ['a3c', 'aBc', 'a8c']

2. dbsize

size=client.dbsize()print(size) # 5

3. exists key

exist=client.exists('abc')print(exist) # 1

4. del key

result = client.delete('a8c')print(result) # 1

5. type key

t = client.type('abc')print(t) # string

6. expire key seconds

result = client.expire('abc',400)print(result) # True# result = client.pexpire('a3c',1000)# result = client.expireat('abc',1607265348)# result = client.pexpireat('abc',1607265348.3004)

7. ttl key

time_remained = client.ttl('abc')print(time_remained) # 295

8. persist key

result = client.persist('abc')print(result) # True

标签: #pythonfromkeys