apifrom.middleware.cache_advanced

Advanced caching middleware for APIFromAnything.

This module provides enhanced caching functionality with various backends, eviction policies, and optimization features.

Overview

Classes

Classes

AdvancedCacheMiddleware(cache_backend = None, ttl = 60, cache_methods = None, ignore_paths = None, vary_headers = None, cache_control_header = True, compress_responses = False, endpoint_ttls = None, auto_vary = True, invalidation_strategy = None):bases: CacheMiddleware

Advanced middleware for caching API responses.

This middleware extends the basic CacheMiddleware with additional features such as per-endpoint TTL, response compression, and automatic cache key generation.

Initialize the advanced cache middleware.

param cache_backend:

The cache backend to use (defaults to MemoryCacheBackend)

param ttl:

Default time-to-live in seconds

param cache_methods:

Set of HTTP methods to cache (defaults to {“GET”})

param ignore_paths:

Set of paths to exclude from caching

param vary_headers:

Set of headers to include in the cache key

param cache_control_header:

Whether to set Cache-Control headers

param compress_responses:

Whether to compress responses before caching

param endpoint_ttls:

Dictionary mapping endpoints to TTL values

param auto_vary:

Whether to automatically determine vary headers

param invalidation_strategy:

Strategy for cache invalidation

apifrom.middleware.cache_advanced._generate_cache_key(request)

Generate a cache key for a request.

param request:

The request object

returns:

A cache key string

apifrom.middleware.cache_advanced._get_endpoint_ttl(path)

Get the TTL for an endpoint.

param path:

The endpoint path

returns:

The TTL in seconds

apifrom.middleware.cache_advanced.clear()

Clear the cache.

apifrom.middleware.cache_advanced.get_stats()

Get cache statistics.

returns:

A dictionary of cache statistics

apifrom.middleware.cache_advanced.invalidate(pattern)

Invalidate cache entries matching a pattern.

param pattern:

The pattern to match

apifrom.middleware.cache_advanced.process_request(request, call_next)
:async:

Process a request and potentially return a cached response.

param request:

The request object

param call_next:

The next middleware function

returns:

The response object

class apifrom.middleware.cache_advanced.CacheBackend[source]

Base class for cache backends.

apifrom.middleware.cache_advanced.clear()
:abstractmethod:

Clear the cache.

apifrom.middleware.cache_advanced.delete(key)
:abstractmethod:

Delete a value from the cache.

param key:

The cache key

returns:

True if the key was deleted, False otherwise

apifrom.middleware.cache_advanced.get(key)
:abstractmethod:

Get a value from the cache.

param key:

The cache key

returns:

The cached value, or None if not found

apifrom.middleware.cache_advanced.get_stats()
:abstractmethod:

Get cache statistics.

returns:

A dictionary of cache statistics

apifrom.middleware.cache_advanced.set(key, value, ttl=60)
:abstractmethod:

Set a value in the cache.

param key:

The cache key

param value:

The value to cache

param ttl:

Time-to-live in seconds

class apifrom.middleware.cache_advanced.CacheControl[source]

Cache control decorators for API endpoints.

This class provides decorators to control caching behavior for API endpoints. It includes decorators to cache responses, prevent caching, and invalidate cache entries.

Example

```python from apifrom import API, api from apifrom.middleware.cache_advanced import CacheControl

app = API()

@api(route=”/users/{user_id}”, method=”GET”) @CacheControl.cache(ttl=60, tags=[“user”]) def get_user(user_id: str):

# This response will be cached for 60 seconds return {“id”: user_id, “name”: “John”}

@api(route=”/users/{user_id}”, method=”PUT”) @CacheControl.invalidate([“user”]) def update_user(user_id: str, name: str):

# This will invalidate all cache entries with the “user” tag return {“id”: user_id, “name”: name}

@api(route=”/users/{user_id}/sensitive”, method=”GET”) @CacheControl.no_cache def get_sensitive_user_data(user_id: str):

# This response will not be cached return {“id”: user_id, “ssn”: “123-45-6789”}

```

apifrom.middleware.cache_advanced.cache(ttl=60, tags=None, dependencies=None)
:staticmethod:

Decorator to cache the response of an API endpoint.

param ttl:

Time to live in seconds

