前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 操作 redis,redi

python 操作 redis,redi

作者头像
py3study
发布2020-01-07 18:00:52
5230
发布2020-01-07 18:00:52
举报
文章被收录于专栏:python3python3
python 操作redis 需要导入redis模块

import redis

代码语言:javascript
复制
    """
    连接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模块
代码语言:javascript
复制
# pip install redis-py-cluster
代码语言:javascript
复制
#!/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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • python 操作redis 需要导入redis模块
    • 安装redis-py-cluster模块
    相关产品与服务
    云数据库 Redis
    腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档