API Reference

gocache

Go cache library with pluggable stores, chaining, metrics, and a context-aware typed interface.

Official Documentation ↗
CompatibilityThis documentation covers version v4.x (lib/v4 modules). Check the official docs for the latest API surface.

Installation

gogo get github.com/eko/gocache/lib/v4

Methods

gocache separates the cache interface from the storage backend. The same application code can run against in-memory, Redis, or chained stores by swapping the store passed to cache.New.

Chaining

A chain tries each store in order. A local in-memory layer absorbs hot reads, while a remote layer keeps data consistent across instances. A hit in a later layer can be written back to earlier layers.

Error handling

Store calls return errors rather than panicking. Treat a cache miss as a normal error path and never let a failed invalidation silently leave stale data in place.

API reference evidence

Fetched from official sources on 2026-11-08T17:22:00+08:00 via WebFetch https://pkg.go.dev/github.com/eko/gocache/lib/v4/cache.

FAQ

Which stores can gocache use?
In-memory stores such as go-cache and bigcache, plus Redis, Memcache, and others through separate store modules. The cache layer stays independent of the backend.
What does chaining give me?
Chain a fast local store with a shared remote store. Reads check the local layer first and fall back to the remote one, then repopulate the local layer.
Is it safe for concurrent use?
The store implementations provide their own synchronization; go-cache and bigcache are concurrency-safe. Avoid sharing mutable values without copying them first.
Does it expose metrics?
Yes. The cache and store layers implement Prometheus collectors, so hit, miss, and error counts can be exported without custom instrumentation.