apifrom.middleware.cache

Caching middleware for APIFromAnything.

This module provides middleware for caching API responses to improve performance.

Overview

Classes

Classes

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.

apifrom.middleware.cache.options

Options for the middleware.

Type:

dict

Initialize a new BaseMiddleware instance.

param **options:

Options for the middleware.

class apifrom.middleware.cache.CacheControl[source]

Decorator for controlling cache behavior on specific endpoints.

apifrom.middleware.cache.cache(ttl=60)
:staticmethod:

Cache an endpoint for the specified TTL.

param ttl:

Time-to-live in seconds

returns:

A decorator function

apifrom.middleware.cache.no_cache(func)
:staticmethod:

Prevent an endpoint from being cached.

param func:

The function to decorate

returns:

The decorated function

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

apifrom.middleware.cache.__call__(scope, receive, send)
:async:

ASGI callable.

param scope:

The ASGI scope.

param receive:

The ASGI receive function.

param send:

The ASGI send function.

apifrom.middleware.cache._generate_cache_key(request)

Generate a cache key for a request.

param request:

The request object

returns:

The cache key

apifrom.middleware.cache._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

apifrom.middleware.cache.process_request(request)
:async:

Process a request through the cache middleware.

param request:

The request object

returns:

The request object

apifrom.middleware.cache.process_response(response)
:async:

Process a response through the cache middleware.

param response:

The response object

returns:

The response object

class apifrom.middleware.cache.MemoryCache(max_size=1000, ttl=60)[source]
Parameters:

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

apifrom.middleware.cache.clear()

Clear the entire cache.

apifrom.middleware.cache.delete(key)

Delete a value from the cache.

param key:

The cache key

apifrom.middleware.cache.get(key)

Get a value from the cache.

param key:

The cache key

returns:

The cached value, or None if not found or expired

apifrom.middleware.cache.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)

class apifrom.middleware.cache.Request(request=None, path_params=None, method=None, path=None, query_params=None, headers=None, body=None, client_ip=None)[source]
Parameters:
  • request (Optional[starlette.requests.Request])

  • path_params (Optional[dict[Any, Any]])

  • method (Optional[str])

  • path (Optional[str])

  • query_params (Optional[dict[Any, Any]])

  • headers (Optional[dict[Any, Any]])

  • body (Optional[Union[str, bytes]])

  • client_ip (Optional[str])

Request class for APIFromAnything.

This class wraps a Starlette request and provides methods for accessing request data in a convenient way.

apifrom.middleware.cache._request

The underlying Starlette request.

apifrom.middleware.cache.path_params

Path parameters extracted from the URL.

apifrom.middleware.cache.query_params

Query parameters extracted from the URL.

apifrom.middleware.cache.headers

HTTP headers.

apifrom.middleware.cache.method

HTTP method.

apifrom.middleware.cache.path

Request path.

apifrom.middleware.cache._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.

class apifrom.middleware.cache.Response(content=None, status_code=200, headers=None, content_type='application/json')[source]
Parameters:
  • content (Any)

  • status_code (int)

  • headers (Optional[Dict[str, str]])

  • content_type (str)

Response class for APIFromAnything.

This class represents an HTTP response and provides methods for setting response data, status code, and headers.

apifrom.middleware.cache.content

The response content.

apifrom.middleware.cache.status_code

The HTTP status code.

apifrom.middleware.cache.headers

HTTP headers.

apifrom.middleware.cache.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.