Created
September 25, 2011 22:58
-
-
Save jpic/1241291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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