Possible Bug: when using memcache backend, fragment templates aren't stored

18 views
Skip to first unread message

Leon Albrecht

unread,
Jan 11, 2021, 10:51:05 AM1/11/21
to Django users

So, in my Project I have template that generates a lot of prcedual html (for loop with about 150 entries at once) so cahcing that "snippet" is a reasonable choice, but after activating caching for that project and this template, I still got consistent long generation times, so I looked in the logs of memcached:
<details>
<summary> Log</summary>
&lt;30 new auto-negotiating client connection

30: Client using the ascii protocol

&lt;30 get :1:views.decorators.cache.cache_header..d31012059d4bf0e4439b76cc89f20710.en-us.Europe/Berlin

&gt;30 sending key :1:views.decorators.cache.cache_header..d31012059d4bf0e4439b76cc89f20710.en-us.Europe/Berlin

&gt;30 END

&lt;30 get :1:views.decorators.cache.cache_page..GET.d31012059d4bf0e4439b76cc89f20710.3be2ac225dbd9a4a16937fe81272b22f.en-us.Europe/Berlin

&gt;30 END

&lt;30 get :1:template.cache.all_students.a798c302967db211ea753aca223d57d8

&gt;30 END

&lt;30 delete :1:template.cache.all_students.a798c302967db211ea753aca223d57d8

&gt;30 NOT_FOUND

&lt;30 set :1:views.decorators.cache.cache_header..d31012059d4bf0e4439b76cc89f20710.en-us.Europe/Berlin 1 600 29

&gt;30 STORED

&lt;30 delete :1:views.decorators.cache.cache_page..GET.d31012059d4bf0e4439b76cc89f20710.3be2ac225dbd9a4a16937fe81272b22f.en-us.Europe/Berlin

&gt;30 NOT_FOUND

&lt;30 connection closed.
</details>
So as you can see django tries to access the template.all_students.... and deletes it after finnishing work, but it never actually sets it.

During testing i tried the same with the filebased-cache, which worked and resonded after only 48ms instead of 1500-1700ms.

Here is my Configuration (cutdown and minimally changed just to get the point across):
<detail>
<summary>Config</summary>
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': 'cache:11211',
    }
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',

    'django.middleware.cache.UpdateCacheMiddleware'#cache 1

    'django.contrib.sessions.middleware.SessionMiddleware',
    
    'django.middleware.common.CommonMiddleware',

    'django.middleware.csrf.CsrfViewMiddleware',  
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',

    'app.Middleware.ForceAuth', # just enforce authentication

    'django.middleware.cache.FetchFromCacheMiddleware'#cache 2
]

INSTALLED_APPS = [
    'app.apps.appConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
</detail>

the cache tag used is:
{% cache 500 all_students search_query type|my_or_all:request.user %}

where my_or_all looks like:
@register.filter(name='my_or_all')
def my_or_all(_type:struser:User):
    if _type=='all':
        return 'all'
    else:
        return f'{_type}:{user.username}'


Reply all
Reply to author
Forward
0 new messages