Skip to content

Instantly share code, notes, and snippets.

@cosenal
Last active November 8, 2016 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosenal/168c81fed721ea9fb4809e8b80b17480 to your computer and use it in GitHub Desktop.
Save cosenal/168c81fed721ea9fb4809e8b80b17480 to your computer and use it in GitHub Desktop.
redis checksum
# We use the redis-py library https://github.com/andymccurdy/redis-py
import redis
# This library is taken verbatim from https://gist.github.com/larsimmisch/9915766
from libs.crc64 import crc64
redis_client = redis.from_url(REDIS_URL, charset="utf-8", decode_responses=True)
def store(l):
values = []
for v in l:
cs = crc64(v)
values.append('{}:{}'.format(cs, v))
pipe = managers.redis.pipeline()
pipe.rpush(key, *values)
pipe.expire(key, TTL)
pipe.execute()
def fetch(key):
for value in managers.redis.lrange(key, 0, -1):
idx = value.index(':')
cs, v = value[:idx], value[idx+1:]
cs = int(cs)
if crc64(v) != cs:
raise Exception('invalid checksum for buffer storage data cs={} value={}'.format(cs, v))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment