Skip to content

Cache

provide.foundation.serialization.cache

TODO: Add module docstring.

Functions

get_cache_enabled

get_cache_enabled() -> bool

Whether caching is enabled.

Source code in provide/foundation/serialization/cache.py
def get_cache_enabled() -> bool:
    """Whether caching is enabled."""
    config = _get_cache_config()
    return config.cache_enabled

get_cache_key

get_cache_key(content: str, format: str) -> str

Generate cache key from content and format.

Parameters:

Name Type Description Default
content str

String content to hash

required
format str

Format identifier (json, yaml, toml, etc.)

required

Returns:

Type Description
str

Cache key string

Source code in provide/foundation/serialization/cache.py
def get_cache_key(content: str, format: str) -> str:
    """Generate cache key from content and format.

    Args:
        content: String content to hash
        format: Format identifier (json, yaml, toml, etc.)

    Returns:
        Cache key string

    """
    content_hash = hashlib.sha256(content.encode()).hexdigest()[:16]
    return f"{format}:{content_hash}"

get_cache_size

get_cache_size() -> int

Cache size limit.

Source code in provide/foundation/serialization/cache.py
def get_cache_size() -> int:
    """Cache size limit."""
    config = _get_cache_config()
    return config.cache_size

get_serialization_cache

get_serialization_cache() -> Any

Get or create serialization cache with thread-safe lazy initialization.

Lock overhead (~20-50ns) is negligible compared to actual cache operations (~100-1000ns lookup, ~1-100μs for serialization).

Source code in provide/foundation/serialization/cache.py
def get_serialization_cache() -> Any:  # LRUCache
    """Get or create serialization cache with thread-safe lazy initialization.

    Lock overhead (~20-50ns) is negligible compared to actual cache operations
    (~100-1000ns lookup, ~1-100μs for serialization).
    """
    global _serialization_cache

    with _cache_lock:
        if _serialization_cache is None:
            from provide.foundation.utils.caching import LRUCache, register_cache

            config = _get_cache_config()
            _serialization_cache = LRUCache(maxsize=config.cache_size)
            register_cache("serialization", _serialization_cache)
        return _serialization_cache

reset_serialization_cache_config

reset_serialization_cache_config() -> None

Reset cached config for testing purposes.

Thread-safe reset that acquires the lock.

Source code in provide/foundation/serialization/cache.py
def reset_serialization_cache_config() -> None:
    """Reset cached config for testing purposes.

    Thread-safe reset that acquires the lock.
    """
    global _cached_config, _serialization_cache
    with _cache_lock:
        _cached_config = None
        _serialization_cache = None