I've got a pretty simple site where I'm using the page_cache
decorator.
I have a cronjob that checks for new data and processes it if it's available.
(This is run using management commands executed with crontab)
I want to then clear all the page caches when new data is processed.
I'm looking at the documentation here: https://docs.djangoproject.com/en/dev/topics/cache/?from=olddocs?from=olddocs
and found cache.clear()
, which seems to be what I want.
I've added a flag to the data processing portion and execute cache.clear()
when new data is found.
However, after the command runs the cache isn't cleared. (I've cleared the browser cached and checked to be sure it's not the browser)
Does cache.clear()
not work in to clear all cached pages?
I'm using the DatabaseCache
, so I guess I could go in and clear the cache table manually, but is there a better way?
It's a bug #19896 that looks to be fixed in 1.6.
If you are using an older version doing something like the following should make the clear work as expected.
This just makes sure that the transaction gets committed.
Put something into cache, then try to call
cache.clear()
from themanage.py shell
console and then manually check database cache contents. If that works then maybe yourcache.clear()
is just not called when new data is found.The easiest way to understand what is going under the hood is just to put
import pdb; pdb.set_trace()
to the beginning of thecache.clear()
function, then run debug server and wait, then some code call this func you'll be able to execute step-by-step its code or you'll just see that this func is not called as your expected.I've had this problem with an SQLite database cache - the
clear()
method doesn't clear the cache although it works fine with a MySQL database cache. It seems that a SQLite cache needs a call todjango.db.transation.commit_unless_managed()
after theDELETE from [table]
statement is run.I have been using multiple caches since before official support was added into core as part of 1.3 and so have a wrapper round several of the cache calls - including
clear()
- so I was able to override this method and include thecommit_unless_managed()
. I think I should probably log it as a bug.Here's the outline of the code I'm using to flush a memcache cache (the default cache in
django.core.cache
) and a database cache stored in thecache_table
of thesettings.DATABASES['cache_database']
database.Rather than being lazy and hard coding it the way I have it should be pretty easy to get the values from
settings.CACHES
anddjango.db.router
.