Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django cache.clear() not working

I have set up memcached on my machine, and added it to a view like so:

@cache_page(3600)
def course_list(request, market_code, destination=None, course_type=None):    
    template = 'course_list.html'
    ...
    ..

And I have set up a view that clears the cache when visited

from django.core.cache import cache
class ClearCacheView(TemplateView):
    """
    This view will clear the cache and display a message to the user saying so
    """
    template_name = 'ebsadmin/cache_clear_success.html'
    def get_context_data(self, **kwargs):
        cache.clear()
        return super(ClearCacheView, self).get_context_data(**kwargs)

When I visit the ClearCacheView, the success template is displayed, but the item in my cache is not deleted.

setings.py

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

    },
}

I have a timestamp in the template, so I can tell that the cache is being used or not:

<!-- {% now "jS F Y H:i:s" %} -->

cache.clear should clear everything, as far as I understand. So why isn't it doing anything here?

like image 885
wobbily_col Avatar asked Feb 13 '26 02:02

wobbily_col


1 Answers

You are using the right syntax for clearing the cache as specified in the Django cache documentation for Memcached.

You may need to verify memcached daemon is running, if it's not running django cache commands can result with no output. Try re-running it with memcached to see if the port is already taken.

like image 79
Forge Avatar answered Feb 15 '26 14:02

Forge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!