From e8cff823d376ec1ce163ccf4fbdc9b90a59644d9 Mon Sep 17 00:00:00 2001 From: Bertrand Bordage Date: Sun, 5 Oct 2014 02:36:19 +0200 Subject: [PATCH] Adds a CACHALOT_ENABLED setting. --- README.rst | 15 ++++++++------- cachalot/monkey_patch.py | 3 ++- cachalot/settings.py | 1 + cachalot/tests.py | 23 +++++++++++++++++++++++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 67ebc46..3074fa4 100644 --- a/README.rst +++ b/README.rst @@ -41,12 +41,14 @@ Usage Settings ........ -================== ============= ============================================== -Setting Default value Description -================== ============= ============================================== -``CACHALOT_CACHE`` ``'default'`` Name of the cache from |CACHES|_ used by - django-cachalot -================== ============= ============================================== +==================== ============= ============================================ +Setting Default value Description +==================== ============= ============================================ +``CACHALOT_ENABLED`` ``True`` If set to ``False``, disables SQL caching + but keeps invalidating to avoid stale cache +``CACHALOT_CACHE`` ``'default'`` Name of the cache from |CACHES|_ used by + django-cachalot +==================== ============= ============================================ .. |CACHES| replace:: ``CACHES`` .. _CACHES: https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-CACHES @@ -119,7 +121,6 @@ For version 1.0 - Add memcached support - Handle multiple databases - Add invalidation on migrations in Django 1.7 (& South?) -- Add a ``CACHALOT_ENABLED`` setting In a more distant future ........................ diff --git a/cachalot/monkey_patch.py b/cachalot/monkey_patch.py index da8deaf..8e237f5 100644 --- a/cachalot/monkey_patch.py +++ b/cachalot/monkey_patch.py @@ -165,7 +165,8 @@ def get_cache(): def _patch_orm_read(): def patch_execute_sql(original): def inner(compiler, *args, **kwargs): - if isinstance(compiler, WRITE_COMPILERS): + if not cachalot_settings.CACHALOT_ENABLED \ + or isinstance(compiler, WRITE_COMPILERS): return original(compiler, *args, **kwargs) query = compiler.query diff --git a/cachalot/settings.py b/cachalot/settings.py index 7079041..9c79eaf 100644 --- a/cachalot/settings.py +++ b/cachalot/settings.py @@ -2,6 +2,7 @@ from django.conf import settings class Settings(object): + CACHALOT_ENABLED = True CACHALOT_CACHE = 'default' def __getattribute__(self, item): diff --git a/cachalot/tests.py b/cachalot/tests.py index 4a4b7f5..574544f 100644 --- a/cachalot/tests.py +++ b/cachalot/tests.py @@ -1178,6 +1178,29 @@ class AtomicTestCase(TestCase): class SettingsTestCase(TestCase): + def test_enabled(self): + with self.settings(CACHALOT_ENABLED=True): + with self.assertNumQueries(1): + list(Test.objects.all()) + with self.assertNumQueries(0): + list(Test.objects.all()) + + with self.settings(CACHALOT_ENABLED=False): + with self.assertNumQueries(1): + list(Test.objects.all()) + with self.assertNumQueries(1): + list(Test.objects.all()) + + with self.assertNumQueries(0): + list(Test.objects.all()) + + with self.settings(CACHALOT_ENABLED=False): + with self.assertNumQueries(1): + t = Test.objects.create(name='test') + with self.assertNumQueries(1): + data = list(Test.objects.all()) + self.assertListEqual(data, [t]) + @skipIf(len(settings.CACHES) == 1, 'We can’t change the cache used since there’s only one configured') def test_cache(self):