繁体   English   中英

Redis 缓存不适用于 Django

[英]Redis cache not working with django

我在 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'}}

关键是你的 CACHES 配置,它应该是:

CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': '/var/run/redis/redis.sock',
    },
}

(参见http://michal.karzynski.pl/blog/2013/07/14/using-redis-as-django-session-store-and-cache-backend/

Raphaël Braud 答案的更新版本:

 'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': '/var/run/redis/redis.sock',
    },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM