Skip to content

Instantly share code, notes, and snippets.

@jpic
Created September 25, 2011 22:58
Show Gist options
  • Select an option

  • Save jpic/1241291 to your computer and use it in GitHub Desktop.

Select an option

Save jpic/1241291 to your computer and use it in GitHub Desktop.
class RedisBackend(base.BaseBackend):
def __init__(self, prefix='subscription'):
self.prefix = prefix
@property
def redis(self):
if not hasattr(self, '_redis'):
self._redis = redis.Redis()
return self._redis
def queue(self, notification, queue):
self.redis.lpush(queue, pickle.dumps(notification))
def move_queue(self, source, destination):
notifications = self.redis.lrange(source, 0, -1)
for notification in notifications:
self.redis.lpush(destination, notification)
self.redis.lrem(source, notification)
def get_notifications(self, queue, limit=-1):
if limit > 0:
queue_limit = limit - 1
elif limit == 0:
return []
else:
queue_limit = limit
return [pickle.loads(n) for n in self.redis.lrange(queue, 0, queue_limit)]
def count_notifications(self, queue):
return self.redis.llen(queue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment