@@ -353,12 +353,44 @@ def dispatch(self, request, *args, **kwargs):
353
353
self .mount ()
354
354
self .hydrate ()
355
355
356
+ self ._cache_component (request , ** kwargs )
357
+
356
358
return self .render_to_response (
357
359
context = self .get_context_data (),
358
360
component = self ,
359
361
init_js = True ,
360
362
)
361
363
364
+ def _cache_component (self , request : HttpRequest , parent = None , ** kwargs ):
365
+ """
366
+ Cache the component in the module and the Django cache. Re-set the `request` that got
367
+ removed to make the component cacheable.
368
+ """
369
+
370
+ # Put the location for the component name in a module cache
371
+ location_cache [self .component_name ] = (self .__class__ .__name__ , self .__module__ )
372
+
373
+ # Put the component's class in a module cache
374
+ views_cache [self .component_id ] = (self .__class__ , parent , kwargs )
375
+
376
+ cacheable_component = None
377
+
378
+ # Put the instantiated component into a module cache and the Django cache
379
+ try :
380
+ cacheable_component = get_cacheable_component (self )
381
+ except UnicornCacheError as e :
382
+ logger .warning (e )
383
+
384
+ if cacheable_component :
385
+ if COMPONENTS_MODULE_CACHE_ENABLED :
386
+ constructed_views_cache [self .component_id ] = cacheable_component
387
+
388
+ cache = caches [get_cache_alias ()]
389
+ cache .set (cacheable_component .component_cache_key , cacheable_component )
390
+
391
+ # Re-set `request` on the component that got removed when making it cacheable
392
+ self .request = request
393
+
362
394
@timed
363
395
def get_frontend_context_variables (self ) -> str :
364
396
"""
@@ -807,30 +839,7 @@ def _get_component_class(
807
839
** kwargs ,
808
840
)
809
841
810
- # Put the location for the component name in a module cache
811
- location_cache [component_name ] = (class_name , module_name )
812
-
813
- # Put the component's class in a module cache
814
- views_cache [component_id ] = (component_class , parent , kwargs )
815
-
816
- # Put the instantiated component into a module cache and the Django cache
817
- cacheable_component = None
818
-
819
- try :
820
- cacheable_component = get_cacheable_component (component )
821
- except UnicornCacheError as e :
822
- logger .warning (e )
823
-
824
- if cacheable_component :
825
- if COMPONENTS_MODULE_CACHE_ENABLED :
826
- constructed_views_cache [component_id ] = cacheable_component
827
-
828
- cache .set (
829
- cacheable_component .component_cache_key , cacheable_component
830
- )
831
-
832
- # Re-setup the `request` that got removed to make the component cacheable
833
- component .setup (request )
842
+ component ._cache_component (request , parent , ** kwargs )
834
843
835
844
return component
836
845
except ModuleNotFoundError as e :
0 commit comments