-
-
Save cosenal/168c81fed721ea9fb4809e8b80b17480 to your computer and use it in GitHub Desktop.
redis checksum
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
# 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