# key-based invalidation of template fragment does not work

**URL:** <https://forum.djangoproject.com/t/key-based-invalidation-of-template-fragment-does-not-work/16826>\
**Category:** Using Django\
**Created:** [November 4, 2022, 6:27pm UTC](https://forum.djangoproject.com/t/key-based-invalidation-of-template-fragment-does-not-work/16826 "2022-11-04T18:27:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darthnash](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/darthnash/32/10689_2.png) [@Darthnash](https://forum.djangoproject.com/u/Darthnash)\
**Post date:** [November 4, 2022, 6:27pm UTC](https://forum.djangoproject.com/t/key-based-invalidation-of-template-fragment-does-not-work/16826/1 "2022-11-04T18:27:10Z")

</div>

Hi there,

I’m currently trying to speed up the rendering of a template by caching the relevant section with:

```
{% load cache %}

{% cache None repotree %}

<expensive operation>

{% endcache %}

```

So far, everything works as expected: when the site gets loaded for the first time it takes ~600 ms and only ~5 ms for every access after that.

When the data in the database gets updated, I would like to invalidate the cache. In my code I have:

```
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key

key = make_template_fragment_key('repotree')
cache.delete(key)

```

`cache.delete(key)` returns `True` but when I access the page, it still contains the old data.

When I delete the entire cache with `cache.clear()` new data is displayed afterwards. However, I would prefer to only invalidate the specific key.

Caching is configured as follows:

```
CACHES = {                      
    'default': {                                                      
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': '/tmp/django_cache',            
    }                                           
}

```

My application runs in a docker container.

I’ve tried to run `django-debug-toolbar` but couldn’t get it to work so far.

Any help would be greatly appreciated.

Lukas
