def redis_cluster(): '''集群操作''' redis_nodes = [{ 'host': '10.12.28.222', 'port': 6380 }, { 'host': '10.12.28.222', 'port': 6381 }, { 'host': '10.12.28.224', 'port': 6380 }, { 'host': '10.12.28.224', 'port': 6381 }, { 'host': '10.12.28.227', 'port': 6380 }, { 'host': '10.12.28.227', 'port': 6381 }] try: r = StrictRedisCluster(startup_nodes=redis_nodes) except Exception as e: print("connect error %s" % e) # string 操作 r.set('thoth:thoth-ai:robot:1', 'kk') # r.delete('thoth:thoth-ai:robot:1') print("name is", r.get('thoth:thoth-ai:robot:1')) # list 操作 r.lpush('thoth:thoth-ai:robot:2', [[1, 2, 3], [2, 3, 4]]) print('list len:', r.llen("thoth:thoth-ai:robot:2")) # list size print("list ", r.lindex('thoth:thoth-ai:robot:2', 0)) # hash 操作 r.hset('thoth:thoth-ai:robot:3', 'private_vector', [[1, 2, 3], [2, 3, 4]]) r.hset('thoth:thoth-ai:robot:3', 'public_vector', [['4', 3, 2], [0, 1, 1]]) pv = r.hget( 'thoth:thoth-ai:robot:3', 'public_vector', ) print('hash.robot3.public_vector:', pv) aaa = pv.decode('utf-8') print(type(aaa), aaa) b = eval(aaa) # eval 函数妙用:将string‘[1,2,3]’--->list [1,2,3] print(type(b), b)
class RedisClient(object): def __init__(self, key, startup_nodes): """ init cluster """ self.key = key self.conn = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True) def hdel(self, field): """ delete an item :param field: :return: """ self.conn.hdel(self.key, field) def hexists(self, field): """ 判断 key 中是否含有 field :param field: :return: """ return self.conn.hexists(self.key, field) def hget(self, field): """ 返回key中指定 field 中的 value :param field: :return: """ value = self.conn.hget(self.key, field) if isinstance(value, bytes): return value.decode('utf-8') else: return value if value else None def hgetall(self): """ 获取 {filed: value, field1: value1....} :return: """ all_dict = self.conn.hgetall(self.key) if not all_dict: return elif sys.version_info.major == 3: return { field.decode('utf-8'): value.decode('utf-8') for field, value in all_dict.items() } else: return all_dict def hkeys(self): """ 获取key中所有field :return: """ field = self.conn.hkeys(self.key) if isinstance(field, bytes): return field.decode('utf-8') else: return field if field else None def hlen(self): """ 获取所有 filed 数量 :return: """ return self.conn.hlen(self.key) def hset(self, field, value): """ 设置 field: value :param field: :param value: :return: """ self.conn.hset(self.key, field, value) def hvals(self): """ 获取所有values :return: """ values = self.conn.hvals(self.key) if not values: return elif sys.version_info.major == 3: return [value.decode('utf-8') for value in values] else: return values def change_key(self, key): """ 替换 key :param key: :return: """ self.key = key # =============================================== def blpop(self, timeout): self.conn.blpop(self.key, timeout=timeout) def brpop(self, timeout): self.conn.brpop(self.key, timeout=timeout) def brpoplpush(self, dst, timeout): self.conn.brpoplpush(self.key, dst=dst, timeout=timeout) def lindex(self, i): self.conn.lindex(self.key, index=i) def llen(self): self.conn.llen(self.key) def lpop(self): self.conn.lpop(self.key) def lpush(self): self.conn.lpush(self.key) def lrange(self, start, stop): self.conn.lrange(self.key, start, stop) def lset(self, i, value): self.conn.lset(self.key, index=i, value=value) def rpop(self): self.conn.rpop(self.key) def rpoplpush(self, dst): self.conn.rpoplpush(self.key, dst=dst) def rpush(self, value): self.conn.rpush(self.key, value)