Cache — Aquilia Documentation
Comprehensive guide and documentation for Cache in the Aquilia framework. View API reference, examples, and implementation patterns.
Advanced / Cache AquilaCache Overview AquilaCache is Aquilia's async-native cache subsystem. It provides a DI-injectable CacheService , pluggable backends (Memory, Redis, Composite L1/L2), HTTP response-caching middleware, decorators, and tag-based invalidation. Runtime Wiring The cache system initializes early in the server bootstrap lifecycle inside _setup_cache(), registers providers into all active DI containers, and binds lifecycle startup/shutdown handlers. # 1. Load configuration via ConfigLoader cache_config = self.config.get_cache_config() if not cache_config.get("enabled", False): return # 2. Build configuration model and instantiate service config_obj = build_cache_config(cache_config) svc = create_cache_service(config_obj) # 3. Register service inside all active DI containers for container in self.runtime.di_containers.values(): register_cache_providers(container, svc) self._cache_service = svc # 4. Conditionally add HTTP response-cache middleware mw_cfg = cache_config.get("middleware", ) if mw_cfg.get("enabled", False): self.middleware_stack.add( CacheMiddleware(cache_service=svc, ttl=mw_cfg.get("ttl", 300)), scope="global", priority=26, name="cache", ) Workspace-Level Integration At the workspace level, you declare cache configurations globally in your workspace.py. This configures the default backend, serialization, default TTL, and middleware behaviors. from aquilia import Workspace from aquilia.integrations import CacheIntegration workspace = ( Workspace("product-service") .integrate(CacheIntegration( backend="composite", # L1 Memory + L2 Redis composite backend default_ttl=300, serializer="json", redis_url="redis://localhost:6379/0", middleware_enabled=True, # Activates CacheMiddleware middleware_default_ttl=60, )) ) Manifest-Level & Controller Integration Modules declare services and controllers in their AppManifest. Because the cache service is pre-registered in the DI container, components can request CacheService via constructor dependency injection automatically. from aquilia import AppManifest from .controllers import ProductController from .services import ProductService manifest = AppManifest( name="catalog", services=[ProductService], controllers=[ProductController], ) from aquilia import Controller, GET, RequestCtx from aquilia.cache import CacheService class ProductController(Controller): prefix = "/products" # Dependency Injection resolves CacheService automatically def __init__(self, cache: CacheService): self.cache = cache @GET("/ ") async def get_product(self, ctx: RequestCtx): product_id = ctx.path_params["product_id"] # Load from cache, or invoke database loader on miss return await self.cache.get_or_set( key=f"product: ", loader=lambda: self.db_fetch(product_id), ttl=120, tags=("products", f"product: ") ) Current Behavior Notes ))} Dive Deeper , , , , , , ].map((item) => ( ))} )
Go to Homepage