1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| class RedisClientBuilder: """ redis client builder
使用方法: client = RedisClientBuilder.Builder().set_host("127.0.0.1").builder() client.connection() client.set_data("test", "test") """
host = None port = None max_connections = None password = None db = None
def __init__(self, builder): self.host = builder.host self.port = builder.port self.max_connections = builder.max_connections self.password = builder.password self.db = builder.db self._connection = None
def connection(self): if not self._connection: self._connection = redis.StrictRedis( connection_pool=redis.ConnectionPool( host=self.host, port=self.port, max_connections=self.max_connections, db=self.db, password=self.password, ) ) return self._connection
def set_data(self, key, value, ex=None, nx=False): return self._connection.set(key, value, ex, None, nx)
def get_data(self, key): return self._connection.get(key)
def del_data(self, key): return self._connection.delete(key)
def zrange_by_score_data(self, key, min_value, max_value, offset=None, count=None): return self._connection.zrangebyscore(key, min_value, max_value, offset, count)
def flush_db(self): self._connection.flushdb()
def hgetall(self, name): hget_result = dict(self._connection.hgetall(name)) return_result = dict() for k in hget_result: key = bytes.decode(k) value = bytes.decode(hget_result.get(k)) return_result[key] = value return return_result
def hset(self, name, key, value): self._connection.hset(name, key, value)
def hmset(self, key, value, ex=0): self._connection.hmset(key, value) if ex > 0: self._connection.expire(key, ex)
def get_connection(self): return self._connection
def set_json(self, key, json_value, ex): json_str = json.dumps(json_value) return self._connection.set(key, json_str, ex)
def get_json(self, key): json_str = self._connection.get(key) if json_str is None: return None json_value = json.loads(json_str) return json_value
def non_blocking_lock(self, name="redis non blocking lock", timeout=1): """ 非阻塞式分布式锁 :param name: 锁名称 :param timeout: 释放锁时间 :return: """ return Lock(self._connection, name=name, blocking=False, timeout=timeout)
class Builder: DEFAULT_PORT, DEFAULT_MAX_CONNECTIONS, DEFAULT_DB = 6379, 2000, 1
host = None port = DEFAULT_PORT max_connections = DEFAULT_MAX_CONNECTIONS password = None db = DEFAULT_DB
def set_host(self, value): if not value: raise ValueError("host can not be empty") self.host = value return self
def set_port(self, value): self.port = value return self
def set_max_connections(self, value): if not isinstance(value, int): raise ValueError("max_connections must be int") self.max_connections = value return self
def set_password(self, value): self.password = value return self
def set_db(self, value): self.db = value return self
def builder(self): if not self.host or not self.port or not self.db: raise ValueError("host, port, db can not be empty") return RedisClientBuilder(self)
|