param tags:

Tags to associate with the cache entry

param dependencies:

Dependencies to associate with the cache entry

returns:

A decorator function

apifrom.middleware.cache_advanced.invalidate(patterns)
:staticmethod:

Decorator to invalidate cache entries matching the given patterns.

param patterns:

Patterns to match cache keys

returns:

A decorator function

apifrom.middleware.cache_advanced.no_cache(func)
:staticmethod:

Decorator to prevent caching of an API endpoint.

param func:

The function to decorate

returns:

The decorated function

class apifrom.middleware.cache_advanced.CacheEvictionPolicy[source]

Base class for cache eviction policies.

apifrom.middleware.cache_advanced.select_items_to_evict(items, target_size)
:abstractmethod:

Select items to evict to reach the target size.

param items:

The list of cache items

param target_size:

The target number of items to keep

returns:

A list of items to evict

class apifrom.middleware.cache_advanced.CacheItem(key, value, ttl=60, created_at=None)[source]
Parameters:
  • key (str)

  • value (Any)

  • ttl (int)

  • created_at (Optional[float])

Represents an item in the cache with metadata.

apifrom.middleware.cache_advanced.key

The cache key

apifrom.middleware.cache_advanced.value

The cached value

apifrom.middleware.cache_advanced.expires_at

The expiration timestamp

apifrom.middleware.cache_advanced.created_at

The creation timestamp

apifrom.middleware.cache_advanced.last_accessed_at

The last access timestamp

apifrom.middleware.cache_advanced.access_count

The number of times the item has been accessed

apifrom.middleware.cache_advanced.size_bytes

The size of the item in bytes

Initialize a cache item.

param key:

The cache key

param value:

The value to cache

param ttl:

Time-to-live in seconds

param created_at:

Creation timestamp (defaults to now)

apifrom.middleware.cache_advanced.access()

Update the last accessed time and access count.

apifrom.middleware.cache_advanced.get_age_seconds()

Get the age of the item in seconds.

returns:

The age in seconds

apifrom.middleware.cache_advanced.get_idle_time_seconds()

Get the idle time of the item in seconds.

returns:

The idle time in seconds

apifrom.middleware.cache_advanced.is_expired()

Check if the item is expired.

returns:

True if the item is expired, False otherwise

apifrom.middleware.cache_advanced.to_dict()

Convert the cache item to a dictionary.

returns:

A dictionary representation of the cache item

CacheMiddleware(cache_backend = None, ttl = 60, cache_methods = None, ignore_paths = None, vary_headers = None, cache_control_header = True):bases: apifrom.middleware.base.Middleware

Middleware for caching API responses.

This middleware caches API responses based on the request method, path, and query parameters. It supports various cache backends and configuration options.

Initialize the cache middleware.

param cache_backend:

The cache backend to use (defaults to MemoryCacheBackend)

param ttl:

Default time-to-live in seconds

param cache_methods:

Set of HTTP methods to cache (defaults to {“GET”})

param ignore_paths:

Set of paths to exclude from caching

param vary_headers:

Set of headers to include in the cache key

param cache_control_header:

Whether to set Cache-Control headers

apifrom.middleware.cache_advanced._generate_cache_key(request)

Generate a cache key for a request.

param request:

The request object

returns:

A cache key string

apifrom.middleware.cache_advanced._should_cache(request)

Check if a request should be cached.

param request:

The request object

returns:

True if the request should be cached, False otherwise

apifrom.middleware.cache_advanced._should_cache_response(response)

Check if a response should be cached.

param response:

The response object

returns:

True if the response should be cached, False otherwise

apifrom.middleware.cache_advanced.clear()

Clear the cache.

apifrom.middleware.cache_advanced.get_stats()

Get cache statistics.

returns:

A dictionary of cache statistics

apifrom.middleware.cache_advanced.process_request(request, call_next)
:async:

Process a request and potentially return a cached response.

param request:

The request object

param call_next:

The next middleware function

returns:

The response object

class apifrom.middleware.cache_advanced.DependencyBasedInvalidation(cache_backend)[source]
Parameters:

cache_backend (CacheBackend)

Dependency-based cache invalidation strategy.

This class provides a way to invalidate cache entries based on dependencies. Dependencies are relationships between cache entries, where invalidating one entry will also invalidate all entries that depend on it.

