def publish(self, channel, data, timestamp=None): if not timestamp: timestamp = time.time() queue = ConnectionPool().getConnection() logging.getLogger().debug('Event:publish(%s): %s ' % (channel, data)) message = json.dumps(data) queue.publish(channel, message) queue.zadd(channel, timestamp, message) queue.expire(channel, self.timeout)
def subscribe(self, channel, lastId=None): queue = ConnectionPool().getConnection() if not lastId: lastId = time.time() - self.timeout # remove stale messages in the archive queue.zremrangebyscore(channel,'-inf','%s' % lastId) # then get usable messages that are in the archive events = queue.zrangebyscore(channel, '(%s' % (float(lastId)+0.0001), '+inf') # @todo: # Implement compaction here # - reverse the list # - if there is newer event about a resource - ignore all the old # for this event for event in events: yield event pubsub = queue.pubsub() pubsub.subscribe(channel) for event in pubsub.listen(): logging.getLogger().debug('Event:Subscribe() Got event: %s' % event) if event['type'] != 'message': continue yield event['data'] pubsub.unsubscribe(channel)
class Redis(): redis = None def __init__(self, prefix="props_"): # @todo: Move this to configuration file self.prefix = prefix self.redis = ConnectionPool().getConnection() def get(self, key, field=None): if field is None: return self.redis.hgetall(self.getCacheKey(key)) else: return self.redis.hget(self.getCacheKey(key), field) def set(self, key, field, value): return self.redis.hset(self.getCacheKey(key), field, value) def delete(self, key, field=None): if field is None: return self.redis.delete(self.getCacheKey(key)) else: return self.redis.hdel(self.getCacheKey(key), field) def items(): return self.redis.hgetall(self.getCacheKey(key)).items() def has_key(self, key): return self.redis.exists(self.getCacheKey(key)) def __getitem__(self, key): return self.redis.hgetall(self.getCacheKey(key)) def __delitem__(self, key): return self.delete(self.getCacheKey(key)) def getCacheKey(self, key): return "%s%s" % (self.prefix, key)
class Redis: redis = None def __init__(self, prefix="props_"): # @todo: Move this to configuration file self.prefix = prefix self.redis = ConnectionPool().getConnection() def get(self, key, field=None): if field is None: return self.redis.hgetall(self.getCacheKey(key)) else: return self.redis.hget(self.getCacheKey(key), field) def set(self, key, field, value): return self.redis.hset(self.getCacheKey(key), field, value) def delete(self, key, field=None): if field is None: return self.redis.delete(self.getCacheKey(key)) else: return self.redis.hdel(self.getCacheKey(key), field) def items(): return self.redis.hgetall(self.getCacheKey(key)).items() def has_key(self, key): return self.redis.exists(self.getCacheKey(key)) def __getitem__(self, key): return self.redis.hgetall(self.getCacheKey(key)) def __delitem__(self, key): return self.delete(self.getCacheKey(key)) def getCacheKey(self, key): return "%s%s" % (self.prefix, key)
def __init__(self, prefix="props_"): # @todo: Move this to configuration file self.prefix = prefix self.redis = ConnectionPool().getConnection()