【问题标题】:Django-redis-cache failing to fetch data from redisDjango-redis-cache 无法从 redis 获取数据
【发布时间】:2017-08-31 12:24:07
【问题描述】:

我有一个关于这个包的工作的问题。它是如何写入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 caching redis django-redis


    【解决方案1】:

    您遇到的错误不是数据检索错误,正在检索数据,但它的 pickle 格式不正确。

    • 当您手动设置数据时,它被设置为字节格式,而不是腌制格式。
    • 当你通过django-redis设置数据时,你的数据首先使用cPickle序列化,然后作为pickle字符串存储到redis中。

    • 在检索通过django-redis存储的数据时,将其作为pickle序列化字符串进行检索,然后反序列化为对应的python数据类型。

    • 您手动输入的数据是字符串类型,但pickle格式不正确,因此虽然被检索到,但无法转换成对应的python类型,出现pickle错误。
    • 解决方案是在 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"
    

    【讨论】:

    • 就是这样!很好解释!我在github上提出了一个问题,只是为了了解这些步骤。我不知道数据在缓存之前被序列化(通过 cache_page 装饰器)。这解释了我的每一个疑问。谢谢 dhruv bhai
    • 我现在这样做了,a="aman"; import cPickle; a=cPickle.dumps(a);这是 a now "S'aman backend'\np1\n." 的值,我复制了这个 a 并在 redis-cli set "key_name" "value" 中做了这个。这个值是 a 的复制值(来自 python 代码)。现在在 shell 中,我试图检索它。并且发生了这个错误。 django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. 我无法复制完整的跟踪,因为它很长。
    猜你喜欢
    • 1970-01-01
    • 2019-03-27
    • 2016-09-23
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    相关资源
    最近更新 更多