Example

```python # Create a cache backend cache_backend = MemoryCacheBackend()

# Create a dependency-based invalidation strategy invalidation = DependencyBasedInvalidation(cache_backend)

# Set a cache entry with dependencies cache_backend.set(“user:123”, {“name”: “John”}) invalidation.add_dependency(“user:123”, “users”)

# Set another cache entry with dependencies cache_backend.set(“post:456”, {“title”: “Hello”}) invalidation.add_dependency(“post:456”, “posts”) invalidation.add_dependency(“post:456”, “user:123”)

# Invalidate all cache entries that depend on “user:123” invalidation.invalidate(“user:123”) ```

Initialize the dependency-based invalidation strategy.

param cache_backend:

The cache backend to use

apifrom.middleware.cache_advanced.add_dependencies(key, dependencies)

Add multiple dependencies to a cache entry.

param key:

The cache key

param dependencies:

List of dependency keys

apifrom.middleware.cache_advanced.add_dependency(key, dependency)

Add a dependency to a cache entry.

param key:

The cache key

param dependency:

The dependency key

apifrom.middleware.cache_advanced.get_dependencies(key)

Get all dependencies of a cache entry.

param key:

The cache key

returns:

A list of dependency keys

apifrom.middleware.cache_advanced.get_dependents(key)

Get all cache entries that depend on a key.

param key:

The dependency key

returns:

A list of cache keys

apifrom.middleware.cache_advanced.invalidate(key)

Invalidate a cache entry and all entries that depend on it.

param key:

The key to invalidate

apifrom.middleware.cache_advanced.invalidate_dependency(dependency)

Invalidate all cache entries that depend on a specific dependency.

param dependency:

The dependency key to invalidate

HybridEvictionPolicy:bases: CacheEvictionPolicy

Hybrid eviction policy combining multiple factors.

apifrom.middleware.cache_advanced.select_items_to_evict(items, target_size)

Select items to evict based on a hybrid policy.

param items:

The list of cache items

param target_size:

The target number of items to keep

returns:

A list of items to evict

LFUEvictionPolicy:bases: CacheEvictionPolicy

Least Frequently Used (LFU) eviction policy.

apifrom.middleware.cache_advanced.select_items_to_evict(items, target_size)

Select items to evict based on the LFU policy.

param items:

The list of cache items

param target_size:

The target number of items to keep

returns:

A list of items to evict

LRUEvictionPolicy:bases: CacheEvictionPolicy

Least Recently Used (LRU) eviction policy.

apifrom.middleware.cache_advanced.select_items_to_evict(items, target_size)

Select items to evict based on the LRU policy.

param items:

The list of cache items

param target_size:

The target number of items to keep

returns:

A list of items to evict

MemoryCacheBackend(max_items = 1000, max_size_bytes = 50 * 1024 * 1024, eviction_policy = None):bases: CacheBackend

In-memory cache backend.

Initialize the memory cache backend.

param max_items:

Maximum number of items to store

param max_size_bytes:

Maximum cache size in bytes

param eviction_policy:

The eviction policy to use

apifrom.middleware.cache_advanced._cleanup_if_needed(force=False)

Clean up expired items if needed.

param force:

Whether to force cleanup regardless of the time since last cleanup

apifrom.middleware.cache_advanced._evict_if_needed()

Evict items if the cache exceeds its limits.

apifrom.middleware.cache_advanced.clear()

Clear the cache.

apifrom.middleware.cache_advanced.delete(key)

Delete a value from the cache.

param key:

The cache key

returns:

True if the key was deleted, False otherwise

apifrom.middleware.cache_advanced.get(key)

Get a value from the cache.

param key:

The cache key

returns:

The cached value, or None if not found

apifrom.middleware.cache_advanced.get_stats()

Get cache statistics.

returns:

A dictionary of cache statistics

apifrom.middleware.cache_advanced.set(key, value, ttl=60)

Set a value in the cache.

param key:

The cache key

param value:

The value to cache

param ttl:

Time-to-live in seconds

Middleware(app, dispatch = None):bases: starlette.middleware.base.BaseHTTPMiddleware, BaseMiddleware

Middleware class for APIFromAnything.

This class implements the BaseMiddleware interface and extends Starlette’s BaseHTTPMiddleware to provide a middleware component that can be used with Starlette.

Initialize a new BaseMiddleware instance.

param **options:

Options for the middleware.

RedisCacheBackend(redis_url = 'redis://localhost:6379/0', prefix = 'apifrom:', serializer = None, deserializer = None):bases: CacheBackend

Redis cache backend.

Initialize the Redis cache backend.

param redis_url:

The Redis connection URL

param prefix:

The key prefix to use

param serializer:

Function to serialize values to bytes

param deserializer:

Function to deserialize bytes to values

apifrom.middleware.cache_advanced._make_key(key)

Create a prefixed key.

param key:

The original key

returns:

The prefixed key

apifrom.middleware.cache_advanced.clear()

Clear the cache.

apifrom.middleware.cache_advanced.delete(key)

Delete a value from the cache.

param key:

The cache key

returns:

True if the key was deleted, False otherwise

apifrom.middleware.cache_advanced.get(key)

Get a value from the cache.

param key:

The cache key

returns:

The cached value, or None if not found

apifrom.middleware.cache_advanced.get_stats()

Get cache statistics.

returns:

A dictionary of cache statistics

apifrom.middleware.cache_advanced.set(key, value, ttl=60)

Set a value in the cache.

param key:

The cache key

param value:

The value to cache

param ttl:

Time-to-live in seconds

class apifrom.middleware.cache_advanced.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_advanced._request

The underlying Starlette request.

apifrom.middleware.cache_advanced.path_params

Path parameters extracted from the URL.

apifrom.middleware.cache_advanced.query_params

Query parameters extracted from the URL.

apifrom.middleware.cache_advanced.headers

HTTP headers.

apifrom.middleware.cache_advanced.method

HTTP method.

apifrom.middleware.cache_advanced.path

Request path.

apifrom.middleware.cache_advanced._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_advanced.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_advanced.content

The response content.

apifrom.middleware.cache_advanced.status_code

The HTTP status code.

apifrom.middleware.cache_advanced.headers

HTTP headers.

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

SizeEvictionPolicy:bases: CacheEvictionPolicy

Size-based eviction policy.

apifrom.middleware.cache_advanced.select_items_to_evict(items, target_size)

Select items to evict based on size.

param items:

The list of cache items

param target_size:

The target number of items to keep

returns:

A list of items to evict

TTLEvictionPolicy:bases: CacheEvictionPolicy

Time-To-Live (TTL) eviction policy.

apifrom.middleware.cache_advanced.select_items_to_evict(items, target_size)

Select items to evict based on the TTL policy.

param items:

The list of cache items

param target_size:

The target number of items to keep

returns:

A list of items to evict

class apifrom.middleware.cache_advanced.TagBasedInvalidation(cache_backend)[source]
Parameters:

cache_backend (CacheBackend)

Tag-based cache invalidation strategy.

This class provides a way to invalidate cache entries based on tags. Tags are arbitrary strings that can be associated with cache entries. When a tag is invalidated, all cache entries associated with that tag are also invalidated.

Example

```python # Create a cache backend cache_backend = MemoryCacheBackend()

# Create a tag-based invalidation strategy invalidation = TagBasedInvalidation(cache_backend)

# Set a cache entry with tags cache_backend.set(“user:123”, {“name”: “John”}) invalidation.tag(“user:123”, [“user”, “user:123”])

# Invalidate all cache entries with the “user” tag invalidation.invalidate_tag(“user”) ```

Initialize the tag-based invalidation strategy.

param cache_backend:

The cache backend to use

apifrom.middleware.cache_advanced.get_keys_for_tag(tag)

Get all cache keys associated with a tag.

param tag:

The tag to get keys for

returns:

A list of cache keys

apifrom.middleware.cache_advanced.invalidate_tag(tag)

Invalidate all cache entries associated with a tag.

param tag:

The tag to invalidate

apifrom.middleware.cache_advanced.invalidate_tags(tags)

Invalidate all cache entries associated with any of the given tags.

param tags:

The tags to invalidate

apifrom.middleware.cache_advanced.tag(key, tags)

Tag a cache entry with one or more tags.

param key:

The cache key

param tags:

The tags to associate with the key