Skip to content

solidworks_mcp.cache.response_cache

solidworks_mcp.cache.response_cache

In-memory response cache with TTL support for adapter operations.

Attributes

T module-attribute

T = TypeVar('T')

Classes

CachePolicy dataclass

CachePolicy(enabled: bool = True, default_ttl_seconds: int = 60, max_entries: int = 512)

Cache policy options for adapter responses.

Attributes:

Name Type Description
default_ttl_seconds int

The default ttl seconds value.

enabled bool

The enabled value.

max_entries int

The max entries value.

ResponseCache

ResponseCache(policy: CachePolicy)

Thread-safe in-memory cache for adapter response objects.

Parameters:

Name Type Description Default
policy CachePolicy

The policy value.

required

Attributes:

Name Type Description
_lock Any

The lock value.

_policy Any

The policy value.

Initialize this cache.

Parameters:

Name Type Description Default
policy CachePolicy

The policy value.

required

Returns:

Name Type Description
None None

None.

Source code in src/solidworks_mcp/cache/response_cache.py
def __init__(self, policy: CachePolicy) -> None:
    """Initialize this cache.

    Args:
        policy (CachePolicy): The policy value.

    Returns:
        None: None.
    """
    self._policy = policy
    self._entries: dict[str, _CacheEntry[object]] = {}
    self._lock = RLock()
Attributes
enabled property
enabled: bool

Return whether caching is enabled.

Returns:

Name Type Description
bool bool

True if enabled, otherwise False.

Functions
get
get(key: str) -> object | None

Fetch a cached value when present and unexpired.

Parameters:

Name Type Description Default
key str

The key value.

required

Returns:

Type Description
object | None

object | None: The result produced by the operation.

Source code in src/solidworks_mcp/cache/response_cache.py
def get(self, key: str) -> object | None:
    """Fetch a cached value when present and unexpired.

    Args:
        key (str): The key value.

    Returns:
        object | None: The result produced by the operation.
    """
    if not self._policy.enabled:
        return None

    now = time.time()
    with self._lock:
        entry = self._entries.get(key)
        if entry is None:
            return None
        if entry.expires_at <= now:
            self._entries.pop(key, None)
            return None
        return entry.value
make_key
make_key(operation: str, payload: object) -> str

Build a deterministic cache key from an operation payload.

Parameters:

Name Type Description Default
operation str

Callable object executed by the helper.

required
payload object

The payload value.

required

Returns:

Name Type Description
str str

The resulting text value.

Source code in src/solidworks_mcp/cache/response_cache.py
def make_key(self, operation: str, payload: object) -> str:
    """Build a deterministic cache key from an operation payload.

    Args:
        operation (str): Callable object executed by the helper.
        payload (object): The payload value.

    Returns:
        str: The resulting text value.
    """
    normalized = self._normalize_payload(payload)
    raw = f"{operation}:{normalized}".encode()
    return hashlib.sha256(raw).hexdigest()
set
set(key: str, value: object, ttl_seconds: int | None = None) -> None

Store a cache value with expiration.

Parameters:

Name Type Description Default
key str

The key value.

required
value object

The value value.

required
ttl_seconds int | None

The ttl seconds value. Defaults to None.

None

Returns:

Name Type Description
None None

None.

Source code in src/solidworks_mcp/cache/response_cache.py
def set(self, key: str, value: object, ttl_seconds: int | None = None) -> None:
    """Store a cache value with expiration.

    Args:
        key (str): The key value.
        value (object): The value value.
        ttl_seconds (int | None): The ttl seconds value. Defaults to None.

    Returns:
        None: None.
    """
    if not self._policy.enabled:
        return

    ttl = (
        ttl_seconds if ttl_seconds is not None else self._policy.default_ttl_seconds
    )
    now = time.time()
    entry = _CacheEntry(value=value, created_at=now, expires_at=now + max(ttl, 1))

    with self._lock:
        if len(self._entries) >= self._policy.max_entries:
            self._evict_oldest_unlocked()
        self._entries[key] = entry

_CacheEntry dataclass

_CacheEntry(value: T, expires_at: float, created_at: float)

Bases: Generic[T]

Internal cache entry with expiration metadata.

Attributes:

Name Type Description
created_at float

The created at value.

expires_at float

The expires at value.

value T

The value value.