Python與redis叢集互動
阿新 • • 發佈:2021-02-17
Python與redis叢集互動
-
安裝包如下
pip install redis-py-cluster
-
redis-py-cluster原始碼地址https://github.com/Grokzen/redis-py-cluster
-
建立⽂件redis_cluster.py,示例碼如下:
-
from rediscluster import RedisCluster if __name__ == '__main__': try: # 構建所有的節點,Redis會使⽤CRC16演算法,將鍵和值寫到某個節點上 startup_nodes = [ {'host': '192.168.X.12', 'port': '7000'}, {'host': '192.168.X.12', 'port': '7002'}, {'host': '192.168.X.12', 'port': '7001'}, ] # 構建StrictRedisCluster物件 src = RedisCluster(startup_nodes=startup_nodes, decode_responses=True) # 設定鍵為name、值為abcd的資料 result = src.set('name2', 'abcd') print(result) # 獲取鍵為name name = src.get('name2') print(name) except Exception as e: print(e)
-
執行效果: