Skip to content

Commit

Permalink
Cache settings value even when they are retrieved from settings to av…
Browse files Browse the repository at this point in the history
…oid database hits.
  • Loading branch information
fabiocaccamo committed Feb 27, 2024
1 parent a80e279 commit 54a744d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions extra_settings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def get(cls, name, default=None):
# cache all other settings to avoid multiple queries
# in pages using the template more than once.
cls._cache_all_settings()
if val is not None:
set_cached_setting(name, val)
return val

@classmethod
Expand Down
6 changes: 5 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

ALLOWED_HOSTS = ["*"]

EXTRA_SETTINGS_TEST_FALLBACK_VALUE = "fallback-value"
# Package settings
EXTRA_SETTINGS_DEFAULTS = [
{
"name": "TEST_DEFAULT_URL",
Expand All @@ -15,6 +15,10 @@
},
]

# Test settings
EXTRA_SETTINGS_TEST_VALUE = "value"
EXTRA_SETTINGS_TEST_FALLBACK_VALUE = "fallback-value"

# Application definition
INSTALLED_APPS = [
"extra_settings",
Expand Down
8 changes: 8 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ def test_get_num_queries(self):
with self.assertNumQueries(0):
Setting.get("TEST_SETTING_STRING")

def test_get_from_conf_settings_num_queries(self):
with self.assertNumQueries(1):
setting_value = Setting.get("EXTRA_SETTINGS_TEST_VALUE")
self.assertEqual(setting_value, "value")
with self.assertNumQueries(0):
setting_value = Setting.get("EXTRA_SETTINGS_TEST_VALUE")
self.assertEqual(setting_value, "value")

def test_cache_updated_on_model_delete(self):
setting_obj, setting_created = Setting.objects.get_or_create(
name="TEST_GETTER_SETTER", defaults={"value_type": Setting.TYPE_STRING}
Expand Down
4 changes: 4 additions & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.template import Context, Template
from django.test import TestCase

from extra_settings.cache import del_cached_setting
from extra_settings.models import Setting


Expand Down Expand Up @@ -49,6 +50,7 @@ def test_get_setting_with_fallback_auto(self):
self.assertEqual(rendered, "fallback-value")

def test_get_setting_with_fallback(self):
del_cached_setting("EXTRA_SETTINGS_TEST_FALLBACK_VALUE")
with self.settings(EXTRA_SETTINGS_FALLBACK_TO_CONF_SETTINGS=True):
rendered = self._render_template(
"{% load extra_settings %}"
Expand All @@ -57,6 +59,7 @@ def test_get_setting_with_fallback(self):
self.assertEqual(rendered, "fallback-value")

def test_get_setting_without_fallback(self):
del_cached_setting("EXTRA_SETTINGS_TEST_FALLBACK_VALUE")
with self.settings(EXTRA_SETTINGS_FALLBACK_TO_CONF_SETTINGS=False):
rendered = self._render_template(
"{% load extra_settings %}"
Expand All @@ -65,6 +68,7 @@ def test_get_setting_without_fallback(self):
self.assertEqual(rendered, "")

def test_get_setting_without_fallback_default(self):
del_cached_setting("EXTRA_SETTINGS_TEST_FALLBACK_VALUE")
with self.settings(EXTRA_SETTINGS_FALLBACK_TO_CONF_SETTINGS=False):
rendered = self._render_template(
"{% load extra_settings %}"
Expand Down

0 comments on commit 54a744d

Please sign in to comment.