本文整理匯總了Python中redis.client.StrictRedis.keys方法的典型用法代碼示例。如果您正苦於以下問題:Python StrictRedis.keys方法的具體用法?Python StrictRedis.keys怎麽用?Python StrictRedis.keys使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.client.StrictRedis
的用法示例。
在下文中一共展示了StrictRedis.keys方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: RedisDataSource
# 需要導入模塊: from redis.client import StrictRedis [as 別名]
# 或者: from redis.client.StrictRedis import keys [as 別名]
class RedisDataSource(AbstractDataSource):
_r = None
def __init__(self,config):
if self._validateConfig(config):
self._r = StrictRedis(host=config[REDIS_DATASOURCE_CONFIG][REDIS_DATASOURCE_CONFIG_HOST],
port=config[REDIS_DATASOURCE_CONFIG][REDIS_DATASOURCE_CONFIG_PORT],
db=config[REDIS_DATASOURCE_CONFIG][REDIS_DATASOURCE_CONFIG_DB])
logger.debug("Obtained internal redis handler" + str(self._r))
else:
raise BaseException("Error validating config ")
def update(self,item):
self.store(item)
def store(self,item):
self._r.set(item.getHash(), item.getValue())
def get(self,item):
return self._r.get(item.getHash())
def exists(self,item):
return self.get(item) is not None
def all(self):
result = []
# Obtain all keys
keys = self._r.keys()
#For each key, get value
for k in keys:
value = self._r.get(k)
result.append(BaseItem({"origin":"redis"},value))
#return result
return result
def _validateConfig(self,config):
validator = MultipleConfigValidator(
{VALIDATORS_LIST:[ContainsKeyConfigValidator({KEY_VALUE:REDIS_DATASOURCE_CONFIG})]})
if not validator.validate(config):
raise BaseException("Config validation error : does not contain " + REDIS_DATASOURCE_CONFIG)
# Validate redis datasource config
validator = MultipleConfigValidator(
{VALIDATORS_LIST:[ContainsKeysConfigValidator({KEYS_LIST:[REDIS_DATASOURCE_CONFIG_DB,
REDIS_DATASOURCE_CONFIG_HOST,
REDIS_DATASOURCE_CONFIG_PORT]})]})
if not validator.validate(config[REDIS_DATASOURCE_CONFIG]):
raise BaseException("Config validation error : config not complete ")
return True
def delete(self,item):
self._r.delete(item.getHash())
示例2: RedisStore
# 需要導入模塊: from redis.client import StrictRedis [as 別名]
# 或者: from redis.client.StrictRedis import keys [as 別名]
class RedisStore():
def __init__(self, db_host, db_port, db_num, db_pw):
pool = ConnectionPool(max_connections=2, db=db_num, host=db_host, port=db_port, password=db_pw)
self.redis = StrictRedis(connection_pool=pool)
self.class_map = {}
self.object_map = {}
def create_object(self, dbo, update_rev=False):
self.save_object(dbo, update_rev)
dbo.on_loaded()
def save_object(self, dbo, update_rev=False, autosave=False):
if update_rev:
rev = getattr(dbo, "dbo_rev", None)
dbo.dbo_rev = 1 if not rev else rev + 1
dbo.before_save()
key = dbo.dbo_key
self.redis.set(key, self.json_encode(dbo.save_json_obj))
if dbo.dbo_set_key:
self.redis.sadd(dbo.dbo_set_key, key)
self.dispatch("db_log{0}".format("_auto" if autosave else ""), "object saved: " + key)
self.object_map[dbo.dbo_key] = dbo
def load_cached(self, key):
return self.object_map.get(key)
def evict_object(self, dbo):
try:
del self.object_map[dbo.dbo_key]
except:
debug("Failed to evict " + dbo.dbo_key + " from db cache", self)
@logged
def load_by_key(self, key_type, key, base_class=None):
dbo_key = unicode('{0}:{1}'.format(key_type, key))
cached_dbo = self.object_map.get(dbo_key)
if cached_dbo:
return cached_dbo
json_str = self.redis.get(dbo_key)
if not json_str:
return None
json_obj = self.json_decode(json_str)
dbo = self._load_class(json_obj, base_class)(key)
if dbo.dbo_key_type:
self.object_map[dbo.dbo_key] = dbo
self.load_json(dbo, json_obj)
return dbo
def object_exists(self, obj_type, obj_id):
key = unicode('{0}:{1}'.format(obj_type, obj_id))
return key in self.redis.keys(key)
def load_object(self, dbo_class, key):
return self.load_by_key(dbo_class.dbo_key_type, key, dbo_class)
def update_object(self, dbo, json_obj):
self.load_json(dbo, json_obj)
self.save_object(dbo, True)
def delete_object(self, dbo):
key = dbo.dbo_key
self.redis.delete(key)
if dbo.dbo_set_key:
self.redis.srem(dbo.dbo_set_key, key)
for dbo_col in dbo.dbo_collections:
if dbo_col.key_type:
coll = getattr(dbo, dbo_col.field_name, set())
for child_dbo in coll:
self.delete_object(child_dbo)
debug("object deleted: " + key, self)
if self.object_map.get(dbo.dbo_key):
del self.object_map[dbo.dbo_key]
return True
def fetch_set_keys(self, set_key):
return self.redis.smembers(set_key)
def set_index(self, index_name, key, value):
return self.redis.hset(index_name, key, value)
def get_index(self, index_name, key):
return self.redis.hget(index_name, key)
def delete_index(self, index_name, key):
return self.redis.hdel(index_name, key)
def _load_class(self, json_obj, base_class):
class_path = json_obj.get("class_name")
if not class_path:
return self.cls_registry(base_class)
clazz = self.class_map.get(class_path)
if clazz:
return clazz
split_path = class_path.split(".")
module_name = ".".join(split_path[:-1])
class_name = split_path[-1]
module = __import__(module_name, globals(), locals(), [class_name])
clazz = getattr(module, class_name)
clazz = self.cls_registry(clazz)
self.class_map[class_path] = clazz
#.........這裏部分代碼省略.........