Why I Built Another In-Memory Cache for Go
Описание и применение
Pacecache – небольшая, настраиваемая кэш‑библиотека для Go, обеспечивающая ограничение по количеству записей, точный TTL, координацию одновременных загрузок и порядок мутаций.
Задача
Existing Go cache libraries lack a small, generic in-process cache with explicit bounded capacity, TTL, cache‑aside loading, and concurrent mutation handling.
Результат
Pacecache delivers a small, configurable cache that avoids duplicate upstream calls, handles TTL correctly without relying on cleanup goroutines, and ensures mutation ordering to prevent stale data.
Рабочий процесс
- 1. Import pacecache and create a cache with desired options (max entries, TTL, segments).
- 2. Use GetOrLoadFunc to load missing keys, ensuring only one loader runs per key.
- 3. Handle loader results: cache found=true, ignore found=false or errors.
- 4. Perform Set operations as needed, trusting pacecache’s ordering to prevent stale overwrites.
- 5. Optionally enable background cleanup for physical reclamation.
- 6. Measure segment count and adjust for workload concurrency.
Доказательства
Pacecache coalesces concurrent misses for the same key into one shared load.
TTL correctness does not depend on a cleanup goroutine.
- Дата записи: 2026-09-22T07:30:22.535Z