Django如何在Windows10中使用redis?

时间:2016-10-10 09:41:19

标签: python django redis windows-10

我正在使用Django框架和Windows 10开发小型系统。我将使用Redis在内存上进行缓存。好吧,我发现Redis不正式支持Windows操作系统,但是MsOpenTech提供了Windows 64位软件包。我用巧克力包经理安装了它。 https://chocolatey.org/packages/redis-64

然后我在cmd中通过pip install安装了django-redis软件包。好吧,它同时自动安装了redis模块。我认为删除redis模块会很好,因为我已经安装了redis 3.0.503 64位(对于Win)。但它出现了错误“没有名为'redis'的mudule”。我检查了django-redis目录“django_redis-4.5.0-py3.4.egg-info”。 'requries.text'文件中有一行'redis> = 2.10.0'。所以我假设django-redis设置为默认需要redis 2.10.0。

好吧,然后我在cmd中通过pip install redis安装了redis。在Django中设置后,我将一些关键值数据保存为redis上的测试。 (通过使用cache_page装饰器)

CACHES = {
    "default" : {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTION": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        },
        "KEY_PREFIX": "Test"
    }
}

我连接到redis。

redis-cli -n 1

我检查了密钥

127.0.0.1:6379[1]> keys *

结果是这样的。空

(empty list or set)

然后我通过cache_page装饰器将测试数据放在redis上进行检查。我得到了这个结果。

1) "Test:1:views.decorators.cache.cache_page..GET.1be77f2243e36ea7e7fa68d85c1c277e.d41d8cd98f00b204e9800998ecf8427e.en-us.Korea_Standard_Time"
2) "Test:1:views.decorators.cache.cache_header..1be77f2243e36ea7e7fa68d85c1c277e.en-us.Korea_Standard_Time"

我用get命令检查了它。它应该显示呈现的HTML字符串。

get "Test:1:views.decorators.cache.cache_page..GET.1be77f2243e36ea7e7fa68d85c1c277e.d41d8cd98f00b204e9800998ecf8427e.en-us.Korea_Standard_Time"

下面的结果似乎没问题。

