我在一个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'}}发布于 2015-03-25 17:50:10
关键是缓存配置,它应该是:
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': '/var/run/redis/redis.sock',
},
}(cf http://michal.karzynski.pl/blog/2013/07/14/using-redis-as-django-session-store-and-cache-backend/)
发布于 2021-06-26 14:19:30
Rapha l Braud答复的最新版本:
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': '/var/run/redis/redis.sock',
},https://stackoverflow.com/questions/29261104
复制相似问题