Skip to content

Commit 6deee77

Browse files
committed
Clean up how component's cache key is generated.
1 parent 24c6d92 commit 6deee77

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

django_unicorn/components/unicorn_view.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def __init__(self, **kwargs):
156156

157157
assert hasattr(self, "component_id"), "Component id is required"
158158
assert self.component_id, "Component id is required"
159+
self.component_cache_key = f"unicorn:component:{self.component_id}"
159160

160161
if "request" in kwargs:
161162
self.setup(kwargs["request"])
@@ -607,6 +608,7 @@ def _is_public(self, name: str) -> bool:
607608
"children",
608609
"call",
609610
"calls",
611+
"component_cache_key",
610612
)
611613
excludes = []
612614

@@ -651,9 +653,6 @@ def create(
651653
assert component_id, "Component id is required"
652654
assert component_name, "Component name is required"
653655

654-
cache = caches[get_cache_alias()]
655-
component_cache_key = f"unicorn:component:{component_id}"
656-
657656
@timed
658657
def _get_component_class(
659658
module_name: str, class_name: str
@@ -666,14 +665,19 @@ def _get_component_class(
666665

667666
return component_class
668667

668+
cache = caches[get_cache_alias()]
669+
component_cache_key = f"unicorn:component:{component_id}"
669670
cached_component = cache.get(component_cache_key)
670671

671672
if cached_component:
673+
# Get the newest version of the parent from cache if it is available
674+
# This needs to happen for Django cache because instances is pickled, so
675+
# a change in the view won't be reflected automatically (like with the module
676+
# cache) so it needs to be retrieved manually.
672677
if cached_component.parent:
673-
parent_component_cache_key = (
674-
f"unicorn:component:{cached_component.parent.component_id}"
678+
cached_parent_component = cache.get(
679+
cached_component.parent.component_cache_key
675680
)
676-
cached_parent_component = cache.get(parent_component_cache_key)
677681

678682
if cached_parent_component:
679683
cached_component.parent = cached_parent_component
@@ -742,7 +746,9 @@ def _get_component_class(
742746
if COMPONENTS_MODULE_CACHE_ENABLED:
743747
constructed_views_cache[component_id] = cacheable_component
744748

745-
cache.set(component_cache_key, cacheable_component)
749+
cache.set(
750+
cacheable_component.component_cache_key, cacheable_component
751+
)
746752

747753
return component
748754
except ModuleNotFoundError as e:

django_unicorn/views.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,7 @@ def _process_component_request(
509509
component.rendered(rendered_component)
510510

511511
cache = caches[get_cache_alias()]
512-
component_cache_key = f"unicorn:component:{component.component_id}"
513-
cacheable_component = get_cacheable_component(component)
514-
cache.set(component_cache_key, cacheable_component)
512+
cache.set(component.component_cache_key, get_cacheable_component(component))
515513

516514
partial_doms = []
517515

@@ -602,9 +600,10 @@ def _process_component_request(
602600
parent_dom = parent_component.render()
603601
component.parent_rendered(parent_dom)
604602

605-
component_cache_key = f"unicorn:component:{parent_component.component_id}"
606-
cacheable_component = get_cacheable_component(parent_component)
607-
cache.set(component_cache_key, cacheable_component)
603+
cache.set(
604+
parent_component.component_cache_key,
605+
get_cacheable_component(parent_component),
606+
)
608607

609608
parent.update(
610609
{
@@ -650,14 +649,12 @@ def _handle_component_request(
650649
cache = caches[get_cache_alias()]
651650

652651
# Add the current request `ComponentRequest` to the cache
653-
component_cache_key = (
654-
f"unicorn:queue:{component_request.name}:{component_request.id}"
655-
)
656-
component_requests = cache.get(component_cache_key) or []
652+
queue_cache_key = f"unicorn:queue:{component_request.id}"
653+
component_requests = cache.get(queue_cache_key) or []
657654
component_requests.append(component_request)
658655

659656
cache.set(
660-
component_cache_key, component_requests, timeout=get_serial_timeout(),
657+
queue_cache_key, component_requests, timeout=get_serial_timeout(),
661658
)
662659

663660
if len(component_requests) > 1:
@@ -669,12 +666,12 @@ def _handle_component_request(
669666
}
670667

671668
return _handle_queued_component_requests(
672-
request, component_request.name, component_cache_key
669+
request, component_request.name, queue_cache_key
673670
)
674671

675672

676673
def _handle_queued_component_requests(
677-
request: HttpRequest, component_name: str, component_cache_key
674+
request: HttpRequest, component_name: str, queue_cache_key
678675
) -> Dict:
679676
"""
680677
Process the current component requests that are stored in cache.
@@ -685,8 +682,8 @@ def _handle_queued_component_requests(
685682
Args:
686683
param request: HttpRequest for the view.
687684
param: component_name: Name of the component, e.g. "hello-world".
688-
param: component_cache_key: Cache key created from name of the component and the
689-
component id which should be unique for any particular user's request lifecycle.
685+
param: queue_cache_key: Cache key created from component id which should be unique
686+
for any particular user's request lifecycle.
690687
691688
Returns:
692689
JSON with the following structure:
@@ -702,10 +699,10 @@ def _handle_queued_component_requests(
702699
cache = caches[get_cache_alias()]
703700

704701
# Handle current request and any others in the cache by first sorting all of the current requests by ascending order
705-
component_requests = cache.get(component_cache_key)
702+
component_requests = cache.get(queue_cache_key)
706703

707704
if not component_requests or not isinstance(component_requests, list):
708-
raise UnicornViewError(f"No request found for {component_cache_key}")
705+
raise UnicornViewError(f"No request found for {queue_cache_key}")
709706

710707
component_requests = sorted(component_requests, key=lambda r: r.epoch)
711708
first_component_request = component_requests[0]
@@ -716,13 +713,13 @@ def _handle_queued_component_requests(
716713
first_json_result = _process_component_request(request, first_component_request)
717714

718715
# Re-check for requests after the first request is processed
719-
component_requests = cache.get(component_cache_key)
716+
component_requests = cache.get(queue_cache_key)
720717

721718
# Check that the request is in the cache before popping it off
722719
if component_requests:
723720
component_requests.pop(0)
724721
cache.set(
725-
component_cache_key, component_requests, timeout=get_serial_timeout(),
722+
queue_cache_key, component_requests, timeout=get_serial_timeout(),
726723
)
727724

728725
if component_requests:
@@ -750,7 +747,7 @@ def _handle_queued_component_requests(
750747

751748
component_requests.pop(0)
752749
cache.set(
753-
component_cache_key, component_requests, timeout=get_serial_timeout(),
750+
queue_cache_key, component_requests, timeout=get_serial_timeout(),
754751
)
755752

756753
merged_json_result = _handle_component_request(

0 commit comments

Comments
 (0)