golang-lru

HashiCorp's generic-typed, thread-safe LRU cache for Go, with ARC, 2Q, and TTL-expiring variants.

Library
Go
vv2.0.7
5,129 stars
Mozilla Public License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
71 /100 Good
Development Activity 60
Maintenance 36
Community 88
Maturity 60
Momentum 40

Technical Analysis

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

golang-lru is HashiCorp’s fixed-size, thread-safe LRU (Least Recently Used) cache implementation for Go, built on generics for full type safety. It began as a wrapper around Groupcache’s cache logic and now ships four interchangeable cache variants in one module: a classic LRU, a 2Q cache that resists scan pollution, an ARC (Adaptive Replacement Cache) that balances recency and frequency automatically, and an expirable LRU with per-entry TTL.

The library is used throughout HashiCorp’s own tooling, including Vault, Consul, and Nomad, as well as many other Go projects that need bounded in-memory caching without standing up an external cache server. Each variant lives as a small, dependency-light package under a shared module, so teams pick exactly the eviction strategy their workload needs.

What You Get

  • A generic, thread-safe Cache[K, V] type implementing classic LRU eviction
  • An expirable subpackage adding per-cache TTL expiration on top of LRU semantics
  • A 2Q cache variant that resists pollution from one-time scan access patterns
  • An ARC (Adaptive Replacement Cache) variant that auto-balances recency vs frequency
  • Optional eviction callbacks fired outside the internal lock for safe cleanup/metrics hooks

Common Use Cases

  • In-process caching of expensive API or database responses to cut redundant calls
  • Bounding memory for connection or session pools inside infrastructure daemons
  • Building TTL-based rate limiters or short-lived counters with the expirable variant
  • Scan-resistant read-through caching where hot keys must survive occasional bulk scans

Under The Hood

Architecture

The module is organized as a set of sibling packages sharing one generic doubly-linked-list implementation in internal/list.go (adapted from Go’s container/list, with an added expiry timestamp field). The root lru package wraps a non-thread-safe simplelru.LRU[K, V] behind a sync.RWMutex and an optional eviction-callback buffer that is drained outside the critical section (see Cache.Purge and Cache.onEvicted in lru.go) to avoid calling user code while holding the lock. The arc package composes four simplelru.LRUCache instances (T1/T2 for recent/frequent entries, B1/B2 as ghost eviction-history lists) to implement the ARC algorithm, while expirable reimplements the list with time-aware eviction and a background sweep goroutine. This layered composition means every variant reuses the same core list/map primitives rather than duplicating cache logic.

Tech Stack

Pure Go with no third-party runtime dependencies; go.mod declares go 1.19 to use generics throughout (K comparable, V any). The arc subpackage is its own Go module with its own go.mod, isolating it for versioning. Build/test tooling is a plain Makefile plus go test; CI runs via GitHub Actions (.github/workflows/ci.yml) with Dependabot keeping the module and Actions versions current, and a HashiCorp copywrite check enforces license headers.

Code Quality

Every package ships a matching _test.go file (lru_test.go, simplelru/lru_test.go, arc/arc_test.go, expirable/expirable_lru_test.go, 2q_test.go) exercising eviction order, capacity limits, callback firing, and expiry timing. Errors are returned explicitly (e.g. New returns an error for non-positive size) rather than panicking, and public types are documented with doc comments throughout. Locking is consistent: every thread-safe wrapper holds its own sync.RWMutex rather than sharing state across packages.

What Makes It Unique

Rather than picking one eviction policy, the module exposes four algorithmically distinct caches behind a near-identical API, letting callers swap ARC for plain LRU or add TTL expiration without changing call sites. The ARC implementation in particular is a comparatively rare open-source instance of IBM’s patented Adaptive Replacement Cache algorithm made available under a permissive-enough license for general use, giving Go projects an alternative to manually tuning 2Q’s fixed parameters.

Used by 22 apps in this directory

TypeScript
71%
Other

highlight.io

Analytics · Developer Tools · Monitoring

9,384

Open-source full-stack monitoring that unifies session replay, error tracking, logging, and distributed tracing so you can stop context-switching between tools.

View details
76
Repo Health
78
Technical
65
Dependency
Built with
TypeScript 71%
Go 16%
Updated 1 weeks ago
Go
97%
Apache 2.0

infracost

Developer Tools · Devops

12,585

Infracost shows cloud cost estimates for Terraform, CloudFormation, and AWS CDK before you deploy — in your terminal, editor, AI coding agent, and pull requests.

View details
83
Repo Health
78
Technical
68
Dependency
Built with
Go 97%
Updated 1 weeks ago
Go
79%
Apache 2.0

OpenMeter

Developer Tools · Invoicing Finance

2,401

Open-source metering and billing engine for AI, agentic, and DevTool monetization — ingest usage events in real time and turn them into accurate invoices automatically.

View details
87
Repo Health
81
Technical
66
Dependency
Built with
Go 79%
TypeScript 16%
Updated 1 weeks ago
Go
91%
Apache 2.0

Ory Kratos

Authentication

13,933

API-first identity and user management that handles login, registration, MFA, and recovery so your application never has to.

View details
69
Repo Health
78
Technical
65
Dependency
Built with
Go 91%
Updated 3 months 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
Python
55%
Other

PostHog

Ab Testing Experimentation · Analytics · Developer Tools

40,397

The all-in-one open source product platform combining analytics, session replay, feature flags, error tracking, AI observability, and a built-in data warehouse in a single self-hostable stack.

View details
92
Repo Health
80
Technical
65
Dependency
Built with
Python 55%
TypeScript 36%
Updated 6 days ago
Go
90%
AGPL 3.0

Pyroscope

Developer Tools · Devops · Monitoring

11,703

An open-source, horizontally scalable continuous profiling platform that pinpoints CPU, memory, and I/O bottlenecks down to the exact line of code, built by Grafana Labs alongside Loki, Tempo, and Mimir.

View details
92
Repo Health
89
Technical
65
Dependency
Built with
Go 90%
Updated 6 days ago
Go
40%
Apache 2.0

Rill

Analytics · Data Engineering

2,962

The fastest BI tool for humans and agents — define metrics, models, and dashboards as code and query them instantly on ClickHouse or DuckDB.

View details
88
Repo Health
88
Technical
64
Dependency
Built with
Go 40%
TypeScript 38%
Svelte 21%
Updated 6 days 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

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