Render cache keys
Exact key helpers and errors for component and named-fragment render caches.
CacheConfig class
Bases: ExtensionConfig
Typed runtime view of one component's Cache declaration.
Citry creates this value from the component's nested Cache class. Read it through component.cache while the component is rendering.
Attributes
enabledbool- Whether Citry may cache this component's rendered output.ttlfloat | None- How many seconds an entry remains valid, orNonefor no expiry.versionint | str- An application-controlled value included in the cache key.
CacheKeyError class
Bases: ValueError
A value cannot be represented safely in a render-cache key.
Attributes
path- Location of the rejected value within the semantic variation.reason- Human-readable reason the value was rejected.
OnComponentCacheHitContext class
Notify an observer after one cached component subtree was replayed.
component is the live boundary from the current call, never an archived object. key_digest is exactly 64 lowercase hexadecimal characters without a backend-key prefix. artifact_bytes is the stored value's validated UTF-8 size, and frame_count includes the boundary frame. kind distinguishes a Component.Cache hit from a transparent <c-cache> fragment hit. For a fragment, component is the live built-in Cache boundary from the current call. Return values and observer failures do not alter the committed hit.
The dataclass is shallowly frozen. Observers should copy the scalar fields they need instead of retaining the live component through this context.
component_cache_key function
component_cache_key(component: type[Component], vary: object, version: int | str = 1, sub_path: str | None = None) -> strBuild the exact physical key for one component variation.
This helper does not construct the component, run input hooks, or call a component's Cache.vary() method. Callers supply the already-computed semantic variation value.
An app served below a sub-path, such as /shop, stores its entries under keys that include it, because a cached render can hold URLs that start with it. Inside a request the key uses the request's sub-path. Outside one, such as in a background job, it uses the app's setting (FastAPI's root_path, Flask's APPLICATION_ROOT, Django's FORCE_SCRIPT_NAME), so pass sub_path when only the server sets the sub-path.
Parameters
componenttype[Component]- Component class whose stable class ID identifies the entry.varyobject- Semantic variation accepted by Citry's canonical key encoder.versionint | str- Author-controlled exact integer or non-empty string version.sub_pathstr | None- The sub-path the app is served below, such as"/shop", or""for the site root.Nonereads the current one.
Returns
str: A short ASCII key suitable for deleting one exact backend entry.
Raises
TypeError- Ifcomponentis not a Component class.ValueError- Ifversionorsub_pathis invalid.CacheKeyError- Ifvarycannot be encoded safely.
fragment_cache_key function
fragment_cache_key(citry: Citry, key: str, vary: object = (), version: int | str = 1, sub_path: str | None = None) -> strBuild the exact physical key for one named fragment variation.
Like <c-cache>, this unwraps an outer Const marker from key, vary, and version before validation and encoding. The key includes the sub-path the app is served below, as component_cache_key() describes.
Parameters
citryCitry- Engine whose cache scope and local revision apply.keystr- Exact non-empty semantic fragment name.varyobject- Semantic variation accepted by Citry's canonical key encoder.versionint | str- Author-controlled exact integer or non-empty string version.sub_pathstr | None- The sub-path the app is served below, such as"/shop", or""for the site root.Nonereads the current one.
Returns
str: A short ASCII key suitable for deleting one exact backend entry.
Raises
TypeError- Ifcitryis not a Citry instance.ValueError- Ifkey,version, orsub_pathis invalid.CacheKeyError- Ifvarycannot be encoded safely.