簡體   English   中英

Django-redis-cache無法從Redis獲取數據

[英]Django-redis-cache failing to fetch data from redis

我對這個程序包的工作有疑問。 它如何寫入redis db?

這是我對Redis的設置-

CACHES = {
     'default': {
    'BACKEND': 'redis_cache.RedisCache',
    'LOCATION': '/var/run/redis/redis.sock',
    'OPTIONS': {
        'DB': 2,
    },
},
}

這是我的意見檔案,

def postview(request):
    print("Working")
   #post_list = Post.objects.all()
    if cache.get("posts") == None:
            post_list = Post.objects.all()
            print("going to be cached")
            a = 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 is working")
    return HttpResponse("Hello, I am Mohammed")

這是我的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]> get ":1:a"
"theman"
127.0.0.1:6379[2]> get "a"
"aman"

這是對應的redis-cli監視器

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"

這意味着可以手動插入數據庫2中的數據,並且可以通過redis-cli進行獲取。

但是,當我嘗試從django-app python shell獲取手動輸入的數據時,就會發生這種情況,

>>> from 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 get
     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: could not find MARK

這是與之相對應的redis-cli監視器,

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"

為什么不能訪問手動寫入的數據? 我知道它為通過django寫入的數據添加了前綴。 前綴是":1:key_name" 這就是為什么我添加了兩個鍵,即“ a”和“:1:a”的原因。 這樣,當我嘗試訪問“ a”時,它將調用“:1:a”。

但是出現此錯誤。 因此,它肯定是通過其他方式將數據寫入Redis。 請告訴我有關錯誤的信息,並告訴我其寫入數據的方式。

您得到的錯誤不是數據檢索錯誤,正在檢索數據,但不是正確的泡菜格式。

  • 當您手動設置數據時,它是以字節格式而不是腌制格式設置的。
  • 通過django-redis設置數據時,首先使用cPickle序列化數據,然后將其作為腌制字符串存儲到redis中。

  • 檢索通過django-redis存儲的數據時,會將其檢索為pickle序列化的字符串,然后反序列化為相應的python數據類型。

  • 您手動輸入的數據是字符串類型,但不是正確的泡菜格式,因此盡管檢索了該數據,但無法將其轉換為相應的python類型,並且出現了酸洗錯誤。
  • 解決方案是在django shell或代碼中使用django-redis本身來存儲和檢索數據,手動輸入數據會破壞序列化協定。

例:

>>> from 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> get ":1:THE_KEY"
"\x80\x02U\x04amanq\x01."
127.0.0.1:6379> set "THE_MANUAL_KEY" "aman"
OK
127.0.0.1:6379> get "THE_MANUAL_KEY" 
"aman"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM