簡體   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