0

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.

enter image description here

What am I missing?

Gokhan Sari
  • 7,185
  • 5
  • 34
  • 32
Milano
  • 18,048
  • 37
  • 153
  • 353
  • Well, you've set the cache backend (twice, for some reason, but the second setting is no longer used), but are you actually caching anything? If so where? – Daniel Roseman Jan 02 '17 at 20:46
  • I thought that it will cache any db lookups and another expensive actions. According to title Caching the Entire Site in this tutorial: https://www.tutorialspoint.com/django/django_caching.htm Isn't it possible to make django do caching automatically? – Milano Jan 02 '17 at 20:49
  • I've set @cache_control(must_revalidate=True, max_age=3600) on one view and it did not cache anything. – Milano Jan 02 '17 at 21:00
  • Please post the view that you are accessing here – e4c5 Jan 03 '17 at 07:50

1 Answers1

0

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

Alex Vyushkov
  • 650
  • 1
  • 6
  • 21