當前位置: 首頁>>代碼示例>>Python>>正文


Python Redis.lrange方法代碼示例

本文整理匯總了Python中redis.client.Redis.lrange方法的典型用法代碼示例。如果您正苦於以下問題:Python Redis.lrange方法的具體用法?Python Redis.lrange怎麽用?Python Redis.lrange使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在redis.client.Redis的用法示例。


在下文中一共展示了Redis.lrange方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: lrange

# 需要導入模塊: from redis.client import Redis [as 別名]
# 或者: from redis.client.Redis import lrange [as 別名]
    def lrange(self, name, start, end):
        """
        Return a slice of the list ``name`` between
        position ``start`` and ``end``
        
        ``start`` and ``end`` can be negative numbers just like
        Python slicing notation

        decode the results
        """
        results = Redis.lrange(self, name, start, end)
        return [result.decode(self.encoding) for result in results]
開發者ID:cdent,項目名稱:tiddlywebredis,代碼行數:14,代碼來源:redisstore.py

示例2: TSStore

# 需要導入模塊: from redis.client import Redis [as 別名]
# 或者: from redis.client.Redis import lrange [as 別名]
class TSStore(object):

    def __init__(self, config):
        self._redis = Redis(host=config.get('redis','host'), 
                            port=int(config.get('redis','port')),
                            db=int(config.get('redis','db')))
        self._delta_secs = int(eval(config.get('timeseries',
                                               'delta_secs')))
        self._expiration_delay_secs = int(eval(config.get('timeseries',
                                                          'expiration_delay_secs')))

    def queries_key(self):
        return 'queries'
    @property
    def queries(self):
        return self._redis.smembers(self.queries_key())
    @queries.setter
    def queries(self, values):
        pipe = self._redis.pipeline()
        pipe.delete(self.queries_key())
        for v in values:
            pipe.sadd(self.queries_key(),
                      v)
        return pipe.execute()

    def _interval_key(self, timestamp):
        return int(timestamp) - int(timestamp) % self._delta_secs
    def _ts_key(self, timestamp, query):
        return 'ts:%(query)s:%(timestamp_key)s'%{'query':query,
                                                 'timestamp_key':self._interval_key(timestamp)}
    def _tweet_key(self, t):
        if type(t) == Tweet:
            return 'tweet:%s'%t.id
        return 'tweet:%s'%t
    def _query_key(self, query):
        return 'query:%s:last_tweet_id'%query

    def _store_tweet(self, pipe, tweet):
        tweet_key = self._tweet_key(tweet)
        pipe.set(tweet_key, tweet.serialize())
        pipe.expire(tweet_key, self._expiration_delay_secs)
    def _reference_tweet(self, pipe, timestamp, query, tweet):
        ts_key = self._ts_key(timestamp, query)
        pipe.lpush(ts_key,tweet.id)
        pipe.expire(ts_key,self._expiration_delay_secs)
    def _update_last_query_tweet(self, pipe, query, tweet):
        query_key = self._query_key(query)
        pipe.set(query_key,tweet.id)
    def append(self, query, tweet):
        pipe = self._redis.pipeline()
        timestamp = time.time()
        self._store_tweet(pipe, tweet)
        self._reference_tweet(pipe, timestamp, query, tweet)
        self._update_last_query_tweet(pipe, query, tweet)
        return pipe.execute()

    def retrieve_ts(self, query, timestamp, n_elements=-1):
        ts_key = self._ts_key(timestamp, query)
        return self._redis.lrange(ts_key, 0, n_elements)
    def retrieve_last_tweet_id(self, query):
        query_key = self._query_key(query)
        return self._redis.get(query_key)
    def retrieve_tweet(self, tweet_id):
        tweet_key = self._tweet_key(tweet_id)
        data = self._redis.get(tweet_key)
        return Tweet.deserialize(data).todict()
    def retrieve(self, query, n_periods=30):
        current_timestamp = now = int(time.time())
        start_timestamp = now - self._delta_secs * n_periods
        tweets = []
        while current_timestamp > start_timestamp:
            current_tweet_ids = self.retrieve_ts(query, current_timestamp)
            tweets.append({'timestamp': current_timestamp,
                           'tweets' : [ self.retrieve_tweet(tid) for tid in current_tweet_ids ] })
            current_timestamp -= self._delta_secs 
        return { 'now' : now,
                 'ts' : tweets }
開發者ID:oddskool,項目名稱:varan,代碼行數:79,代碼來源:ts_store.py


注:本文中的redis.client.Redis.lrange方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。