Skip to content

Instantly share code, notes, and snippets.

@mjallday
Created October 31, 2012 17:06
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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
) + ']'
@svvitale
Copy link

This is great, thanks for sharing!

@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