Back
Cache
Key/value storage with TTL, in two modes: in-memory (default) or Redis,
shared across replicas. Enabled via CACHE_ENABLED=true.
CACHE_ENABLED=true
CACHE_DRIVER=memory # memory | redis
CACHE_ADDR=127.0.0.1:6379 # only with CACHE_DRIVER=redis
CACHE_PASSWORD= # Redis AUTH; empty = no password
If the Redis connection fails at boot, bootstrap falls back to an
in-memory cache (not shared across replicas) and logs a warning, instead
of refusing to start. Behavior differs between the two modes: in
memory, the original value comes back exactly as it was stored (same Go
type); in Redis, the value goes through json.Marshal/Unmarshal — a
struct saved with Set comes back from Get as map[string]any, not
its original type. Prefer simple types (string, number, slice, map) in
Redis mode, or decode it yourself into your own target struct from the
returned map.
// Store
fw.Cache.Set("posts:lista", posts, 5*time.Minute)
// Retrieve
if v, ok := fw.Cache.Get("posts:lista"); ok {
posts := v.([]*models.Post)
}
// Remove
fw.Cache.Delete("posts:lista")
Recommended usage pattern in a view:
func ListaView(ctx *router.Context) {
const cacheKey = "posts:lista"
if v, ok := fw.Cache.Get(cacheKey); ok {
render.For("blog").Render(ctx, "lista.html", map[string]any{
"posts": v,
})
return
}
posts, _ := models.ListarPublicados(fw.DB.Use())
fw.Cache.Set(cacheKey, posts, 2*time.Minute)
render.For("blog").Render(ctx, "lista.html", map[string]any{
"posts": posts,
})
}