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>
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
---
|
||||
name: cache_to_file
|
||||
kind: function
|
||||
lang: py
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "def cache_to_file(cache_dir: str, namespace: str = 'default') -> FileCache"
|
||||
description: "Cache key-value donde cada entry es un archivo JSON en disco. Keys se hashean con SHA-256 para generar nombres de archivo seguros. Metadata (ttl, created_at, original_key) en sidecar .meta. Mejor que SQLite para valores grandes (PDFs procesados, embeddings)."
|
||||
tags: [cache, file, persistence, ttl, key-value, sha256]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: ["os", "json", "hashlib", "time", "threading"]
|
||||
tested: true
|
||||
tests:
|
||||
- "Set y get basico"
|
||||
- "TTL expirado → None"
|
||||
- "Archivo .meta con metadata correcta"
|
||||
- "Clear elimina el directorio del namespace"
|
||||
- "Key con caracteres especiales → hash seguro"
|
||||
test_file_path: "python/functions/infra/cache_to_file_test.py"
|
||||
file_path: "python/functions/infra/cache_to_file.py"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```python
|
||||
from infra.cache_to_file import cache_to_file
|
||||
|
||||
store = cache_to_file("/tmp/my_cache", namespace="embeddings")
|
||||
|
||||
# Almacenar un embedding grande
|
||||
store.set("doc:123", embedding_vector, ttl=86400)
|
||||
|
||||
# Recuperar
|
||||
vec = store.get("doc:123")
|
||||
|
||||
# Factory pattern
|
||||
result = store.get_or_set(
|
||||
"pdf:page_42",
|
||||
factory=lambda: extract_pdf_text("doc.pdf", page=42),
|
||||
ttl=0, # sin expiracion
|
||||
)
|
||||
```
|
||||
|
||||
## Estructura en disco
|
||||
|
||||
```
|
||||
cache_dir/
|
||||
namespace/
|
||||
{sha256_key}.json # valor serializado como JSON
|
||||
{sha256_key}.meta # {"created_at": ..., "expires_at": ..., "original_key": ...}
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Cada entry genera exactamente dos archivos: `.json` para el valor y `.meta` para la metadata. La key original se guarda en `.meta["original_key"]` para facilitar debugging. Thread-safe mediante `threading.Lock`. La eviction es lazy: se verifica expires_at al hacer `get`.
|
||||
Reference in New Issue
Block a user