Files
fn_registry/python/functions/infra/cache_to_sqlite.md
T
egutierrez 5a324f6554 feat: funciones Python infra y tipos Python (core, datascience, infra)
Infra: cache_to_file, cache_to_sqlite, http_download_file, http_get_json,
http_post_json, read_file_with_encoding, safe_extract_zip, scan_directory,
setup_logger, normalize_zip_filenames.
Tipos: 30+ tipos core (agent_action, context, task, message, parse_result...),
6 tipos datascience (entity_candidate, extraction_result...), 2 tipos infra.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 17:11:43 +02:00

1.9 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports tested tests test_file_path file_path
cache_to_sqlite function py infra 1.0.0 impure def cache_to_sqlite(db_path: str, namespace: str = 'default') -> CacheStore Cache key-value persistido en SQLite con TTL y lazy eviction. Cada namespace es un espacio logico dentro de la misma BD. Keys son strings, values se serializan con JSON. TTL en segundos, 0 = sin expiracion. Thread-safe mediante mutex.
cache
sqlite
persistence
ttl
memoize
key-value
false error_go_core
sqlite3
json
time
threading
true
Set y get basico
TTL expirado → None
TTL 0 → nunca expira
get_or_set con factory que solo se llama en miss
Namespaces independientes
Clear elimina solo el namespace
Stats contadores correctos
Concurrencia (threading basico)
python/functions/infra/cache_to_sqlite_test.py python/functions/infra/cache_to_sqlite.py

Ejemplo

from infra.cache_to_sqlite import cache_to_sqlite

store = cache_to_sqlite("my_cache.db", namespace="llm")

# Almacenar con TTL de 1 hora
store.set("prompt:explain_x", "explanation...", ttl=3600)

# Recuperar (None si miss o expirado)
val = store.get("prompt:explain_x")

# Factory pattern: solo computa si no esta en cache
result = store.get_or_set(
    "prompt:explain_y",
    factory=lambda: call_llm("explain y"),
    ttl=3600,
)

# Estadisticas
print(store.stats())  # {"hits": 2, "misses": 1, "size": 5}

Notas

La eviction de entradas expiradas es lazy: se ejecuta en cada llamada a get o stats, no en background. El schema SQLite usa (namespace, key) como PRIMARY KEY para garantizar upserts atomicos. Usa WAL mode para mejor concurrencia de lecturas. Cada thread mantiene su propia conexion SQLite (thread-local), sincronizada via threading.Lock para escrituras.