Created
April 27, 2012 04:08
-
-
Save imankulov/2505681 to your computer and use it in GitHub Desktop.
Find out why this script doesn't work (memcached test)
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
""" | |
Why memcached doesn't work? | |
We observe weird behaviour of the function save_object_to_cache(...) | |
-------------------------------------------------------------------------------- | |
$ python /tmp/memcached_test.py | |
A new object <__main__.Wrapper object at 0x925f60c> with value "bar" has been created | |
Traceback (most recent call last): | |
File "/tmp/memcached_test.py", line 34, in <module> | |
save_object_to_cache('bar') | |
File "/tmp/memcached_test.py", line 31, in save_object_to_cache | |
client.set('foo', obj, 60) | |
File "/usr/lib/python2.7/dist-packages/memcache.py", line 565, in set | |
return self._set("set", key, val, time, min_compress_len) | |
File "/usr/lib/python2.7/dist-packages/memcache.py", line 802, in _set | |
return _unsafe_set() | |
File "/usr/lib/python2.7/dist-packages/memcache.py", line 780, in _unsafe_set | |
store_info = self._val_to_store_info(val, min_compress_len) | |
File "/usr/lib/python2.7/dist-packages/memcache.py", line 751, in _val_to_store_info | |
pickler.dump(val) | |
cPickle.PicklingError: Can't pickle <class '__main__.Wrapper'>: attribute lookup __main__.Wrapper failed | |
-------------------------------------------------------------------------------- | |
""" | |
from memcache import Client | |
def save_object_to_cache(value): | |
""" | |
Handy function to store a wrapped value in memcached | |
""" | |
# connect to memcached | |
client = Client(['127.0.0.1:11211']) | |
# create a wrapper class to work with a value and initalize its instance | |
class Wrapper(object): | |
def __init__(self, value): | |
self.value = value | |
def get_value(self): | |
return self.value | |
obj = Wrapper(value) | |
print '\nA new object %r with value "%s" has been created\n\n' % (obj, obj.get_value()) | |
# save the object to memcached | |
client.set('foo', obj, 60) | |
if __name__ == '__main__': | |
save_object_to_cache('bar') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment