python_操作redis

安裝redis-py

pip3 install redis
image

python 操作 redis

默認(rèn)情況下,所有響應(yīng)在Python 3中以字節(jié)返回(輸出打印 type 為 bytes),在Python 2中以str返回。用戶負(fù)責(zé)解碼為Python 3字符串或Python 2 unicode對(duì)象。

image
import redis
#連接 redis
#指定主機(jī)地址,port與服務(wù)器連接,redis默認(rèn)數(shù)據(jù)庫有16個(gè),默認(rèn)db是0
r = redis.Redis(host='172.17.0.4',port=6380,db=0) #password='**'
print(r.get('foo'))
r.set('foo','[1,2,3]')
print(r.get('foo'))
print(r.keys())
r.delete('foo')
print(r.keys())

image

python 連接 redis 使用連接池

redis-py使用connection pool來管理對(duì)一個(gè)redis server的所有連接,避免每次建立、釋放連接的開銷。默認(rèn),每個(gè)Redis實(shí)例都會(huì)維護(hù)一個(gè)自己的連接池??梢灾苯咏⒁粋€(gè)連接池,然后作為參數(shù)Redis,這樣就可以實(shí)現(xiàn)多個(gè)Redis實(shí)例共享一個(gè)連接池

import redis

pool = redis.ConnectionPool(host='172.17.0.4', port=6380)
r = redis.Redis(connection_pool=pool)
r.set('foo', 'Bar')
print(r.get('foo'))

python 連接 redis 主從服務(wù)器 sentinel 集群 (哨兵模式)

image
import redis
from redis.sentinel import Sentinel

#連接哨兵服務(wù)器(主機(jī)名也可以用域名)
sentinel = Sentinel([('172.17.0.4',26379),('172.17.0.4',26380),('172.17.0.4',26381)],socket_timeout=0.5)

# 獲取主服務(wù)器地址
master = sentinel.discover_master('mymaster')  # 設(shè)置的master名字
print(master)

# 獲取從服務(wù)器地址
slave = sentinel.discover_slaves('mymaster')
print(slave)

# 獲取主服務(wù)器進(jìn)行寫入
master = sentinel.master_for('mymaster', socket_timeout=0.5, db=0)
print("主服務(wù)器插入前的name為:",master.get('name'))
w_ret = master.set('name', 'shark')
print("主服務(wù)器插入后的name為:", master.get('name'))

# # 獲取從服務(wù)器進(jìn)行讀取(默認(rèn)是round-roubin)
slave = sentinel.slave_for('mymaster', socket_timeout=0.5, db=0)
r_ret = slave.get('name')
print("從服務(wù)器的name為:", r_ret)

image

會(huì)出現(xiàn)如下報(bào)錯(cuò)

image

解決方案: 配置文件中寫入

bind 0.0.0.0
daemonize no
protected-mode no

image

解決方案: 配置文件中

sentinel monitor mymaster 127.0.0.1 6380 2
改成 
sentinel monitor mymaster 172.17.0.4 6380 2

python 操作 redis 集群

啟動(dòng)集群
redis-server /etc/redis/7001.conf 
redis-server /etc/redis/7002.conf 
redis-server /etc/redis/7003.conf 
redis-server /etc/redis/7004.conf 
redis-server /etc/redis/7005.conf
redis-server /etc/redis/7006.conf 

image
image
pip3 install redis-py-cluster

redis-py-cluster 官方網(wǎng)站

#python連接rediscluster集群測(cè)試
from rediscluster import StrictRedisCluster 
startup_nodes = [{"host": "172.17.0.3", "port": "7001"},
                 {"host": "172.17.0.3", "port": "7002"},
                 {"host": "172.17.0.3", "port": "7003"},
                 {"host": "172.17.0.3", "port": "7004"},
                 {"host": "172.17.0.3", "port": "7005"},
                 {"host": "172.17.0.3", "port": "7006"}  ]  
# Note: 與python3一起使用時(shí),decode_responses必須設(shè)置為True  
m1 = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)  
m1.set("name", "shark")  
print("集群中的 name 為:",m1.get("name"))

打印結(jié)果如下:

image

可能有報(bào)錯(cuò)如下

image

原因是 rediscluster 和 redis-py 版本不兼容
建議使用以下版本

image
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 安裝redis-py redis-py 官方網(wǎng)站 python 操作 redis 默認(rèn)情況下,所有響應(yīng)在Pytho...
    zxhChex閱讀 1,172評(píng)論 2 17
  • python 操作 redis 默認(rèn)情況下,Python 3響應(yīng)redis中以字節(jié)返回(輸出打印 type 為 b...
    夜醉夢(mèng)紅塵閱讀 1,680評(píng)論 0 7
  • Redis是一個(gè)基于內(nèi)存的鍵值對(duì)存儲(chǔ)系統(tǒng),常用作數(shù)據(jù)庫、緩存和消息代理。它支持字符串、字典、列表、集合、有序集合、...
    Four__years閱讀 691評(píng)論 0 0
  • python操作Redis 一. Redis是什么 Redis 是一個(gè)開源(BSD許可)的,內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)存儲(chǔ)系...
    shu_ke閱讀 3,976評(píng)論 0 9
  • 這篇文章主要介紹了Windows下安裝Redis及使用Python操作Redis的方法,非常不錯(cuò),具有參考借鑒價(jià)值...
    零三鄧何芯桃379閱讀 279評(píng)論 0 0

友情鏈接更多精彩內(nèi)容