Skip to content

Commit c3173f6

Browse files
don't cache component retrieval when DEBUG is true (#71)
* don't cache component getting when `DEBUG` is true * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * update changelog --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ae7d0ac commit c3173f6

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Changed
22+
23+
- When `DEBUG=True`, the `django_bird.components.Registry` will no longer cache the retrieval of `Component` instances.
24+
2125
## [0.6.1]
2226

2327
### Fixed

src/django_bird/components.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any
66

77
from cachetools import LRUCache
8+
from django.conf import settings
89
from django.template.backends.django import Template as DjangoTemplate
910
from django.template.loader import select_template
1011

@@ -44,7 +45,8 @@ def get_component(self, name: str) -> Component:
4445
return self._cache[name]
4546
except KeyError:
4647
component = Component.from_name(name)
47-
self._cache[name] = component
48+
if not settings.DEBUG:
49+
self._cache[name] = component
4850
return component
4951

5052
def clear(self) -> None:

tests/test_components.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from django.template.backends.django import Template
55
from django.template.exceptions import TemplateDoesNotExist
6+
from django.test import override_settings
67

78
from django_bird.components import Component
89
from django_bird.components import Registry
@@ -99,3 +100,17 @@ def test_lru_cache_behavior(self, registry, create_bird_template):
99100
def test_component_not_found(self, registry):
100101
with pytest.raises(TemplateDoesNotExist):
101102
registry.get_component("nonexistent")
103+
104+
def test_cache_with_debug(self, registry, create_bird_template):
105+
create_bird_template(name="button", content="<button>Click me</button>")
106+
107+
assert len(registry._cache) == 0
108+
109+
with override_settings(DEBUG=True):
110+
registry.get_component("button")
111+
112+
assert len(registry._cache) == 0
113+
114+
registry.get_component("button")
115+
116+
assert len(registry._cache) == 1

0 commit comments

Comments
 (0)