def _init_from_conf(self): """根据配置初始化redis客户端连接 """ self.redis_pool = tree.create_tree() # 重置redis_pool # qconf的话特殊对待 if self.redis_local_flag is False: self.redis_conf = self._get_conf() LOG.info("init redis conf, redis_conf=%s", self.redis_conf) for url in self.redis_conf.itervalues(): options = parse_from_url(url) # 如果url不符合标准将会直接ValueError异常 client_name = options["client_name"] db = options["db"] weight = options["weight"] if int(weight) == 0: continue client = Client( host=options["host"], port=options["port"], db=db, transaction=options.get("transaction", False), ) if self.redis_pool[client_name][db]: self.redis_pool[client_name][db].extend([client] * weight) else: self.redis_pool[client_name][db] = [client] * weight
def _do_check_conf(self): """检测配置是否更新,如果配置有变更则重新加载配置信息 """ new_conf = self._get_conf() for k, v in new_conf.iteritems(): if self.redis_conf.get(k, None) != v: self._init_from_conf() LOG.info("run _do_check_conf, config has changed") break
def keys(self, pattern="*"): '''获取所有的key,不建议使用,codis本身也不支持 Args: pattern: 正则 Returns: list类型key的列表 ''' if self.transaction: return self.api.keys(pattern) LOG.warning("keys is not supported by codis")
def rename(self, old_name, new_name): """重命名redis的key Args: old_name: 旧版本key new_name: 新key Returns: False: 如果key不存在 True: key存在并且设置成功 """ try: self.api.rename(mkey(old_name), mkey(new_name)) return True except ResponseError as exc: LOG.error("zyredis rename error, error info=%s", exc) return False
def _wrap(self, method, *args, **kwargs): """对执行的命令进行验证 Args: method: redis的方法 """ LOG.debug("redis adapter execute method:%s, args=%s, kwargs=%s", method, args, kwargs) try: key = args[0] except IndexError: raise ValueError('method %s requires a key param as the first argument' % method) if method.upper() in NOT_SUPPORT_COMMANDS: LOG.error('%s is not supported by codis', method) raise NotSupportCommandError('method %s is not supported by codis, key=%s' % (method, key)) codis_func = getattr(self.api, method) return codis_func(*args, **kwargs)
def _wrap(self, method, *args, **kwargs): '''打包pipeline的方法 Args: method: pipeline要执行的方法 args: pipeline执行方法的参数 kwargs: pipeline字典参数 ''' LOG.debug("pipeline execute method:%s, args=%s, kwargs=%s", method, args, kwargs) try: key = args[0] except: raise ValueError("'%s' requires a key param as the first argument" % method) if not self.transaction and method.upper() in NOT_SUPPORT_COMMANDS: LOG.error('%s is not supported by codis', method) raise NotSupportCommandError('method %s is not supported by codis, key=%s' % (method, key)) # 执行codis f = getattr(self.codis_pipeline, method) f(*args, **kwargs)
def _wrap(self, method, *args, **kwargs): """对执行的命令进行验证 Args: method: redis的方法 """ LOG.debug("redis adapter execute method:%s, args=%s, kwargs=%s", method, args, kwargs) try: key = args[0] except IndexError: raise ValueError( 'method %s requires a key param as the first argument' % method) if method.upper() in NOT_SUPPORT_COMMANDS: LOG.error('%s is not supported by codis', method) raise NotSupportCommandError( 'method %s is not supported by codis, key=%s' % (method, key)) codis_func = getattr(self.api, method) return codis_func(*args, **kwargs)
def _wrap(self, method, *args, **kwargs): '''打包pipeline的方法 Args: method: pipeline要执行的方法 args: pipeline执行方法的参数 kwargs: pipeline字典参数 ''' LOG.debug("pipeline execute method:%s, args=%s, kwargs=%s", method, args, kwargs) try: key = args[0] except: raise ValueError( "'%s' requires a key param as the first argument" % method) if not self.transaction and method.upper() in NOT_SUPPORT_COMMANDS: LOG.error('%s is not supported by codis', method) raise NotSupportCommandError( 'method %s is not supported by codis, key=%s' % (method, key)) # 执行codis f = getattr(self.codis_pipeline, method) f(*args, **kwargs)
def _init_from_conf(self): """根据配置初始化redis客户端连接 """ self.redis_pool = tree.create_tree() # 重置redis_pool self.redis_conf = self._get_conf() LOG.info("init redis conf, redis_conf=%s", self.redis_conf) for url in self.redis_conf.itervalues(): options = parse_from_url(url) # 如果url不符合标准将会直接ValueError异常 client_name = options["client_name"] db = options["db"] weight = options["weight"] if int(weight) == 0: continue client = Client( host=options["host"], port=options["port"], db=db, transaction=options.get("transaction", False), ) if self.redis_pool[client_name][db]: self.redis_pool[client_name][db].extend([client] * weight) else: self.redis_pool[client_name][db] = [client] * weight
def __getattr__(self, method): """用于适配未定义的redis client的函数 """ LOG.debug("in __getattr__ method") return functools.partial(self._wrap, method)
def __len__(self): """``x.__len__() <==> len(x)``""" if self.transaction: return self.api.dbsize() LOG.warning("dbsize is not supported by codis")
def clear(self): """清理所有的key,该命令不支持codis """ if self.transaction: return self.api.flushdb() LOG.warning("flushdb is not supported by codis")
def iterkeys(self, pattern="*"): '''使用迭代器遍历key ''' if self.transaction: return iter(self.keys(pattern)) LOG.warning("keys is not supported by codis")
def info(self): """获取info信息,该命令不支持codis """ if self.transaction: return self.api.info() LOG.warning("info is not supported by codis")
def dbsize(self): """获取dbsize数量,改命令不支持codis """ if self.transaction: return self.api.dbsize() LOG.warning("dbsize is not supported by codis")
def execute(self): '''提交pipeline执行 ''' LOG.debug("pipeline execute flag") return self.codis_pipeline.execute()