当前位置: 首页>>代码示例>>Python>>正文


Python StrictRedis.dbsize方法代码示例

本文整理汇总了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)
开发者ID:aehuynh,项目名称:scavenger,代码行数:62,代码来源:cache.py

示例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.'
开发者ID:ui,项目名称:rq-scheduler,代码行数:12,代码来源:__init__.py


注:本文中的redis.StrictRedis.dbsize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。