88
Dummy Caching (for development) – this implements the cache interface, but doesn’t actually cache so you could have it on your development/testing site to reduce caching and also prevent errors from caching, if those should arise.
Finally, Django comes with a "dummy" cache that doesn’t actually cache – it just implements the cache interface without doing anything.
This is useful if you have a production site that uses heavy-duty caching in various places but a development/test environment where you don’t want to cache and don’t want to have to change your code to special-case the latter. To activate dummy caching, set BACKEND like so:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
14
I use this in my settings, so it’s a bit more flexible i case I do want to test the usage of the deployed caching (in this case memcache)
TEST_MEMCACHE = False
if not DEBUG or TEST_MEMCACHE:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
else:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
- [Django]-Django comparing model instances for equality
- [Django]-Getting a count of objects in a queryset in Django
- [Django]-How do you Serialize the User model in Django Rest Framework
7
Whilst using DRF, on some views, I would enable caching using django.views.decorators.cache.cache_page
. The accepted answer did not work for me and I resorted to clearing the cache on tear down
from django.core.cache import cache
from rest_framework.test import APITestCase
class SomeTestCase(APITestCase):
def tearDown(self):
cache.clear()
- [Django]-Django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
- [Django]-Many-To-Many Fields View on Django Admin
- [Django]-Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty
6
Solution for multiple caches, and you want to disable all of them:
if True:
CACHES = {
k : {'BACKEND': 'django.core.cache.backends.dummy.DummyCache',}
for k,v in CACHES.iteritems()
}
Solution if you want to disable some caches, may help:
if True:
disable_names = [ 'cache_name' ]
for name in disable_names:
CACHES[name] = {'BACKEND' : 'django.core.cache.backends.dummy.DummyCache',}
- [Django]-How to add multiple objects to ManyToMany relationship at once in Django ?
- [Django]-Django-social-auth django-registration and django-profiles — together
- [Django]-Django get objects not referenced by foreign key
3
For that purpose you can use “dummy” cache backend. Django comes with a “dummy” cache that doesn’t actually cache — it just implements the cache interface without doing anything.
Here are the old style and new style configuration formats.
-
old style
To activate dummy caching, set CACHE_BACKEND like so:
CACHE_BACKEND = 'dummy://'
-
new style
CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', } }
- [Django]-Running "unique" tasks with celery
- [Django]-FileUploadParser doesn't get the file name
- [Django]-Django admin – inline inlines (or, three model editing at once)