Files
fn_registry/functions/infra/cache_to_sqlite.md
T
egutierrez 53200cbc0d feat: funciones Go — core (cron, join_by_key, validate_struct), datascience (pivot, diff_entities), infra (http, cache, cron_ticker)
Nuevas funciones Go con tests en tres dominios:
- core: parse_cron_expr, next_cron_time, join_by_key, validate_struct_fields + tipo CronSchedule
- datascience: pivot (tabla dinámica), diff_entities (comparación de entidades)
- infra: http_get_json, http_post_json, http_download_file, cache_to_sqlite, cron_ticker

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

1.8 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 go infra 1.0.0 impure func CacheToSQLite(dbPath, namespace string) (*SQLiteCache, error) Cache key-value persistido en SQLite con TTL y lazy eviction. Valores almacenados como JSON bytes; el caller serializa y deserializa. Thread-safe con sync.Mutex. Soporta Get, Set, Delete, Clear y GetOrSet.
cache
sqlite
persistence
ttl
key-value
concurrent
false error_go_core
database/sql
encoding/json
sync
time
fmt
true
Set/Get basico
TTL expirado
GetOrSet con factory
Concurrencia (goroutines)
functions/infra/cache_to_sqlite_test.go functions/infra/cache_to_sqlite.go

Ejemplo

cache, err := infra.CacheToSQLite("my_cache.db", "default")
if err != nil {
    log.Fatal(err)
}
defer cache.Close()

// Almacenar JSON bytes con TTL de 1 hora
payload, _ := json.Marshal(map[string]string{"result": "ok"})
cache.Set("key1", payload, time.Hour)

// Recuperar
if v, ok := cache.Get("key1"); ok {
    var result map[string]string
    json.Unmarshal(v, &result)
    fmt.Println(result["result"]) // ok
}

// Factory pattern
val, err := cache.GetOrSet("expensive_key", func() ([]byte, error) {
    return json.Marshal(computeExpensiveThing())
}, time.Hour)

// Helper para serializar directamente
cache.SetJSON("user:42", userStruct, 30*time.Minute)

Notas

Usa WAL mode para mejor concurrencia de lecturas. La eviction lazy elimina expirados en cada Get. El schema comparte la tabla cache con cache_to_sqlite_py_infra — ambas implementaciones son interoperables sobre el mismo archivo SQLite si usan namespaces distintos. Requiere github.com/mattn/go-sqlite3 (ya presente en el registry).