Esempio n. 1
0
    def query_by_keyword(cls,
                         keyword,
                         result_limit=CONFIG.SEARCH_PAGE_SIZE,
                         search_limit=CONFIG.MAX_SEARCH_COUNT):
        cache_key = 'KeywordArticles:' + keyword
        cached_result = redis_cache_client.get(cache_key)
        if cached_result is not None:
            if not cached_result:
                return []
            try:
                article_ids = cached_result.split(',')
                return [int(article_id) for article_id in article_ids]
            except ValueError:
                logging.warning('Key "%s" contains wrong value: %s', cache_key,
                                cached_result)
                redis_cache_client.delete(cache_key)

        pattern = '*%s*:*' % keyword.lower()
        cursor, members = cls.redis_client.sscan(cls.KEY,
                                                 match=pattern,
                                                 count=search_limit)
        if members:
            article_ids = [
                member.rsplit(':', 1)[-1] for member in members[:result_limit]
            ]
            result = [int(article_id) for article_id in article_ids]
        else:
            article_ids = result = []

        redis_cache_client.set(cache_key,
                               ','.join(article_ids),
                               ex=CONFIG.DEFAULT_CACHE_TIME)

        return result
Esempio n. 2
0
    def query_by_keyword(cls, keyword, result_limit=CONFIG.SEARCH_PAGE_SIZE, search_limit=CONFIG.MAX_SEARCH_COUNT):
        cache_key = 'KeywordArticles:' + keyword
        cached_result = redis_cache_client.get(cache_key)
        if cached_result is not None:
            if not cached_result:
                return []
            try:
                article_ids = cached_result.split(',')
                return [int(article_id) for article_id in article_ids]
            except ValueError:
                logging.warning('Key "%s" contains wrong value: %s', cache_key, cached_result)
                redis_cache_client.delete(cache_key)

        pattern = '*%s*:*' % keyword.lower()
        cursor, members = cls.redis_client.sscan(cls.KEY, match=pattern, count=search_limit)
        if members:
            article_ids = [member.rsplit(':', 1)[-1] for member in members[:result_limit]]
            result = [int(article_id) for article_id in article_ids]
        else:
            article_ids = result = []

        redis_cache_client.set(cache_key, ','.join(article_ids), ex=CONFIG.DEFAULT_CACHE_TIME)

        return result
Esempio n. 3
0
 def delete(cls, key):
     return redis_cache_client.delete(cls.KEY % key) == 1