Files
egutierrez 47fac22230 chore: auto-commit (799 archivos)
- .claude/CLAUDE.md
- .claude/commands/subagentes.md
- .claude/rules/INDEX.md
- .mcp.json
- bash/functions/cybersecurity/analyze_dns.md
- bash/functions/cybersecurity/audit_http_headers.md
- bash/functions/cybersecurity/audit_ssh_config.md
- bash/functions/cybersecurity/check_firewall.md
- bash/functions/cybersecurity/detect_suspicious_users.md
- bash/functions/cybersecurity/encrypt_file.md
- ...

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 00:28:20 +02:00

65 lines
2.1 KiB
Markdown

---
name: cache_to_sqlite
kind: function
lang: go
domain: infra
version: "1.0.0"
purity: impure
signature: "func CacheToSQLite(dbPath, namespace string) (*SQLiteCache, error)"
description: "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."
tags: [cache, sqlite, persistence, ttl, key-value, concurrent, pendiente-usar]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: ["database/sql", "encoding/json", "sync", "time", "fmt"]
params:
- name: dbPath
desc: "ruta del archivo SQLite donde persiste el cache"
- name: namespace
desc: "prefijo de tabla para aislar caches en el mismo archivo"
output: "instancia thread-safe de SQLiteCache con metodos Get, Set, Delete, Clear, GetOrSet"
tested: true
tests:
- "Set/Get basico"
- "TTL expirado"
- "GetOrSet con factory"
- "Concurrencia (goroutines)"
test_file_path: "functions/infra/cache_to_sqlite_test.go"
file_path: "functions/infra/cache_to_sqlite.go"
---
## Ejemplo
```go
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).