Python进阶学习_链接操做Redis数据库

安装导入第三方模块Redis

pip3 install redis

import redis 

操做String类型

""" redis 基本命令 String set(name, value, ex=None, px=None, nx=False, xx=False) 在 Redis 中设置值,默认,不存在则建立,存在则修改。 参数: ex - 过时时间(秒) px - 过时时间(毫秒) nx - 若是设置为True,则只有name不存在时,当前set操做才执行 xx - 若是设置为True,则只有name存在时,当前set操做才执行 redis 取出的结果默认是字节,咱们能够设定 decode_responses=True 改为字符串。 """ redis = redis.Redis(host="116.62.13.104", port=6379, decode_responses=True) # python-k1 表明key; hello 表明 value; ex表明seconds;px表明ms redis.set("python-k1","hello",ex=120) # 获取值的第一种方式 使用 redis.get("key") print(redis.get("python-k1"),type(redis.get("python-k1"))) # 获取值的第二种方式 直接使用 redis['key'] print(redis['python-k1'],type(redis['python-k1'])) 

在这里插入图片描述

链接池

redis-py 使用 connection pool 来管理对一个 redis server 的全部链接,避免每次创建、释放链接的开销。python

""" 链接池 redis-py 使用 connection pool 来管理对一个 redis server 的全部链接,避免每次创建、释放链接的开销。 默认,每一个Redis实例都会维护一个本身的链接池。能够直接创建一个链接池,而后做为参数 Redis,这样就能够实现多个 Redis 实例共享一个链接池。 """ pool = redis.ConnectionPool(host='116.62.13.104', port=6379, decode_responses=True) redis = redis.Redis(connection_pool=pool) 

完整代码

# author: LiuShihao # data: 2020/12/3 2:59 下午 # youknow: 各位老铁,个人这套代码曾经有人出价三个亿我没有卖,现在拿出来和你们分享,不求别的,只求你们免费的小红心帮忙点一点,这里谢过了。 # desc: Python操做Redis """ 经过 get()、set() 操做redis字符型数据; 经过 hset()、hget() 操做redis哈希类型数据 经过 json.dumps() 和 json.loads() 能够实现python中的字典数据的序列化和反序列化; """ import redis import traceback # redis = redis.Redis(host="116.62.13.104", port=6379, decode_responses=True) """ 链接池 redis-py 使用 connection pool 来管理对一个 redis server 的全部链接,避免每次创建、释放链接的开销。 默认,每一个Redis实例都会维护一个本身的链接池。能够直接创建一个链接池,而后做为参数 Redis,这样就能够实现多个 Redis 实例共享一个链接池。 """ pool = redis.ConnectionPool(host='116.62.13.104', port=6379, decode_responses=True) redis = redis.Redis(connection_pool=pool) """ redis 基本命令 String set(name, value, ex=None, px=None, nx=False, xx=False) 在 Redis 中设置值,默认,不存在则建立,存在则修改。 参数: ex - 过时时间(秒) px - 过时时间(毫秒) nx - 若是设置为True,则只有name不存在时,当前set操做才执行 xx - 若是设置为True,则只有name存在时,当前set操做才执行 redis 取出的结果默认是字节,咱们能够设定 decode_responses=True 改为字符串。 # 获取值的第一种方式 使用 redis.get("key") print(redis.get("python-k1"),type(redis.get("python-k1"))) # 获取值的第二种方式 直接使用 redis['key'] print(redis['python-k1'],type(redis['python-k1'])) """ # 获取全部的key名 def findAllKeys(): keyList = redis.keys() for item in keyList: print(item) # 获取全部的键值对 def findAllKeyAndValue(): # keyAndValue = [] keyList = redis.keys() for item in keyList: print(item) value = redis[item] # d=dict(item=value) d= {item:value} print(d) # keyAndValue.append(d) if __name__ == '__main__': # redis.set('PK2','PV2') # value = redis.get('PK2') # print('值:',value) findAllKeyAndValue() # print("键值对:",keyAndValue) 

Bug redis.exceptions.ResponseError

在这里插入图片描述
报错信息:linux

redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error. 

 

报错缘由:
强制把redis快照关闭了致使不能持久化的问题。
解决方法:
在linux下经过redis-cli链接redis进行数据库操做:redis

redis-cli config set stop-writes-on-bgsave-error no 

在这里插入图片描述

相关文章
相关标签/搜索