caching - Django-redis-cache failing to fetch data from redis -


i have question regarding working of package. how writes redis db ?

this settings redis -

caches = {      'default': {     'backend': 'redis_cache.rediscache',     'location': '/var/run/redis/redis.sock',     'options': {         'db': 2,     }, }, } 

this views file,

def postview(request):     print("working")    #post_list = post.objects.all()     if cache.get("posts") == none:             post_list = post.objects.all()             print("going cached")             = cache.get("aman")             print("aman ", a)             cache.set("posts", post_list, timeout=60*100*10)             print("cached")     else :             post_list = cache.get("posts")             aman = cache.get("aman")             print(aman, " aman's job")             print("already present in cache")     context = {"post_list" : post_list}     print("problem")     return render(request, 'post_list.html', context)  @cache_page(60*15*10, key_prefix="cache_redis") def testview(request):     post_list = cache.get("posts")     print("post_list is", post_list)     return render(request, 'post_list.html', {"post_list":post_list})  @cache_page(60*25*10, key_prefix="cache_test") def new(request):     print("hey")     print("cache_page working")     return httpresponse("hello, mohammed") 

this redis -cli ,

luvpreet@dhari-inspiron-3542:~/test_venv_wrapper/test_redis/cache_redis$ redis-cli 127.0.0.1:6379> select 2  ok 127.0.0.1:6379[2]> set "a" "aman"  ok 127.0.0.1:6379[2]> set ":1:a" "theman"  ok  127.0.0.1:6379[2]> keys *  1) "a"  2)":1:views.decorators.cache.cache_page..get.ad00468064711919773512f81be0dbc4.d41d8cd98f00b204e9800998ecf8427e.en-us.utc"  3) ":1:posts"  4) ":1:a"  5) ":1:aman"  6) ":1:views.decorators.cache.cache_header.cache_test.ad00468064711919773512f81be0dbc4.en-us.utc"  7) ":1:views.decorators.cache.cache_header..ad00468064711919773512f81be0dbc4.en-us.utc"  8) ":1:b"  9)":1:views.decorators.cache.cache_page.cache_test.get.ad00468064711919773512f81be0dbc4.d41d8cd98f00b204e9800998ecf8427e.en-us.utc" 10) "aman" 127.0.0.1:6379[2]> ":1:a" "theman" 127.0.0.1:6379[2]> "a" "aman" 

this corresponding redis-cli monitor

luvpreet@dhari-inspiron-3542:~/test_venv_wrapper/test_redis/cache_redis$ redis-cli monitor ok 1491412249.001149 [0 unix:/var/run/redis/redis.sock] "info" 1491412249.086196 [0 127.0.0.1:44984] "select" "2" 1491412250.001249 [0 unix:/var/run/redis/redis.sock] "info" 1491412257.001426 [0 unix:/var/run/redis/redis.sock] "info" 1491412257.423536 [2 127.0.0.1:44984] "set" "a" "aman" 1491412258.001311 [0 unix:/var/run/redis/redis.sock] "info" 1491412269.001211 [0 unix:/var/run/redis/redis.sock] "info" 1491412269.820886 [2 127.0.0.1:44984] "set" ":1:a" "theman" 1491412270.000741 [0 unix:/var/run/redis/redis.sock] "info" 1491412272.955386 [2 127.0.0.1:44984] "keys" "*" 1491412273.001121 [0 unix:/var/run/redis/redis.sock] "info" 1491412340.991928 [2 127.0.0.1:44984] "get" ":1:a" 1491412341.002001 [0 unix:/var/run/redis/redis.sock] "info" 1491412344.106985 [2 127.0.0.1:44984] "get" "a" 1491412345.001677 [0 unix:/var/run/redis/redis.sock] "info" 

this means data manually inserting database 2 available , can fetch through redis-cli.

but when try fetch manually entered data django-app python shell, happens,

>>> django.core.cache import cache >>> cache.get("a") traceback (most recent call last):    file "<console>", line 1, in <module>    file "/usr/local/lib/python2.7/dist-packages/redis_cache/backends/base.py", line 33, in wrapped      return method(self, client, key, *args, **kwargs)    file "/usr/local/lib/python2.7/dist-packages/redis_cache/backends/base.py", line 259, in      value = self.get_value(value)    file "/usr/local/lib/python2.7/dist-packages/redis_cache/backends/base.py", line 210, in get_value      value = self.deserialize(value)    file "/usr/local/lib/python2.7/dist-packages/redis_cache/backends/base.py", line 197, in deserialize      return self.serializer.deserialize(value)    file "/usr/local/lib/python2.7/dist-packages/redis_cache/serializers.py", line 42, in deserialize      return pickle.loads(force_bytes(value)) unpicklingerror: not find mark 

here redis-cli monitor corresponding it,

ok 1491413058.004167 [0 unix:/var/run/redis/redis.sock] "info" 1491413059.002746 [0 unix:/var/run/redis/redis.sock] "info" 1491413060.663292 [2 unix:/var/run/redis/redis.sock] "get" ":1:a" 1491413061.001167 [0 unix:/var/run/redis/redis.sock] "info" 

why cannot access data written manually ? know adds prefix data written through django. prefix ":1:key_name". that's why added 2 keys namely "a" , ":1:a". when try access "a", call ":1:a".

but error comes up. so, might writing data redis other way. please tell me error , tell me way in writes data.

the error gettting not data retreival error, data being retreived, not in correct pickle format.

  • when setting data manually, being set in byte format, not pickled format.
  • when set data through django-redis, data first serialized using cpickle, , stored redis pickled string.

  • when retreive data stored through django-redis, retreived pickle serialized string, , deserialized corresponding python datatype.

  • your manually entered data string type, not in correct pickle format, hence though gets retreived, cannot converted corresponding python type, , pickling error.
  • the solution use django-redis in django shell or code store , retreive data, entering data manually breaks serialization contract.

example:

>>> django.core.cache import cache >>> cache.set("the_key","aman") true >>> cache.get("the_key") 'aman' >>>  dhruv@dhruvpathak:~$ redis-cli 127.0.0.1:6379> keys *  1) ":1:the_key" 127.0.0.1:6379> ":1:the_key" "\x80\x02u\x04amanq\x01." 127.0.0.1:6379> set "the_manual_key" "aman" ok 127.0.0.1:6379> "the_manual_key"  "aman" 

Comments