My Django site recently started throwing errors from my caching code and I can\'t figure out why...
I call:
from django.core.cache import cache
cache
I solved this problem by creating a custom cache backend, overriding the _base_set() function and changing the INSERT INTO statement like this. This SQL trick prevents the INSERT from happening in the case the cache_key already exists.
cursor.execute("INSERT INTO %s (cache_key, value, expires) SELECT %%s, %%s, %%s WHERE NOT EXISTS (SELECT 1 FROM %s WHERE cache_key = %%s)" % (table, table),
[key, encoded, connections[db].ops.value_to_db_datetime(exp), key])