python连接redis集群如何释放内存_python连接redis3.x集群并做操作

notes-------

(1)redis3.x集群模式操作需要List of all supported python versions.2.7/3.2/3.3/3.4.1+/3.5

#!/usr/bin/python

# -*- coding: UTF-8 -*-

#-------------------------------------------------------------------------------

# Name:

# Author: yangsq

# Email: [email protected]

# Created: 14/03/2016

# requirement: python >=2.7

#-------------------------------------------------------------------------------

import redis

from rediscluster import StrictRedisCluster

import os, time, sys

serverip = "127.0.0.1"

startup_nodes = [{"host":serverip, "port":port} for port in xrange(7000,7006)]

def connRedisCluster():

rc = StrictRedisCluster(startup_nodes=startup_nodes)

return rc

def public_article(conn, article, user, title, link):

conn.hmset(article, {'title':title, 'publicer':user, 'link':link})

if __name__=='__main__':

print startup_nodes

rc = connRedisCluster()

public_article(rc, 'chinese_history', 'yangsq', 'war', 'http://goon')

print rc.hgetall('chinese_history')

Python连接Redis集群,可以使用第三方库redis-py-cluster。这个库是专门为了支持Redis集群操作而设计的,并且一直在维护。你可以使用pip进行安装,命令是pip3 install redis-py-cluster。 以下是一个示例代码,展示了如何使用redis-py-cluster连接Redis集群: ``` from rediscluster import StrictRedisCluster # 连接redis集群 class RedisCluster(object): def __init__(self,conn_list): self.conn_list = conn_list def connect(self): try: # 非密码连接redis集群 # redisconn = StrictRedisCluster(startup_nodes=self.conn_list) # 使用密码连接redis集群 redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='123456') return redisconn except Exception as e: print(e) print("错误,连接redis集群失败") return False # Redis集群连接列表 redis_basis_conn = [{'host': '192.168.10.168', 'port': 7201}, {'host': '192.168.10.169', 'port': 7201}, {'host': '192.168.10.170', 'port': 7201}, {'host': '192.168.10.171', 'port': 7201}, {'host': '192.168.10.142', 'port': 7201}, {'host': '192.168.10.143', 'port': 7201}] # 连接Redis集群 res = RedisCluster(redis_basis_conn).connect() # 检查连接结果 if not res: print("连接Redis集群失败") else: print("连接Redis集群成功") ``` 使用redis-py-cluster库,你可以通过传入连接列表中的所有节点信息来连接Redis集群。在上述示例代码中,我们使用了一个名为RedisCluster的类来进行连接,并在初始化方法中传入了连接列表。然后,在connect方法中,我们尝试连接Redis集群,如果连接成功,返回一个Redis连接对象,否则返回False表示连接失败。最后,我们根据连接的结果输出相应的提示信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值