"\x80\x04\x95\xe8\a\x00\x00\x00\x00\x00\x00\x8c\x14django.http.response\x94\x8c\x0cHttpResponse\x94\x93\x94)}\x94\x92\x94}\x94(\x8c\x06closed\x94\x89\x8c\b_charset\x94N\x8c\x0e_handler_class\x94N\x8c\b_headers\x94}\x94(\x8c\x0ccontent-type\x94\x8c\x0cContent-Type\x94\x8c\x18text/html; charset=utf-8\x94\x86\x94\x8c\aexpires\x94\x8c\aExpires\x94\x8c\x1dMon, 10 Oct 2016 09:32:19 GMT\x94\x86\x94\x8c\rcache-control\x94\x8c\rCache-Control\x94\x8c\nmax-age=60\x94\x86\x94\x8c\rlast-modified\x94\x8c\rLast-Modified\x94\x8c\x1dMon, 10 Oct 2016 09:31:19 GMT\x94\x86\x94u\x8c\x0e_reason_phrase\x94N\x8c\acookies\x94\x8c\x0chttp.cookies\x94\x8c\x0cSimpleCookie\x94\x93\x94)}\x94\x92\x94\x8c\x11_closable_objects\x94]\x94\x8c\n_container\x94]\x94B/\x06\x00\x00<!DOCTYPE html>\n<html lang=\"ko\">\n\n<head>\n<title>Menu front page</title>\n</head>\n\n<body>\n<div id=\"context_processor_test\">\n\t<ul>\n\t\t<li>[&lt;Menu: root&gt;, &lt;Menu: \xed\x98\x84\xed\x99\xa9\xeb\xb6\x84\xec\x84\x9d&gt;, &lt;Menu: \xec\x9e\xac\xea\xb3\xa0\xed\x98\x84\xed\x99\xa9&gt;, &lt;Menu: \xed\x98\x84\xec\x9e\xac \xec\x9e\xac\xea\xb3\xa0&gt;, &lt;Menu: \xea\xb8\xb0\xea\xb0\x84\xec\x9e\xac\xea\xb3\xa0 \xec\xa1\xb0\xed\x9a\x8c&gt;, &lt;Menu: \xec\x9e\xac\xea\xb3\xa0\xea\xb8\xb0\xea\xb4\x80 \xec\xa1\xb0\xed\x9a\x8c&gt;, &lt;Menu: \xec\x9e\xac\xea\xb3\xa0 \xed\x9a\x8c\xec\xa0\x84\xec\x9c\xa8 \xec\xa1\xb0\xed\x9a\x8c&gt;, &lt;Menu: \xeb\xa7\xa4\xec\x9e\x85\xeb\xa7\xa4\xec\xb6\x9c \xeb\xb6\x84\xec\x84\x9d&gt;, &lt;Menu: \xea\xb8\xb0\xea\xb0\x84\xeb\xb3\x84 \xed\x98\x84\xed\x99\xa9&gt;, &lt;Menu: \xec\x83\x81\xed\x92\x88\xeb\xb3\x84 \xed\x98\x84\xed\x99\xa9&gt;, &lt;Menu: \xea\xb1\xb0\xeb\x9e\x98\xed\x98\x95\xed\x83\x9c\xeb\xb3\x84 \xed\x98\x84\xed\x99\xa9&gt;, &lt;Menu: \xec\x82\xac\xec\x97\x85\xeb\xb6\x80\xeb\xb3\x84 \xed\x98\x84\xed\x99\xa9&gt;, &lt;Menu: \xea\xb1\xb0\xeb\x9e\x98\xec\xb2\x98\xeb\xb3\x84 \xed\x98\x84\xed\x99\xa9&gt;, &lt;Menu: \xec\x9e\x91\xec\x97\x85\xed\x98\x84\xed\x99\xa9&gt;, &lt;Menu: \xec\x9e\x91\xec\x97\x85\xec\x9a\x94\xec\xb2\xad \xed\x98\x84\xed\x99\xa9&gt;, &lt;Menu: \xea\xb2\x80\xec\x88\x98\xec\x9a\x94\xec\xb2\xad&gt;, &lt;Menu: \xec\x83\x81\xed\x92\x88\xed\x99\x94 \xec\x9a\x94\xec\xb2\xad&gt;, &lt;Menu: \xec\xa0\x9c\xed\x92\x88\xed\x99\x94 \xec\x9a\x94\xec\xb2\xad&gt;, &lt;Menu: \xec\x97\xbd\xec\x97\x89\xea\xb4\x80\xeb\xa6\xac&gt;, &lt;Menu: \xeb\x82\xb4\xeb\xb6\x80\xec\xb2\x98\xeb\xa6\xac&gt;, &lt;Menu: \xed\x8c\x90\xeb\xa7\xa4\xea\xb4\x80\xeb\xa6\xac&gt;, &lt;Menu: \xea\xb5\xac\xeb\xa7\xa4\xea\xb4\x80\xeb\xa6\xac&gt;, &lt;Menu: \xed\x99\x98\xea\xb2\xbd\xec\x84\xa4\xec\xa0\x95&gt;]</li>\n\t</ul>\n</div>\n\n<div id=\"context_processor_test2\">\n\t<ul>\n\t\t<li>&lt;QuerySet [&lt;UserInfo: dkjfadl&gt;, &lt;UserInfo: fidlajf&gt;, &lt;UserInfo: fjialejgljadf&gt;, &lt;UserInfo: dfialghelfdja&gt;, &lt;UserInfo: dkajlf&gt;, &lt;UserInfo: testgroup&gt;, &lt;UserInfo: dfjlda&gt;, &lt;UserInfo: fjdalfiagda&gt;, &lt;UserInfo: remann&gt;, &lt;UserInfo: aldfjdalfj&gt;, &lt;UserInfo: dlkajflad&gt;, &lt;UserInfo: dkajfldafj&gt;, &lt;UserInfo: djafldijfl&gt;, &lt;UserInfo: testgroup&gt;, &lt;UserInfo: &lt;bound method Group.__str__ of &lt;Group: remann&gt;&gt;&gt;, &lt;UserInfo: remann&gt;, &lt;UserInfo: testgroup&gt;, &lt;UserInfo: testgroup&gt;]&gt;</li>\n\t</ul>\n</div>\n<!--\n<div id=\"top_menu\">\n\t\n</div>\n-->\n\n\n\n\n</body>\n</html>\n\n\x94aub."

