python 操作 redis,redi
- 2020 年 1 月 7 日
- 筆記
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()