apifrom.middleware.cache ======================== Caching middleware for APIFromAnything. This module provides middleware for caching API responses to improve performance. .. py:currentmodule:: apifrom.middleware.cache Overview -------- **Classes** * :py:class:`BaseMiddleware` * :py:class:`CacheControl` * :py:class:`CacheMiddleware` * :py:class:`MemoryCache` * :py:class:`Request` * :py:class:`Response` Classes ------- .. py:class:: BaseMiddleware(**options):bases: abc.ABC Base middleware class for APIFromAnything. This abstract class defines the interface for middleware components. Middleware components can process requests and responses. .. attribute:: options Options for the middleware. :type: dict Initialize a new BaseMiddleware instance. :param \*\*options: Options for the middleware. .. py:class:: CacheControl Decorator for controlling cache behavior on specific endpoints. .. method:: cache(ttl = 60) :staticmethod: Cache an endpoint for the specified TTL. :param ttl: Time-to-live in seconds :returns: A decorator function .. method:: no_cache(func) :staticmethod: Prevent an endpoint from being cached. :param func: The function to decorate :returns: The decorated function .. py:class:: CacheMiddleware(cache_backend = None, ttl = 60, methods = None, exclude_routes = None, vary_headers = None, key_prefix = 'apifrom-cache:'):bases: apifrom.middleware.base.BaseMiddleware Middleware for caching API responses. Initialize the cache middleware. :param cache_backend: The cache backend to use (defaults to MemoryCache) :param ttl: Default time-to-live in seconds for cached items :param methods: HTTP methods to cache (defaults to ["GET"]) :param exclude_routes: Routes to exclude from caching :param vary_headers: Headers to include in the cache key :param key_prefix: Prefix for cache keys .. :: cache .. :: exclude_routes .. :: key_prefix .. :: methods .. :: ttl .. :: vary_headers .. method:: __call__(scope, receive, send) :async: ASGI callable. :param scope: The ASGI scope. :param receive: The ASGI receive function. :param send: The ASGI send function. .. method:: _generate_cache_key(request) Generate a cache key for a request. :param request: The request object :returns: The cache key .. method:: _should_cache(request) Determine if a request should be cached. :param request: The request object :returns: True if the request should be cached, False otherwise .. method:: process_request(request) :async: Process a request through the cache middleware. :param request: The request object :returns: The request object .. method:: process_response(response) :async: Process a response through the cache middleware. :param response: The response object :returns: The response object .. py:class:: MemoryCache(max_size = 1000, ttl = 60) Simple in-memory cache implementation. Initialize the memory cache. :param max_size: Maximum number of items to store in the cache :param ttl: Time-to-live in seconds for cached items .. :: cache :annotation: Dict[str, Dict[str, Any]] .. :: max_size .. :: ttl .. method:: clear() Clear the entire cache. .. method:: delete(key) Delete a value from the cache. :param key: The cache key .. method:: get(key) Get a value from the cache. :param key: The cache key :returns: The cached value, or None if not found or expired .. method:: set(key, value, ttl = None) Set a value in the cache. :param key: The cache key :param value: The value to cache :param ttl: Time-to-live in seconds (overrides the default) .. py:class:: Request(request = None, path_params = None, method = None, path = None, query_params = None, headers = None, body = None, client_ip = None) Request class for APIFromAnything. This class wraps a Starlette request and provides methods for accessing request data in a convenient way. .. attribute:: _request The underlying Starlette request. .. attribute:: path_params Path parameters extracted from the URL. .. attribute:: query_params Query parameters extracted from the URL. .. attribute:: headers HTTP headers. .. attribute:: method HTTP method. .. attribute:: path Request path. .. attribute:: _body Cached request body. Initialize a new Request instance. :param request: The underlying Starlette request. :param path_params: Path parameters extracted from the URL. :param method: The HTTP method. :param path: The request path. :param query_params: Query parameters. :param headers: HTTP headers. :param body: Request body. :param client_ip: Client IP address. .. py:class:: Response(content = None, status_code = 200, headers = None, content_type = 'application/json') Response class for APIFromAnything. This class represents an HTTP response and provides methods for setting response data, status code, and headers. .. attribute:: content The response content. .. attribute:: status_code The HTTP status code. .. attribute:: headers HTTP headers. .. attribute:: content_type The content type of the response. Initialize a new Response instance. :param content: The response content. :param status_code: The HTTP status code. :param headers: HTTP headers. :param content_type: The content type of the response.