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


Python Redis.smembers方法代碼示例

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


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

示例1: TSStore

# 需要導入模塊: from redis.client import Redis [as 別名]
# 或者: from redis.client.Redis import smembers [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

示例2: smembers

# 需要導入模塊: from redis.client import Redis [as 別名]
# 或者: from redis.client.Redis import smembers [as 別名]
 def smembers(self, name):
     """
     Return all members of the set ``name``, decoding.
     """
     values = Redis.smembers(self, name)
     return (value.decode(self.encoding) for value in values)
開發者ID:cdent,項目名稱:tiddlywebredis,代碼行數:8,代碼來源:redisstore.py

示例3: RedisSocketManager

# 需要導入模塊: from redis.client import Redis [as 別名]
# 或者: from redis.client.Redis import smembers [as 別名]

#.........這裏部分代碼省略.........
        if not socket.connection_established:
            connected = self.redis.sismember(self.connected_key, socket.sessid)
            if connected:
                self.init_connection(socket, nosync=True)

        return socket

    def save_socket(self, sessid):
        """Stores into Redis any internal state that must be shared between all sockets in the same session.
        """
        return

    def init_connection(self, socket, *args, **kwargs):
        super(RedisSocketManager, self).init_connection(socket, *args, **kwargs)
        if not kwargs.get("nosync", False):
            self.redis.sadd(self.connected_key, socket.sessid)

    def activate_endpoint(self, sessid, endpoint):
        key = self.make_session_key(sessid, "endpoints")
        self.redis.sadd(key, endpoint)
        self.notify_socket(sessid, "endpoint_activated", endpoint=endpoint)

    def deactivate_endpoint(self, sessid, endpoint):
        key = self.make_session_key(sessid, "endpoints")
        ret = self.redis.srem(key, endpoint) > 0
        if (
            ret
        ):  # only notify if the endpoint was actually removed by this manager (or we'll get in infinite loop when disconnect triggers deactivate_endpoint)
            self.notify_socket(sessid, "endpoint_deactivated", endpoint=endpoint)
        return ret

    def active_endpoints(self, sessid):
        key = self.make_session_key(sessid, "endpoints")
        return self.redis.smembers(key)

    def notify_socket(self, sessid, event, *args, **kwargs):
        msg = json.dumps(dict(uuid=self.uuid, sessid=sessid, event=event, args=args, kwargs=kwargs))
        self.redis.publish("socket.events", msg)

    def notify_endpoint(self, endpoint, sessid, event, *args, **kwargs):
        msg = json.dumps(dict(uuid=self.uuid, endpoint=endpoint, sessid=sessid, event=event, args=args, kwargs=kwargs))
        self.redis.publish("endpoint.events", msg)

    def _redis_listener(self):
        """Listens to a Redis PubSub for event messages."""
        pubsub = self.redis.pubsub()
        pubsub.subscribe(self.event_handlers.keys())

        for msg in pubsub.listen():
            if msg.get("type") == "message":
                handler = self.event_handlers.get(msg.get("channel"))
                if handler:
                    handler(msg)

            gevent.sleep(0)

    def on_socket_event(self, message):
        """Receiving a socket event from the Redis channel.
        """
        msg = json.loads(message.get("data"))
        sessid = msg.get("sessid")
        event = msg.get("event")
        args = msg.get("args", [])
        kwargs = msg.get("kwargs", {})
        uuid = msg.get("uuid")
        if uuid != self.uuid:
開發者ID:ryesoft,項目名稱:gevent-socketio,代碼行數:70,代碼來源:socket_manager.py


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