Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best place to clear cache when restarting django server

I want memcached to be flushed on every restart/reload of django server. I use cherrypy for production and builtin server for development.

I would add this to settings.py, right after CACHES:

from django.core.cache import cache cache.clear() 

but it makes a recursive import:

Error: Can't find the file 'settings.py' in the directory containing 'manage.py'. It appears you've customized things. You'll have to run django-admin.py, passing it your settings module. (If the file settings.py does indeed exist, it's causing an ImportError somehow.) make: *** [server] Error 1 

Any other suggestions? Thanks.

like image 892
Motiejus Jakštys Avatar asked May 09 '11 21:05

Motiejus Jakštys


People also ask

Where is Django cache stored?

Local Memory Cache Unless we explicitly specify another caching method in our settings file, Django defaults to local memory caching. As its name implies, this method stores cached data in RAM on the machine where Django is running. Local memory caching is fast, responsive, and thread-safe.

Does Django automatically cache?

Django stated in their docs that all query sets are automatically cached, https://docs.djangoproject.com/en/dev/topics/db/queries/#caching-and-querysets.


1 Answers

It's bad practice to put code in settings.py other than assignments. It's better suited as a management command:

from django.core.management.base import BaseCommand from django.core.cache import cache  class Command(BaseCommand):     def handle(self, *args, **kwargs):         cache.clear()         self.stdout.write('Cleared cache\n') 

Which you can add to your project by sticking it in someapp/management/commands. For instance you could create a new app called utils and add that to your INSTALLED_APPS and the directory structure would look like this:

utils ├── __init__.py └── management     ├── __init__.py     └── commands         ├── __init__.py         └── clearcache.py 

You can now clear cache by doing ./manage.py clearcache. If you want to run clearcache every time you runserver you can just write a shell alias to do that:

alias runserver='./manage.py clearcache && ./manage.py runserver' 

Alternately I think you can write it as a stand-alone script and configure the settings it requires by hand:

from django.conf import settings  # obviously change CACHES to your settings CACHES = {     'default': {         'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',         'LOCATION': 'unique-snowflake'     } }  settings.configure(CACHES=CACHES) # include any other settings you might need  from django.core.cache import cache cache.clear() 

Writing your stand-alone script like this will prevent circular imports, and allow you to import it from your settings.py. Although there is no guarantee that settings.py will be imported only once, so in general I'd avoid this. It'd be nice if the signal framework could fire off an event once every time the app is started, after settings are loaded for stuff like this.

like image 121
zeekay Avatar answered Sep 28 '22 09:09

zeekay