Created
October 31, 2012 17:06
-
-
Save mjallday/3988344 to your computer and use it in GitHub Desktop.
Redis Circular Buffer
This file contains 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 RedisCircularBuffer(object): | |
def __init__(self, namespace, size): | |
self.namespace = namespace | |
self.size = size | |
import redis | |
self.redis = redis.Redis() | |
def append(self, item): | |
self.redis.lpush(self.namespace, item) | |
self.redis.ltrim(self.namespace, 0, self.size) | |
def __iter__(self): | |
return (x for x in self.redis.lrange(self.namespace, 0, self.size)) | |
def __len__(self): | |
return self.redis.llen(self.namespace) | |
def __repr__(self): | |
return '[' + ','.join( | |
repr(x) for x in self | |
) + ']' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great, thanks for sharing!