学习笔记 -- python操作redis

python操作数据库系列

  1. python使用SQLite
  2. python操作redis
  3. python操作mysql

一、redis-py的模块的安装

控制台输入如下命令:模块主页

pip install redis -i https://pypi.douban.com/simple

二、连接redis

redis没有用户的概念,所以所有共用一个密码,也不建议直接随意创建和关闭连接,建议使用连接池。
导入包

import redis

创建普通连接

r = redis.Redis(host="localhost", port=6379, password="12345678", db=0)

三、redis连接池

创建连接池

从连接池中获取的连接,不必关闭,redis-py模块不提供删除连接的方法。删除引用,触发垃圾回收的时候,连接会自动被归还给连接池,

# max_connections 最大连接数
pool = redis.ConnectionPool(host="localhost", port=6379, db=0, password="12345678", max_connections=20)

构建连接

# 通过连接池创建连接
conn = redis.Redis(connection_pool=pool)
# 删除引用归还连接
del conn

四、python操作指令

与客户端的redis常用命令基本相同,不熟悉的朋友可以去我的博客寻找对应redis的文章。记得对打印结果进行转码。

字符串操作指令

操作指令(一)

conn.set("country", "中国")
conn.set("city", "深圳")
# conn.expire("city", 3)  # 设置过期时间
city = conn.get("city").decode("utf-8")

操作指令(二)

# 删除key 无需del
conn.delete("country", "city")
# 需传入字典类型
conn.mset({"country": "中国", "city": "上海"})
# 返回的是list类型
result = conn.mget("country", "city")
print(type(result))
for r in result:
    print(r.decode("utf-8"))

列表操作指令

操作指令(三)

conn.rpush("name", "张三", "李四", "王五")
conn.lpop("name")  # 删除第一个元素
result = conn.lrange("name", 0, -1)
for r in result:
    print(r.decode("utf-8"))

集合操作指令

操作指令(四)

# 按照元素的哈希值大小排序输出
conn.sadd("id", 1, 2, 3)
conn.srem("id", 1)
result = conn.smembers("id")
print(result)

有序集合操作指令

操作指令(五)

# 参数1:字段, 参数2:字典{值:分数值} 
conn.zadd("keyword", {"张三": 0, "李四": 0, "王五": 0})
conn.zincrby("keyword", "10", "李四")
result = conn.zrevrange("keyword", 0, -1)
print(result)
for i in result:
    print(i.decode("utf-8"))

哈希操作指令

操作指令(六)

conn.hmset("2021", {"name": "zhangsan", "sex": "male", "age": "22"})
conn.hset("2021", "city", "shanghai")
conn.hdel("2021", "age")
conn.hexists("2021", "name")
result = conn.hgetall("2021")
for key in result:
    print(key.decode("utf-8"), result[key].decode("utf-8"))

五、redis-py的事务函数

redis-py模块用pipline(管道)的方式向Redis服务器传递批处理命令和执行事务

常用命令

pipeline= conn.pipeline()
# 监视数据
pipeline.watch("key1", "key2")
# 开始事务
pipeline.multi()
# 提交事务
pipeline.execute()
# 关闭事务
pipeline.reset()

小案例,承接上半部分

pipeline = conn.pipeline()
pipeline.watch("2021")
pipeline.multi()
pipeline.hset("2021", "name", "lisi")
pipeline.hset("2021", "age", "22")
pipeline.execute()
if "pipeline" in dir():
    pipeline.reset()

六、总结

  • 多敲多练即可熟悉常用命令
  • 结合案例会更加不易忘记
scrapy-redis是基于redis的分布式组件,它是scrapy框架的一个组件。它的主要作用是实现断点续爬和分布式爬虫的功能。 使用scrapy-redis可以实现分布式数据处理,爬取到的item数据可以被推送到redis中,这样你可以启动尽可能多的item处理程序。 安装和使用scrapy-redis非常简单,一般通过pip安装Scrapy-redis:pip install scrapy-redis。同时,scrapy-redis需要依赖Python 2.7, 3.4 or 3.5以上,Redis >= 2.8和Scrapy >= 1.1。在使用时,你只需要做一些简单的设置,几乎不需要改变原本的scrapy项目的代码。 scrapy-redis将数据存储在redis中,你可以在redis中查看存储的数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [scrapy_redis的基本使用和介绍](https://blog.csdn.net/WangTaoTao_/article/details/107748403)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [爬虫学习笔记(十二)—— scrapy-redis(一):基本使用、介绍](https://blog.csdn.net/qq_46485161/article/details/118863801)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值