Skip to content

Instantly share code, notes, and snippets.

@mjallday
Created October 31, 2012 17:06
Show Gist options
  • Save mjallday/3988344 to your computer and use it in GitHub Desktop.
Save mjallday/3988344 to your computer and use it in GitHub Desktop.
Redis Circular Buffer
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
) + ']'
@DiTsi
Copy link

DiTsi commented Sep 2, 2021

Thank you! It's useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment