Adds a CACHALOT_ENABLED setting.

This commit is contained in:
Bertrand Bordage 2014-10-05 02:36:19 +02:00
parent 554dbfde94
commit e8cff823d3
4 changed files with 34 additions and 8 deletions

View file

@ -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
........................

View file

@ -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

View file

@ -2,6 +2,7 @@ from django.conf import settings
class Settings(object):
CACHALOT_ENABLED = True
CACHALOT_CACHE = 'default'
def __getattribute__(self, item):

View file

@ -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 cant change the cache used since theres only one configured')
def test_cache(self):