Posts for: #Redis

Redis Speed

http://www.digitaldragoon.com/blog/testing-speed-redis-py/

have pget and pset inside, I change to this


import cPickle as pickle
import zlib
import new

####################################
# gzip header
gzip_header = zlib.compress(’’)[:2]
####################################
# CUSTOM COMMANDS for Redis
####################################

def _pset(self, name, object, compress=False):
""" pickle set for redis client, with compress flag
“”"
pobj = pickle.dumps(object, pickle.HIGHEST_PROTOCOL)
if compress:
pobj = zlib.compress(pobj)

self.set(name, pobj)

def _pget(self, name):
""" pickle get for redis client, automatic to check compress flag
“”"
pobj = self.get(name)
if pobj is None: # No value for this key, let’s go home :-(
return None
else:
if pobj[:2] == gzip_header:
# it compressed data
pobj = zlib.decompress(pobj)

load_object= pickle.loads(pobj)
return load_object

redis.Redis.pset = new.instancemethod(_pset, None, redis.Redis)
redis.Redis.pget = new.instancemethod(_pget, None, redis.Redis)