網站首頁 編程語言 正文
redis模塊的使用
1.安裝模塊
pip3 install redis
2.導入模塊
import redis
3.連接方式
- 嚴格連接模式:r=redis.StrictRedis(host=“”,port=)
- 更Python化的連接模式:r=redis.Redis(host=“”,port=)
- StrictRedis用于實現大部分官方的命令,并使用官方的語法和命令
- Redis與StrictRedis的區別是:Redis是StrictRedis的子類,用于向前兼容舊版本的redis-py,并且這個連接方式是更加"python化"的
4.連接池
為了節省資源,減少多次連接損耗,連接池的作用相當于總攬多個客戶端與服務端的連接,當新客戶端需要連接時,只需要到連接池獲取一個連接即可,實際上只是一個連接共享給多個客戶端。
import redis
pool= redis.ConnectionPool(host='localhost',port=6379,decode_responses=True)
r=redis.Redis(connection_pool=pool)
r2=redis.Redis(connection_pool=pool)
r.set('apple','a')
print(r.get('apple'))
r2.set('banana','b')
print(r.get('banana'))
print(r.client_list())
print(r2.client_list())#可以看出兩個連接的id是一致的,說明是一個客戶端連接
5.操作
- 值的設置和獲取,可以參考redis的命令,redis模塊中的對應功能的函數名基本與redis中的一致
- 注意默認情況下,設置的值或取得的值都為bytes類型,如果想改為str類型,需要在連接時添加上decode_responses=True】
- 設置值:
redis中set() ==>r.set()
redis中setnx() ==>r.set()
redis中setex() ==>r.setex()
redis中setbit() ==>r.setbit()
redis中mset() == > r.mset()
redis中hset() ==>r.hset()
redis中sadd() == >r.sadd()
#其他。。。基本redis的命令名與redis模塊中的函數名一致
獲取:
redis中get() ==》r.get()
redis中mget() ==》r.mget()
redis中getset() ==》r.getset()
redis中getrange() ==》r.getrange()
#其他。。。基本redis的命令名與redis模塊中的函數名一致
import redis
r=redis.Redis(host='localhost',port=6379,decode_responses=True)
# r=redis.StrictRedis(host='localhost',port=6379)
r.set('key','value')
value=r.get('key')
# print(type(value))
print(value)
r.hset('info','name','lilei')
r.hset('info','age','18')
print(r.hgetall('info'))
r.sadd('course','math','english','chinese')
print(r.smembers('course'))
管道
一般情況下,執行一條命令后必須等待結果才能輸入下一次命令,管道用于在一次請求中執行多個命令。
參數介紹:
transaction:指示是否所有的命令應該以原子方式執行。
import redis,time
r=redis.Redis(host="localhost",port=6379,decode_responses=True)
pipe=r.pipeline(transaction=True)
pipe.set('p1','v2')
pipe.set('p2','v3')
pipe.set('p3','v4')
time.sleep(5)
pipe.execute()
事務
python中可以使用管道來代替事務:
補充:監視watch:pipe.watch()
import redis,time
import redis.exceptions
r=redis.Redis(host='localhost',port=6379,decode_responses=True)
pipe=r.pipeline()
print(r.get('a'))
try:
# pipe.watch('a')
pipe.multi()
pipe.set('here', 'there')
pipe.set('here1', 'there1')
pipe.set('here2', 'there2')
time.sleep(5)
pipe.execute()
except redis.exceptions.WatchError as e:
print("Error")
訂閱\發布
發布方:
import redis
r=redis.Redis(host="localhost",port=6379,decode_responses=True)
#發布使用publish(self, channel, message):Publish ``message`` on ``channel``.
Flag=True
while Flag:
msg=input("主播請講話>>:")
if len(msg)==0:
continue
elif msg=='quit':
break
else:
r.publish('cctv0',msg)
訂閱方:
當訂閱成功后,第一次接收返回的第一個消息是一個訂閱確認消息:
import redis
r=redis.Redis(host="localhost",port=6379,decode_responses=True)
#發布使用publish(self, channel, message):Publish ``message`` on ``channel``.
Flag=True
chan=r.pubsub()#返回一個發布/訂閱對象
msg_reciver=chan.subscribe('cctv0')#訂閱
msg=chan.parse_response()#第一次會返回訂閱確認信息
print(msg)
print("訂閱成功,開始接收------")
while Flag:
msg=chan.parse_response()#接收消息
print(">>:",msg[2])#此處的信息格式['消息類型', '頻道', '消息'],所以使用[2]來獲取
原文鏈接:https://blog.csdn.net/sinat_38682860/article/details/125444512
相關推薦
- 2022-11-11 Python?第三方庫?Pandas?數據分析教程_python
- 2023-01-21 Python?configparser模塊的用法示例代碼_python
- 2022-05-20 SpringCloud系列:springboot改造集成nacos
- 2022-06-26 ASP.NET?Core中引用OpenAPI服務的添加示例_實用技巧
- 2022-05-23 c++?qt自定義搜索編輯框的實現方法_C 語言
- 2022-05-25 Flutter?Animation實現縮放和滑動動畫效果_Android
- 2022-04-27 bash?shell獲取當前腳本的絕對路徑(pwd/readlink)_linux shell
- 2022-11-04 ASP.NET?MVC實現登錄后跳轉到原界面_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支