因为我的模板代码如下所示。

<!DOCTYPE html>
<html lang="ko">

<head>
<title>{% block title %}Menu front page{% endblock %}</title>
</head>

<body>
<div id="context_processor_test">
    <ul>
        <li>{{ menu_list }}</li>
    </ul>
</div>

<div id="context_processor_test2">
    <ul>
        <li>{{ user_info_list }}</li>
    </ul>
</div>
<!--
<div id="top_menu">
    {% for top_menu in root.get_children %}
    <ul style="display:inline-block;">
        <li><a href="{{top_menu.menu_alias}}"> {{ top_menu.menu_name }} </a></li>
    </ul>
    {% endfor %}
</div>
-->
{% block content %}{% endblock %}

{% block footer %}{% endblock %}

</body>
</html>

我的问题是

1)使用此设置在Windows上使用redis是否可以?我想知道它是否正常工作。

2)我想知道它为什么命名为“Test:1:views.decorators.cache.cache_page..GET.1be77f2243e36ea7e7fa68d85c1c277e.d41d8cd98f00b204e9800998ecf8427e.en-us.Korea_Standard_Time”而不仅仅是“Test:1:views.decorators.cache” .cache_page“

好吧,我的问题可能没有得到很好的解释或说不清楚。 如果您需要更详细或更正确地了解,请告诉我。

我希望有些人在使用Windows操作系统时遇到类似问题来处理Redis和Django!

感谢您阅读!

1 个答案:

答案 0 :(得分:1)

您卸载了Python redis包,认为它是Redis服务器,但它只是一个包含访问 Redis代码的Python包。你需要重新安装它。但是你似乎已经解决了这个问题。

至于在Windows 10上使用Redis和Django的安装方式,我想说这取决于你想做什么。如果你只是想玩它来学习,你应该没事。另一方面,如果你想进行更严肃的开发,我会以另一种方式安装Redis。以下是我能想到的一些可能性:

  • 使用VirtualBox,VMWare或类似的东西在虚拟机中安装Linux,并在那里安装Redis;
  • Windows 10现在可以在其中运行Ubuntu Linux,因此安装Redis可能是最简单的选择;
  • 使用Docker容器运行Redis(使用Docker for Windows:https://docs.docker.com/engine/getstarted/然后您可以从此处获取Redis图像:https://hub.docker.com/_/redis/

最后,关于缓存条目具有这些名称的原因,为缓存条目创建的默认密钥Django缓存定义为here (django/core/cache/backends/base.py:27)

def default_key_func(key, key_prefix, version):
    """
    Default function to generate keys.

    Constructs the key used by all other methods. By default it prepends
    the `key_prefix'. KEY_FUNCTION can be used to specify an alternate
    function with custom key making behavior.
    """
    return '%s:%s:%s' % (key_prefix, version, key)

在您的情况下,密钥为Test:1:views.decorators.cache.cache_page..GET.1be77f2243e36ea7e7fa68d85c1c277e.d41d8cd98f00b204e9800998ecf8427e.en-us.Korea_Standard_Time,因此会给出以下细分:

key_prefix = Test
version = 1
key = views.decorators.cache.cache_page..GET.1be77f2243e36ea7e7fa68d85c1c277e.d41d8cd98f00b204e9800998ecf8427e.en-us.Korea_Standard_Time

如果查看django/views/decorators/cache.py:10,您可以看到关键部分的组成方式。具体来说,这个:

  

缓存由URL和标题中的一些数据键入。

您在预期之外看到的额外内容是来自请求的标题数据。