Django doesn't use memcached framework

狂风中的少年 提交于 2021-02-08 11:23:21

问题


I'm trying to find out how Django caching framework works. I set memcached in settings.py but the time of loading page didn't get smaller and Django-debug-toolbar shows 0 cache calls.

This is what I've set in settings.py:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
CACHE_MIDDLEWARE_ALIAS = "default"
CACHE_MIDDLEWARE_SECONDS  = 60

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'querycount.middleware.QueryCountMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',

]

Now I refreshed two times the page with a table of objects. I thought that the second time there should be no database lookups because nothing changed.

What am I missing?


回答1:


You should put @cache_page decorator on your view to enable caching for that view. See https://docs.djangoproject.com/en/1.10/topics/cache/#the-per-view-cache for examples



来源:https://stackoverflow.com/questions/41433076/django-doesnt-use-memcached-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!