本文整理汇总了Python中redis.StrictRedis.dbsize方法的典型用法代码示例。如果您正苦于以下问题:Python StrictRedis.dbsize方法的具体用法?Python StrictRedis.dbsize怎么用?Python StrictRedis.dbsize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.StrictRedis
的用法示例。
在下文中一共展示了StrictRedis.dbsize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: IndexCache
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import dbsize [as 别名]
class IndexCache(object):
def __init__(self, host, port):
self.client = StrictRedis(host=host, port=port, db=0)
def build(self, doc_word_scores):
"""Clears the entire store and adds all the doc_word_scores into a
hash table in the store.
:param doc_word_scores: dictionary of dictionaries that looks like:
doc_word_scores[word][doc_id] = score of word in that
document
"""
self.reset()
for word, doc_id_score in doc_word_scores.items():
# Add table name to word
word_key = DOCUMENT_WORD_SCORE_NAME + word
self.client.hmset(word_key, doc_id_score)
self.save_to_disk()
def reset(self):
"""Deletes all keys in this DB.
"""
self.client.flushdb()
def save_to_disk(self):
"""Asyncronous write to disk for persistent storage.
"""
self.client.bgsave()
def to_dict(self):
"""Returns the "doc_word_scores" table in Redis in dictionary form.
"""
doc_word_scores = {}
for word_key in self.doc_word_scores_iter():
# Remove the table name from the key
word = word_key.replace(DOCUMENT_WORD_SCORE_NAME, "")
# Grab the {doc_ids : scores} dictionary for word
doc_word_scores[word] = self.client.hgetall(word_key)
return doc_word_scores
def doc_word_scores_iter(self):
"""Returns an iterator for the keys of all the words stored in Redis
"""
return self.client.scan_iter(match=DOCUMENT_WORD_SCORE_NAME + "*")
def is_empty(self):
return self.client.dbsize() <= 0
def doc_scores(self, word):
"""Returns a hash table of document_ids mapping to scores
"""
word_key = DOCUMENT_WORD_SCORE_NAME + word
return self.client.hgetall(word_key)
示例2: find_empty_redis_database
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import dbsize [as 别名]
def find_empty_redis_database():
"""Tries to connect to a random Redis database (starting from 4), and
will use/connect it when no keys are in there.
"""
for dbnum in range(4, 17):
testconn = StrictRedis(db=dbnum)
empty = testconn.dbsize() == 0
if empty:
return testconn
assert False, 'No empty Redis database found to run tests in.'