src/pocketflow/cache

Caching module for PocketFlow

Provides in-memory and persistent caching for LLM responses, embeddings, and other expensive operations.

Types

Cache = ref object
  store*: TableRef[string, CacheEntry]
  maxSize*: int
  defaultTtl*: int
CacheEntry = object
  value*: JsonNode
  timestamp*: Time
  ttlSeconds*: int

Vars

globalCache = newCache(10000, 3600)

Procs

proc clear(cache: Cache) {....raises: [], tags: [], forbids: [].}
Clears all cached entries
proc computeKey(parts: varargs[string]): string {....raises: [], tags: [],
    forbids: [].}
Computes a cache key from multiple parts Uses hash for consistent key generation
proc evictExpired(cache: Cache): int {....raises: [], tags: [TimeEffect],
                                       forbids: [].}

Removes all expired entries

Returns: Number of entries removed

proc get(cache: Cache; key: string): JsonNode {....raises: [KeyError],
    tags: [TimeEffect], forbids: [].}

Gets a value from the cache

Returns: The cached value, or JNull if not found or expired

proc getCached(key: string): JsonNode {....raises: [KeyError], tags: [TimeEffect],
                                        forbids: [].}
Gets a value from the global cache
proc has(cache: Cache; key: string): bool {....raises: [KeyError],
    tags: [TimeEffect], forbids: [].}
Checks if a key exists and is not expired
proc newCache(maxSize: int = 10000; defaultTtl: int = 3600): Cache {....raises: [],
    tags: [], forbids: [].}

Creates a new cache instance

Args: maxSize: Maximum number of entries (default 10000) defaultTtl: Default time-to-live in seconds (default 1 hour)

proc set(cache: Cache; key: string; value: JsonNode; ttl: int = -1) {.
    ...raises: [], tags: [TimeEffect], forbids: [].}

Sets a value in the cache

Args: key: The cache key value: The value to cache ttl: Time-to-live in seconds (-1 uses default)

proc setCached(key: string; value: JsonNode; ttl: int = -1) {....raises: [],
    tags: [TimeEffect], forbids: [].}
Sets a value in the global cache
proc size(cache: Cache): int {....raises: [], tags: [], forbids: [].}
Returns the number of cached entries