Skip to content

hyperion.ports.cache

hyperion.ports.cache

Port: a serverless cache for our shenanigans.

Abstract :class:Cache base plus its contract types (:class:CacheStats, :class:CachingError). Concrete adapters (InMemoryCache, LocalFileCache, DynamoDBCache) live in hyperion.adapters.cache.*; Cache.from_config delegates backend selection to :mod:hyperion.composition (the single composition root). The deprecated PersistentCache (the Catalog knot, S7) moved to :mod:hyperion.application.persistent_cache.

Cache

Cache(prefix, hash_keys=True, default_ttl=DEFAULT_TTL_SECONDS)

Bases: ABC

A serverless cache for our shenanigans.

Initializes the cache with the given prefix and default TTL.

Parameters:

Name Type Description Default
prefix str

The prefix for the cache keys.

required
hash_keys bool

Whether to hash the keys.

True
default_ttl int

The default TTL for the cache.

DEFAULT_TTL_SECONDS
Source code in hyperion/ports/cache.py
def __init__(self, prefix: str, hash_keys: bool = True, default_ttl: int = DEFAULT_TTL_SECONDS):
    """Initializes the cache with the given prefix and default TTL.

    Args:
        prefix (str): The prefix for the cache keys.
        hash_keys (bool): Whether to hash the keys.
        default_ttl (int): The default TTL for the cache.
    """
    self.prefix = prefix
    self.hash_keys = hash_keys
    self.default_ttl = default_ttl
    self._stats = CacheStats.empty()

from_config classmethod

from_config()

Creates a cache from the configuration.

This function emulates a singleton pattern, so it will return the same instance for the same configuration.

Returns:

Name Type Description
Cache Cache

A cache instance.

Source code in hyperion/ports/cache.py
@classmethod
def from_config(cls) -> "Cache":
    """
    Creates a cache from the configuration.

    This function emulates a singleton pattern, so it will return the same instance for the same configuration.

    Returns:
        Cache: A cache instance.
    """
    from hyperion import composition

    instance_key = (storage_config.cache_key_prefix, True)
    if instance_key not in Cache._instances:
        cls._instances[instance_key] = composition.default_cache()
    return cls._instances[instance_key]

get

get(key)

Gets a value from the cache.

Source code in hyperion/ports/cache.py
def get(self, key: str) -> str | None:
    """Gets a value from the cache."""
    self._stats.gets += 1
    if (cached := self._get(key)) is not None:
        self._stats.hits += 1
    else:
        self._stats.misses += 1
    return cached

set

set(key, value)

Sets a value in the cache.

Source code in hyperion/ports/cache.py
def set(self, key: str, value: str) -> None:
    """Sets a value in the cache."""
    self._stats.sets += 1
    self._set(key, value)

get_bytes

get_bytes(key)

Gets a bytes value from the cache.

Source code in hyperion/ports/cache.py
def get_bytes(self, key: str) -> bytes | None:
    """Gets a bytes value from the cache."""
    self._stats.gets += 1
    if (cached := self._get_bytes(key)) is not None:
        self._stats.hits += 1
    else:
        self._stats.misses += 1
    return cached

set_bytes

set_bytes(key, value)

Sets a bytes value in the cache.

Source code in hyperion/ports/cache.py
def set_bytes(self, key: str, value: bytes) -> None:
    """Sets a bytes value in the cache."""
    self._stats.sets += 1
    self._set_bytes(key, value)

delete

delete(key)

Deletes a value from the cache.

Source code in hyperion/ports/cache.py
def delete(self, key: str) -> None:
    """Deletes a value from the cache."""
    self._stats.deletes += 1
    self._delete(key)

clear

clear()

Clears the cache.

Source code in hyperion/ports/cache.py
def clear(self) -> None:
    """Clears the cache."""
    self._stats.clears += 1
    self._clear()

hit

hit(key)

Checks if a key exists in the cache.

Source code in hyperion/ports/cache.py
def hit(self, key: str) -> bool:
    """Checks if a key exists in the cache."""
    return self._hit(key)