Reputation: 35280
To get a key from memcache (using pylibmc), you do this:
client.set(key, {'object': 'dictionary'}, time=expire)
client.get(key)
The same in redis is this:
redis.setex(key, expire, {'object': 'dictionary'})
eval(redis.get(key) or 'None')
That last line doesn't look right to me. redis only seems to return strings. Is there a get redis to return the object in the same form that it was put in?
Upvotes: 12
Views: 9159
Reputation: 4062
Or you can even subclass Redis:
import pickle
from redis import StrictRedis
class PickledRedis(StrictRedis):
def get(self, name):
pickled_value = super(PickledRedis, self).get(name)
if pickled_value is None:
return None
return pickle.loads(pickled_value)
def set(self, name, value, ex=None, px=None, nx=False, xx=False):
return super(PickledRedis, self).set(name, pickle.dumps(value), ex, px, nx, xx)
Courtesy: https://github.com/andymccurdy/redis-py/issues/186
Upvotes: 10
Reputation: 41316
The difference is that while both memcached and redis only support string values, pylibmc
serializes the values you send it using pickle
, redis-py
just converts them to string.
If you want to do the same with redis, you can have your own functions to do the pickling for you.
def set_value(redis, key, value):
redis.set(key, pickle.dumps(value))
def get_value(redis, key):
pickled_value = redis.get(key)
if pickled_value is None:
return None
return pickle.loads(pickled_value)
Upvotes: 27