python 操作 redis,redis集群

python 操作redis 需要导入redis模块

import redis

    """
    连接redis ConnectionPool 方式连接
    """
    def connRedis(self):
        pool=redis.ConnectionPool(host='172.16.1.2',password='',db=2, port=6379) #按具体情况填写参数
        r=redis.StrictRedis(connection_pool=pool)
        r.set("test_name","admin")
        print(r.get('test_name'))

python 操作redis 集群 用redis模块不行,需要导入模块

安装redis-py-cluster模块
# pip install redis-py-cluster
#!/usr/bin/env python
#coding:utf-8


from rediscluster import StrictRedisCluster
import sys

def redis_cluster():
    redis_nodes =  [{'host':'192.168.1.2','port':6378},
                    {'host':'192.168.1.2','port':6380},
                    {'host':'192.168.1.2','port':6381},
                    {'host':'192.168.1.2','port':6382},
                    {'host':'192.168.1.2','port':6383},
                    {'host':'192.168.1.2','port':6384},
                    {'host':'192.168.1.2','port':6385}
                   ]
    try:
        redisconn = StrictRedisCluster(startup_nodes=redis_nodes)
    except Exception,e:
        print "Connect Error!"
        sys.exit(1)

    redisconn.set('name','admin')
    print "name is: ", redisconn.get('name')

redis_cluster()

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值