Login

Clear Django Cache

Author:
girasquid
Posted:
September 23, 2008
Language:
Python
Version:
1.0
Score:
4 (after 4 ratings)

This piece of code will clear the cache, whether you are using in-memory or filesystem caching.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from django.core.cache import cache
try:
	cache._cache.clear()	# in-memory caching
except AttributeError:
	# try filesystem caching next
	old = cache._cull_frequency
	old_max = cache._max_entries
	cache._max_entries = 0
	cache._cull_frequency = 1
	cache._cull()
	cache._cull_frequency = old
	cache._max_entries = old_max

More like this

  1. Treat empty value on DateRangeFilter as a filter for non filled dates by kmmbvnr 1 month, 1 week ago
  2. Django Settings Assignment Expressions aka Walrus Operator example by webology 2 months, 1 week ago
  3. codigo alto nivel by MrRocklion 4 months ago
  4. Load template from specific app by Krzysiek555 4 months, 3 weeks ago
  5. PostgreSQL JSON subqueries by dolamroth 4 months, 3 weeks ago

Comments

diefenbach (on January 20, 2009):

At least for locmem one has to do clear also "_expire_info", otherwise there will be key errors:

cache._expire_info.clear()

#

ofri (on May 3, 2009):

if you're using memcached it's

 cache._cache.flush_all()

#

Please login first before commenting.