python操作redis(单机、集群、哨兵)
安装单机和哨兵,使用的是redis模块,所以下载redis即可,使用pip或者pip3命令来安装。pip3 install redis集群需要用到rediscluster模块,采用以下命令安装redis-py-cluster即可,最好指定版本pip3 install redis-py-cluster=1.3.6redis单机连接import redisconn = redis...
·
安装
单机和哨兵,使用的是redis模块,所以下载redis即可,使用pip或者pip3命令来安装。
pip3 install redis
集群需要用到rediscluster模块,采用以下命令安装redis-py-cluster即可,最好指定版本
pip3 install redis-py-cluster=1.3.6
redis单机连接
import redis
conn = redis.StrictRedis(connection_pool=redis.ConnectionPool(
host="192.168.233.1", port="6379", password=""
))
# string类型的写入读取
conn.set("key_name", "value")
conn.get("key_name")
# 列表类型的读取写入
conn.lpush("key_name", "value1", "value2",...)
conn.lrange("key_name", 0, -1) # 列出key_name下的所有值
以下是函数形式:
import redis
def get_single_connection(host, port, password):
conn = redis.StrictRedis(connection_pool=redis.ConnectionPool(
host=host, port=port, password=password
))
return conn
redis哨兵连接
from redis.sentinel import Sentinel
sentinel_list = [
("192.168.233.1", "26379"),
("192.168.233.2", "26379"),
("192.168.233.3", "26379")
]
mySentinel = Sentinel(sentinel_list)
master = mySentinel.master_for("mymaster", db=0)
slave = mySentinel.slave_for("mymaster", db=0)
# 使用master进行写的操作,使用slave进行读的操作
master.hset("key_name", "filed", "value")
slave.hget("key_name", "filed")
slave.hgetall("key_name")
以下是函数形式:
from redis.sentinel import Sentinel
class RedisSentinel:
def __init__(self, sentinel_list, name="mymaster", password="", db=0):
self.sentinel = Sentinel(sentinel_list, socket_timeout=60)
self.name = name
self.password = password
self.db = db
def get_master_and_slave_conn(self):
master = self.sentinel.master_for(
service_name=self.name,
socket_timeout=60,
password=self.password,
db=self.db)
slave = self.sentinel.slave_for(
service_name=self.name,
socket_timeout=60,
password=self.password,
db=self.db
)
return master, slave
redis集群连接
from rediscluster import StrictRedisCluster
cluster_nodes = [{"host": "192.168.233.1", "port": "9001"},
{"host": "192.168.233.1", "port": "9002"},
{"host": "192.168.233.1", "port": "9003"},
{"host": "192.168.233.2", "port": "9004"},
{"host": "192.168.233.2", "port": "9005"},
{"host": "192.168.233.2", "port": "9006"}]
password = "root"
conn = StrictRedisCluster(
startup_nodes=cluster_nodes,
decode_response=True,
password=password,
max_connection=300
)
# 写入值,获取值
conn.set("key_name", "value")
conn.get("key_name")
以下是函数形式:
from rediscluster import StrictRedisCluster
def get_cluster_conn(cluster_nodes, password)
conn= StrictRedisCluster(
startup_nodes=cluster_nodes,
decode_response=True,
password=password,
max_connection=300
)
return conn
redis的操作
redis有五种数据类型:string, set, list, hash, sorted set
常用的操作有:增加,查询,删除
以下是python中,使用redis连接后的操作,其中conn即为上边的单机、集群、哨兵的连接:
string字符串
# 增加\查询\删除
conn.set(key, value)
conn.get(key)
conn.delete(key)
set集合
# 增加\查询\删除
conn.sadd(key, value1, value2...)
conn.smembers(key)
conn.delete(key)
list列表
# 增加\查询\删除
conn.lpush(key, value1, value2,...)
conn.lrange(key, start, end) # 比如列出所有conn.lrange("key_name", 0, -1)
conn.delete(key)
hash哈希
# 增加\查询\删除
conn.hset(key, field, value)
conn.hget(key, filed)
conn.hgetall(key)
conn.delete(key)
sorted set有序集合
# 增加\查询\删除
# 注意:有序集合键值对是与redis中操作相反的,redis中:zadd key score value
# python中,分值对应的是字典中的值
conn.zadd(key, {value1: score1, value2: score2})
conn.zrange(key, start, end)
conn.delete(key)
更多推荐
已为社区贡献1条内容
所有评论(0)