gocache
Go cache library with pluggable stores, chaining, metrics, and a context-aware typed interface.
Official Documentation ↗v4.x (lib/v4 modules). Check the official docs for the latest API surface.Installation
go get github.com/eko/gocache/lib/v4Methods
No methods match this filter.
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.
cache.New→ signature from https://pkg.go.dev/github.com/eko/gocache/lib/v4/cache; official text: “func New[T any](store store.StoreInterface) *Cache[T]”Cache.Set→ signature from https://pkg.go.dev/github.com/eko/gocache/lib/v4/cache; official text: “func (c *Cache[T]) Set(ctx context.Context, key any, object T, options …store.Option) error”Cache.Get→ signature from https://pkg.go.dev/github.com/eko/gocache/lib/v4/cache; official text: “func (c *Cache[T]) Get(ctx context.Context, key any) (T, error)”Cache.Delete→ signature from https://pkg.go.dev/github.com/eko/gocache/lib/v4/cache; official text: “func (c *Cache[T]) Delete(ctx context.Context, key any) error”Cache.Clear→ signature from https://pkg.go.dev/github.com/eko/gocache/lib/v4/cache; official text: “func (c *Cache[T]) Clear(ctx context.Context) error”
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.