1. 首先安裝啊
pip install?redis-py-cluster
2. 開始使用集群操作咯
RedisConf.nodelist =?[{'host':'10.2**.1**.**', 'port':'6379'}, {'host':'10.**5.*2.**2', 'port':'6379'}]
from redisclusterimport RedisCluster
# redis集群操作
class SxRedis:
"""
? ? 使用舉例:? ? print(f"檢查返回值:{SxRedis().connect().get('IM:ACCOUNT:RED_PAPER_ORDER:594233041325531137:red_13b8242875966000')}")
"""
? ? def __init__(self):
self.conn_list = RedisConf.nodelist# 連接列表
? ? ? ? self.sxredis =self.connect()
def connect(self):
"""
? ? ? ? 連接redis集群
? ? ? ? :return: object
"""
? ? ? ? try:
# 使用密碼連接redis集群
? ? ? ? ? ? redisconn = RedisCluster(startup_nodes=self.conn_list, decode_responses=True, password='sdjsx')
return redisconn
except Exception as e:
print("錯誤,連接redis 集群失敗")
raise e
# 來看看使用舉例
if __name__ =='__main__':
pass
? ? print(f"檢查返回值:{SxRedis().connect().get('ipush_invalid_token_619699241471844353')}")
# print(f"檢查返回值:{SxRedis().connect()..hgetall('IM:ACCOUNT:RED_PAPER_ORDER:530868770131025921')}")
? ? # print(f"檢查返回值:{SxRedis().connect().delete('ipush_invalid_token_619699241471844353')}")
『『需自動化項目源碼\交流學(xué)習(xí)添加v:ct-xiaozhou
免費(fèi)提供的。行業(yè)變遷,多個好友多點(diǎn)可能性。非機(jī)構(gòu)』』