Skip to content

Add CACHALOT_ONLY_CACHABLE_APPS & CACHALOT_UNCACHABLE_APPS (Fixes #186) #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
What’s new in django-cachalot?
==============================

2.4.2
-----

- Add convenience settings `CACHALOT_ONLY_CACHABLE_APPS`
and `CACHALOT_UNCACHABLE_APPS`

2.4.1
-----

Expand Down
19 changes: 17 additions & 2 deletions cachalot/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from itertools import chain

from django.apps import apps
from django.conf import settings
from django.utils.module_loading import import_string

Expand Down Expand Up @@ -52,7 +55,9 @@ class Settings(object):
CACHALOT_CACHE_RANDOM = False
CACHALOT_INVALIDATE_RAW = True
CACHALOT_ONLY_CACHABLE_TABLES = ()
CACHALOT_ONLY_CACHABLE_APPS = ()
CACHALOT_UNCACHABLE_TABLES = ('django_migrations',)
CACHALOT_UNCACHABLE_APPS = ()
CACHALOT_ADDITIONAL_TABLES = ()
CACHALOT_QUERY_KEYGEN = 'cachalot.utils.get_query_cache_key'
CACHALOT_TABLE_KEYGEN = 'cachalot.utils.get_table_cache_key'
Expand Down Expand Up @@ -103,14 +108,24 @@ def convert(value):
return value


def convert_tables(value, setting_app_name):
dj_apps = getattr(settings, setting_app_name, ())
if dj_apps:
dj_apps = tuple(model._meta.db_table for model in chain.from_iterable(
apps.all_models[_app].values() for _app in dj_apps
)) # Use [] lookup to make sure app is loaded (via INSTALLED_APP's order)
return frozenset(tuple(value) + dj_apps)
return frozenset(value)


@Settings.add_converter('CACHALOT_ONLY_CACHABLE_TABLES')
def convert(value):
return frozenset(value)
return convert_tables(value, 'CACHALOT_ONLY_CACHABLE_APPS')


@Settings.add_converter('CACHALOT_UNCACHABLE_TABLES')
def convert(value):
return frozenset(value)
return convert_tables(value, 'CACHALOT_UNCACHABLE_APPS')


@Settings.add_converter('CACHALOT_ADDITIONAL_TABLES')
Expand Down
26 changes: 26 additions & 0 deletions cachalot/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,21 @@ def test_only_cachable_tables(self):
# table, it’s cachable.
self.assert_query_cached(TestChild.objects.values('public'))

@override_settings(CACHALOT_ONLY_CACHABLE_APPS=('cachalot',))
def test_only_cachable_apps(self):
self.assert_query_cached(Test.objects.all())
self.assert_query_cached(TestParent.objects.all())
self.assert_query_cached(Test.objects.select_related('owner'), after=1)

# Must use override_settings to get the correct effect. Using the cm doesn't
# reload settings on cachalot's side
@override_settings(CACHALOT_ONLY_CACHABLE_TABLES=('cachalot_test', 'auth_user'),
CACHALOT_ONLY_CACHABLE_APPS=('cachalot',))
def test_only_cachable_apps_set_combo(self):
self.assert_query_cached(Test.objects.all())
self.assert_query_cached(TestParent.objects.all())
self.assert_query_cached(Test.objects.select_related('owner'))

def test_uncachable_tables(self):
qs = Test.objects.all()

Expand All @@ -163,6 +178,17 @@ def test_uncachable_tables(self):
with self.settings(CACHALOT_UNCACHABLE_TABLES=('cachalot_test',)):
self.assert_query_cached(qs, after=1)

@override_settings(CACHALOT_UNCACHABLE_APPS=('cachalot',))
def test_uncachable_apps(self):
self.assert_query_cached(Test.objects.all(), after=1)
self.assert_query_cached(TestParent.objects.all(), after=1)

@override_settings(CACHALOT_UNCACHABLE_TABLES=('cachalot_test',),
CACHALOT_UNCACHABLE_APPS=('cachalot',))
def test_uncachable_apps_set_combo(self):
self.assert_query_cached(Test.objects.all(), after=1)
self.assert_query_cached(TestParent.objects.all(), after=1)

def test_only_cachable_and_uncachable_table(self):
with self.settings(
CACHALOT_ONLY_CACHABLE_TABLES=('cachalot_test',
Expand Down
26 changes: 25 additions & 1 deletion docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,32 @@ Settings
SQL queries – read :ref:`raw queries limits <Raw SQL queries>` for more info.


.. _CACHALOT_ONLY_CACHABLE_TABLES:

``CACHALOT_ONLY_CACHABLE_TABLES``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Default: ``frozenset()``
:Description:
Sequence of SQL table names that will be the only ones django-cachalot
will cache. Only queries with a subset of these tables will be cached.
The sequence being empty (as it is by default) doesn’t mean that no table
The sequence being empty (as it is by default) does not mean that no table
can be cached: it disables this setting, so any table can be cached.
:ref:`CACHALOT_UNCACHABLE_TABLES` has more weight than this:
if you add a table to both settings, it will never be cached.
Run ``./manage.py invalidate_cachalot`` after changing this setting.

``CACHALOT_ONLY_CACHABLE_APPS``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Default: ``frozenset()``
:Description:
Sequence of Django apps whose associated models will be appended to
:ref:`CACHALOT_ONLY_CACHABLE_TABLES`. The rules between
:ref:`CACHALOT_UNCACHABLE_TABLES` and :ref:`CACHALOT_ONLY_CACHABLE_TABLES` still
apply as this setting only appends the given Django apps' tables on initial
Django setup.


.. _CACHALOT_UNCACHABLE_TABLES:

Expand All @@ -144,6 +157,17 @@ Settings
some issues, especially during tests.
Run ``./manage.py invalidate_cachalot`` after changing this setting.

``CACHALOT_UNCACHABLE_APPS``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Default: ``frozenset()``
:Description:
Sequence of Django apps whose associated models will be appended to
:ref:`CACHALOT_UNCACHABLE_TABLES`. The rules between
:ref:`CACHALOT_UNCACHABLE_TABLES` and :ref:`CACHALOT_ONLY_CACHABLE_TABLES` still
apply as this setting only appends the given Django apps' tables on initial
Django setup.

``CACHALOT_ADDITIONAL_TABLES``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down