@@ -47,6 +47,7 @@ def inner(self):
4747
4848 return property (inner )
4949
50+
5051# 1. The ContextVar (Default is None)
5152# This replaces threading.local with the async-safe standard.
5253generation_cache : ContextVar [Optional [Dict [Any , Any ]]] = ContextVar (
@@ -56,10 +57,11 @@ def inner(self):
5657# Optimization: Bind .get for speed
5758_get_cache = generation_cache .get
5859
60+
5961@contextlib .contextmanager
6062def generation_cache_context ():
6163 """Context manager to explicitly manage the cache lifecycle.
62-
64+
6365 Usage:
6466 with generation_cache_context():
6567 # Cache is active (fast)
@@ -74,6 +76,7 @@ def generation_cache_context():
7476 # This allows the dictionary to be garbage collected.
7577 generation_cache .reset (token )
7678
79+
7780def cached_proto_context (func ):
7881 """Decorator to memoize with_context calls based on self and collisions."""
7982
@@ -83,7 +86,7 @@ def wrapper(self, *, collisions, **kwargs):
8386
8487 context_cache = _get_cache ()
8588 if context_cache is None :
86- raise RuntimeError (
89+ raise RuntimeError (
8790 f"Cache MISSING! { func .__name__ } called on { type (self ).__name__ } outside of context manager."
8891 )
8992
@@ -97,9 +100,7 @@ def wrapper(self, *, collisions, **kwargs):
97100
98101 # 4. Execute the actual function
99102 # We ensure context_cache is passed down to the recursive calls
100- result = func (
101- self , collisions = collisions , ** kwargs
102- )
103+ result = func (self , collisions = collisions , ** kwargs )
103104
104105 # 5. Update Cache
105106 context_cache [key ] = result
0 commit comments