我正在使用 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'的模块”。我检查了 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
我检查了 key
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>[<Menu: root>, <Menu: \xed\x98\x84\xed\x99\xa9\xeb\xb6\x84\xec\x84\x9d>, <Menu: \xec\x9e\xac\xea\xb3\xa0\xed\x98\x84\xed\x99\xa9>, <Menu: \xed\x98\x84\xec\x9e\xac \xec\x9e\xac\xea\xb3\xa0>, <Menu: \xea\xb8\xb0\xea\xb0\x84\xec\x9e\xac\xea\xb3\xa0 \xec\xa1\xb0\xed\x9a\x8c>, <Menu: \xec\x9e\xac\xea\xb3\xa0\xea\xb8\xb0\xea\xb4\x80 \xec\xa1\xb0\xed\x9a\x8c>, <Menu: \xec\x9e\xac\xea\xb3\xa0 \xed\x9a\x8c\xec\xa0\x84\xec\x9c\xa8 \xec\xa1\xb0\xed\x9a\x8c>, <Menu: \xeb\xa7\xa4\xec\x9e\x85\xeb\xa7\xa4\xec\xb6\x9c \xeb\xb6\x84\xec\x84\x9d>, <Menu: \xea\xb8\xb0\xea\xb0\x84\xeb\xb3\x84 \xed\x98\x84\xed\x99\xa9>, <Menu: \xec\x83\x81\xed\x92\x88\xeb\xb3\x84 \xed\x98\x84\xed\x99\xa9>, <Menu: \xea\xb1\xb0\xeb\x9e\x98\xed\x98\x95\xed\x83\x9c\xeb\xb3\x84 \xed\x98\x84\xed\x99\xa9>, <Menu: \xec\x82\xac\xec\x97\x85\xeb\xb6\x80\xeb\xb3\x84 \xed\x98\x84\xed\x99\xa9>, <Menu: \xea\xb1\xb0\xeb\x9e\x98\xec\xb2\x98\xeb\xb3\x84 \xed\x98\x84\xed\x99\xa9>, <Menu: \xec\x9e\x91\xec\x97\x85\xed\x98\x84\xed\x99\xa9>, <Menu: \xec\x9e\x91\xec\x97\x85\xec\x9a\x94\xec\xb2\xad \xed\x98\x84\xed\x99\xa9>, <Menu: \xea\xb2\x80\xec\x88\x98\xec\x9a\x94\xec\xb2\xad>, <Menu: \xec\x83\x81\xed\x92\x88\xed\x99\x94 \xec\x9a\x94\xec\xb2\xad>, <Menu: \xec\xa0\x9c\xed\x92\x88\xed\x99\x94 \xec\x9a\x94\xec\xb2\xad>, <Menu: \xec\x97\xbd\xec\x97\x89\xea\xb4\x80\xeb\xa6\xac>, <Menu: \xeb\x82\xb4\xeb\xb6\x80\xec\xb2\x98\xeb\xa6\xac>, <Menu: \xed\x8c\x90\xeb\xa7\xa4\xea\xb4\x80\xeb\xa6\xac>, <Menu: \xea\xb5\xac\xeb\xa7\xa4\xea\xb4\x80\xeb\xa6\xac>, <Menu: \xed\x99\x98\xea\xb2\xbd\xec\x84\xa4\xec\xa0\x95>]</li>\n\t</ul>\n</div>\n\n<div id=\"context_processor_test2\">\n\t<ul>\n\t\t<li><QuerySet [<UserInfo: dkjfadl>, <UserInfo: fidlajf>, <UserInfo: fjialejgljadf>, <UserInfo: dfialghelfdja>, <UserInfo: dkajlf>, <UserInfo: testgroup>, <UserInfo: dfjlda>, <UserInfo: fjdalfiagda>, <UserInfo: remann>, <UserInfo: aldfjdalfj>, <UserInfo: dlkajflad>, <UserInfo: dkajfldafj>, <UserInfo: djafldijfl>, <UserInfo: testgroup>, <UserInfo: <bound method Group.__str__ of <Group: remann>>>, <UserInfo: remann>, <UserInfo: testgroup>, <UserInfo: testgroup>]></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:viewcache.decorators .cache_page"
好吧,我的问题可能没有得到很好的解释或不合时宜。 如果您需要更详细或更恰本地了解,请告诉我。
我希望有人对 Windows 操作系统有类似的问题来处理 Redis 和 Django!
感谢阅读!
最佳答案
您卸载了 Python redis
包,以为它是 Redis 服务器,但它只是一个带有代码的 Python 包,用于访问 Redis。您需要重新安装它。但你似乎已经修复了那部分。
至于如何在 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
你可以看到关键部分是如何组成的。具体来说,这个:
The cache is keyed by the URL and some data from the headers.
您在那里看到的超出预期的额外内容是来自请求的 header 数据。
关于python - Django 如何在 Windows 10 中使用 redis?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39955449/