Repository: joshuadavidthomas/django-bird

Issue:
Component caching too aggressive in dev
I'm having to restart the development server anytime I make a change to a component. It's annoying. Maybe if `DEBUG = True` we should not cache. Yeah, that'd be nice.

Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9203d0b..2f66718 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
 
 ## [Unreleased]
 
+### Changed
+
+- When `DEBUG=True`, the `django_bird.components.Registry` will no longer cache the retrieval of `Component` instances.
+
 ## [0.6.1]
 
 ### Fixed
diff --git a/src/django_bird/components.py b/src/django_bird/components.py
index 9188235..226e6a9 100644
--- a/src/django_bird/components.py
+++ b/src/django_bird/components.py
@@ -5,6 +5,7 @@ from dataclasses import field
 from typing import Any
 
 from cachetools import LRUCache
+from django.conf import settings
 from django.template.backends.django import Template as DjangoTemplate
 from django.template.loader import select_template
 
@@ -44,7 +45,8 @@ class Registry:
             return self._cache[name]
         except KeyError:
             component = Component.from_name(name)
-            self._cache[name] = component
+            if not settings.DEBUG:
+                self._cache[name] = component
             return component
 
     def clear(self) -> None:


Test patch:
diff --git a/tests/test_components.py b/tests/test_components.py
index 1df8a47..49a25af 100644
--- a/tests/test_components.py
+++ b/tests/test_components.py
@@ -3,6 +3,7 @@ from __future__ import annotations
 import pytest
 from django.template.backends.django import Template
 from django.template.exceptions import TemplateDoesNotExist
+from django.test import override_settings
 
 from django_bird.components import Component
 from django_bird.components import Registry
@@ -99,3 +100,17 @@ class TestRegistry:
     def test_component_not_found(self, registry):
         with pytest.raises(TemplateDoesNotExist):
             registry.get_component("nonexistent")
+
+    def test_cache_with_debug(self, registry, create_bird_template):
+        create_bird_template(name="button", content="<button>Click me</button>")
+
+        assert len(registry._cache) == 0
+
+        with override_settings(DEBUG=True):
+            registry.get_component("button")
+
+        assert len(registry._cache) == 0
+
+        registry.get_component("button")
+
+        assert len(registry._cache) == 1