|
| 1 | +import logging |
| 2 | +import pickle |
| 3 | +from typing import List |
| 4 | + |
| 5 | +from django.core.cache import caches |
| 6 | +from django.http import HttpRequest |
| 7 | + |
| 8 | +import django_unicorn |
| 9 | +from django_unicorn.errors import UnicornCacheError |
| 10 | +from django_unicorn.settings import get_cache_alias |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class PointerUnicornView: |
| 16 | + def __init__(self, component_cache_key): |
| 17 | + self.component_cache_key = component_cache_key |
| 18 | + self.parent = None |
| 19 | + self.children = [] |
| 20 | + |
| 21 | + |
| 22 | +class CacheableComponent: |
| 23 | + """ |
| 24 | + Updates a component into something that is cacheable/pickleable. Also set pointers to parents/children. |
| 25 | + Use in a `with` statement or explicitly call `__enter__` `__exit__` to use. It will restore the original component |
| 26 | + on exit. |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, component: "django_unicorn.views.UnicornView"): |
| 30 | + self._state = {} |
| 31 | + self.cacheable_component = component |
| 32 | + |
| 33 | + def __enter__(self): |
| 34 | + components = [] |
| 35 | + components.append(self.cacheable_component) |
| 36 | + |
| 37 | + while components: |
| 38 | + component = components.pop() |
| 39 | + |
| 40 | + if component.component_id in self._state: |
| 41 | + continue |
| 42 | + |
| 43 | + if hasattr(component, "extra_context"): |
| 44 | + extra_context = component.extra_context |
| 45 | + component.extra_context = None |
| 46 | + else: |
| 47 | + extra_context = None |
| 48 | + |
| 49 | + request = component.request |
| 50 | + component.request = None |
| 51 | + |
| 52 | + self._state[component.component_id] = ( |
| 53 | + component, |
| 54 | + request, |
| 55 | + extra_context, |
| 56 | + component.parent, |
| 57 | + component.children.copy(), |
| 58 | + ) |
| 59 | + |
| 60 | + if component.parent: |
| 61 | + components.append(component.parent) |
| 62 | + component.parent = PointerUnicornView(component.parent.component_cache_key) |
| 63 | + |
| 64 | + for index, child in enumerate(component.children): |
| 65 | + components.append(child) |
| 66 | + component.children[index] = PointerUnicornView(child.component_cache_key) |
| 67 | + |
| 68 | + for component, *_ in self._state.values(): |
| 69 | + try: |
| 70 | + pickle.dumps(component) |
| 71 | + except ( |
| 72 | + TypeError, |
| 73 | + AttributeError, |
| 74 | + NotImplementedError, |
| 75 | + pickle.PicklingError, |
| 76 | + ) as e: |
| 77 | + raise UnicornCacheError( |
| 78 | + f"Cannot cache component '{type(component)}' because it is not picklable: {type(e)}: {e}" |
| 79 | + ) from e |
| 80 | + |
| 81 | + return self |
| 82 | + |
| 83 | + def __exit__(self, *args): |
| 84 | + for component, request, extra_context, parent, children in self._state.values(): |
| 85 | + component.request = request |
| 86 | + component.parent = parent |
| 87 | + component.children = children |
| 88 | + |
| 89 | + if extra_context: |
| 90 | + component.extra_context = extra_context |
| 91 | + |
| 92 | + def components(self) -> List["django_unicorn.views.UnicornView"]: |
| 93 | + return [component for component, *_ in self._state.values()] |
| 94 | + |
| 95 | + |
| 96 | +def cache_full_tree(component: "django_unicorn.views.UnicornView"): |
| 97 | + root = component |
| 98 | + |
| 99 | + while root.parent: |
| 100 | + root = root.parent |
| 101 | + |
| 102 | + cache = caches[get_cache_alias()] |
| 103 | + |
| 104 | + with CacheableComponent(root) as caching: |
| 105 | + for component in caching.components(): |
| 106 | + cache.set(component.component_cache_key, component) |
| 107 | + |
| 108 | + |
| 109 | +def restore_from_cache(component_cache_key: str, request: HttpRequest = None) -> "django_unicorn.views.UnicornView": |
| 110 | + """ |
| 111 | + Gets a cached unicorn view by key, restoring and getting cached parents and children |
| 112 | + and setting the request. |
| 113 | + """ |
| 114 | + |
| 115 | + cache = caches[get_cache_alias()] |
| 116 | + cached_component = cache.get(component_cache_key) |
| 117 | + |
| 118 | + if cached_component: |
| 119 | + roots = {} |
| 120 | + root: "django_unicorn.views.UnicornView" = cached_component |
| 121 | + roots[root.component_cache_key] = root |
| 122 | + |
| 123 | + while root.parent: |
| 124 | + root = cache.get(root.parent.component_cache_key) |
| 125 | + roots[root.component_cache_key] = root |
| 126 | + |
| 127 | + to_traverse: List["django_unicorn.views.UnicornView"] = [] |
| 128 | + to_traverse.append(root) |
| 129 | + |
| 130 | + while to_traverse: |
| 131 | + current = to_traverse.pop() |
| 132 | + current.setup(request) |
| 133 | + current._validate_called = False |
| 134 | + current.calls = [] |
| 135 | + |
| 136 | + for index, child in enumerate(current.children): |
| 137 | + key = child.component_cache_key |
| 138 | + cached_child = roots.pop(key, None) or cache.get(key) |
| 139 | + |
| 140 | + cached_child.parent = current |
| 141 | + current.children[index] = cached_child |
| 142 | + to_traverse.append(cached_child) |
| 143 | + |
| 144 | + return cached_component |
0 commit comments