【发布时间】:2015-05-29 10:40:38
【问题描述】:
我在 ubuntu EC2 节点上有一个 django 项目,我想设置一个缓存,我正在关注 http://michal.karzynski.pl/blog/2013/07/14/using-redis-as-django-session-store-and-cache-backend/ 以使用 redis。在文章中作者参考https://docs.djangoproject.com/en/1.7/topics/cache/,基于此我可以做到:
(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ python manage.py shell
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import django
>>> import redis
>>> from django.core.cache import cache
>>> cache.set('my_key','hi world')
>>> cache.get('my_key')
'hi world'
我当前的 django 视图包含;
def index(token):
html = calculator(token)
print('here1')
import redis
from django.core.cache import cache
cache.set('my_key', 'hello, world!', 60*60*12)
print('here2')
return html
但是,当我触发索引功能时,没有任何内容保存到缓存中。我从命令行检查后。
如何让缓存工作?
编辑:
>>> print(settings.CACHES)
{'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}}
【问题讨论】:
-
你的缓存设置是什么样的?
-
“导入redis”的目的是什么?您在下面不使用它,因此正如 Daniel 所说,您的缓存设置很重要。在 shell 和你的索引函数中打印它
-
从 django.conf 导入设置打印 settings.CACHES
-
我已经删除了“import redis”,请查看编辑
-
好的,您正在使用 LocMemCache 后端,这意味着您正在使用 Python 进程的内存来缓存数据。这也意味着您的数据将在进程终止后立即丢失。这也意味着你没有使用 Redis
标签: python django caching redis