go-cache

A thread-safe, in-memory key-value cache for Go with per-item expiration, atomic counters, and optional disk persistence.

Library
Go
vv2.1.0+incompatible
8,841 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
47 /100 Fair
Development Activity 0
Maintenance 20
Community 68
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
75 /100 Good
Architecture 80
Code Quality 78
Innovation 55
Learning Curve 85

go-cache is a thread-safe, in-memory key-value store for Go, built around a simple map[string]interface{} guarded by a sync.RWMutex, with per-item expiration times. It behaves like a single-process Memcached: any Go value can be cached forever or for a set duration without the overhead of serializing data or making network round-trips to an external cache server.

Expired items are automatically purged by a background janitor goroutine on a configurable interval, and the entire cache can be saved to and restored from disk using gob encoding — useful for surviving short-lived restarts without a cold cache. An experimental sharded cache implementation is also included (unexported, opt-in via the package’s internal APIs) for workloads that need finer-grained locking than the single-mutex default.

What You Get

  • A generic Cache type usable with any Go value via interface{}, with Set/Get/GetWithExpiration/Delete and typed Increment/Decrement helpers for every numeric kind.
  • Configurable default expiration and cleanup interval, set once at construction time with New() or NewFrom().
  • Optional persistence to disk (or any io.Writer) via gob encoding through Save/SaveFile and Load/LoadFile.
  • An OnEvicted callback hook fired on delete or expiry, plus an experimental sharded cache variant for high-concurrency scenarios.

Common Use Cases

  • Caching expensive computed responses or database query results in a single-process API service without standing up Redis or Memcached.
  • Tracking per-key counters (rate limits, request counts) with Increment/IncrementInt and an expiration window that resets automatically.
  • Storing short-lived session data or parsed auth tokens with expirations that mirror the token’s own TTL.
  • Persisting a long-running worker’s in-memory cache to disk with SaveFile so it can reload with LoadFile after a deploy or crash.

Under The Hood

Architecture

The package is a single cache package split across two files: cache.go holds the primary implementation and sharded.go an experimental, currently-unexported sharded variant. The public Cache type wraps an internal *cache struct purely so runtime.SetFinalizer can stop the background janitor goroutine when the wrapper is garbage-collected — a deliberate embedding trick called out directly in the source comments. All state lives behind a single sync.RWMutex; read paths (Get, GetWithExpiration) take a read lock and inline the expiration check to avoid an extra function call, while writes (Set, Add, Replace, the typed Increment*/Decrement* family) take the write lock. The sharded variant in sharded.go distributes keys across N independent *cache buckets chosen by a custom djb33 hash, so a change to the core cache struct’s locking model would also need to stay consistent with the sharded bucket type it embeds.

Tech Stack

Zero third-party dependencies — the entire implementation uses only the Go standard library: sync for locking, time for expiration arithmetic, encoding/gob for persistence, crypto/rand/math/big (with an insecurerand fallback) for the sharded cache’s hash seed, and runtime/os for the finalizer and CSPRNG-failure warning. There is no go.mod in the repository; the module is published under the legacy pre-Go-modules convention, reflected in its +incompatible version tag, and consumers import it as a plain GOPATH-style package.

Code Quality

cache_test.go contains 72 test functions covering the base cache, expiration timing, NewFrom round-tripping, pointer storage, and every typed Increment*/Decrement* variant (int8 through uint64 and both float sizes), plus 19 benchmark functions comparing locked vs. sharded performance; sharded_test.go adds a further concurrency-focused test. Error handling is explicit and idiomatic — Add/Replace/typed increments return a descriptive error rather than panicking or silently no-op’ing. Naming follows standard Go exported/unexported conventions throughout. No linter configuration, formatter config, or CI workflow file is present in the repository, and there is no go.mod, so the project predates (or opted out of) modern Go tooling conventions.

API Design The public surface mirrors familiar cache semantics (Set/Get/Delete, DefaultExpiration/NoExpiration sentinels) so it reads naturally to anyone who has used Memcached-style clients. The type-assertion burden inherent to a pre-generics interface{} value store is called out directly in the README with idiomatic patterns for consumers, and the typed IncrementInt64/IncrementFloat64-style helpers exist specifically to avoid a second type assertion after an atomic update — a small but genuinely convenience-focused API decision rather than a generic wrapper.

Used by 16 apps in this directory

Go
95%
Other

NetBird

Security

30,175

Replace your VPN with a zero-trust WireGuard overlay network that auto-connects devices, enforces SSO and posture checks, and deploys in under 5 minutes.

View details
92
Repo Health
82
Technical
66
Dependency
Built with
Go 95%
Updated 1 weeks ago
Go
67%
MIT

nginx ignition

Developer Tools · Devops

2,141

A modern web UI for nginx that eliminates config file editing with visual virtual hosts, SSL automation, Docker integrations, and real-time traffic analytics.

View details
74
Repo Health
80
Technical
67
Dependency
Built with
Go 67%
TypeScript 28%
Updated 2 weeks ago
Go
98%
Apache 2.0

Nightingale

Monitoring

13,383

Open-source alerting engine that connects to any time-series or log data source and routes alarms to 20+ notification channels with AI-assisted triage.

View details
95
Repo Health
77
Technical
67
Dependency
Built with
Go 98%
Updated 1 weeks ago
TypeScript
51%
ZLIB

Portainer

Devops

38,707

A lightweight, open-source web UI that puts Docker, Kubernetes, and Podman management within reach of any team—no CLI expertise required.

View details
93
Repo Health
79
Technical
65
Dependency
Built with
TypeScript 51%
Go 38%
Updated 1 weeks ago
Go
75%
AGPL 3.0

Teleport

Authentication · Security

21,016

Zero-trust infrastructure access platform that replaces credentials and VPNs with short-lived certificates, SSO, and identity-aware proxies for SSH, Kubernetes, databases, RDP, and AI agents.

View details
94
Repo Health
81
Technical
68
Dependency
Built with
Go 75%
TypeScript 16%
Updated 1 weeks ago
Go
94%
MIT

Traefik

Automation · Devops · Security

65,344

A cloud-native reverse proxy and load balancer that auto-configures itself from Docker, Kubernetes, and other orchestrators — zero manual routing required.

View details
93
Repo Health
85
Technical
64
Dependency
Built with
Go 94%
Updated 1 weeks ago
Go
79%
MIT

Wakapi

Analytics · Developer Tools

4,462

Self-hosted WakaTime-compatible coding statistics backend that gives developers full control over their coding activity data.

View details
89
Repo Health
77
Technical
72
Dependency
Built with
Go 79%
HTML 11%
Updated 1 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers