# patrickmn/go-cache

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/patrickmn-go-cache).**

8,828 stars · 904 forks · Go · MIT

## Links

- GitHub: https://github.com/patrickmn/go-cache
- Homepage: https://patrickmn.com/projects/go-cache/
- awesome-repositories: https://awesome-repositories.com/repository/patrickmn-go-cache.md

## Topics

`cache` `go` `library`

## Description

go-cache is a thread-safe, in-memory cache library for Go that stores arbitrary objects with per-item expiration timestamps. It provides a concurrent key-value store where multiple goroutines can safely read and write shared cached data without external synchronization, using a mutex-guarded map for access control.

The library distinguishes itself through its expiration management and optional disk persistence. Each cached item carries its own time-to-live, and a background goroutine periodically purges expired entries. The cache can serialize its entire contents to disk using Go's gob encoding format, allowing cached data to survive application restarts and be reloaded on startup.

The library supports storing any Go object with configurable expiration durations, from temporary entries to items that persist indefinitely. Its file serialization uses gob encoding for type-safe cross-version compatibility, making it suitable for single-machine applications that need a lightweight alternative to external caching systems.

## Tags

### Data & Databases

- [Go In-Memory Caches](https://awesome-repositories.com/f/data-databases/go-in-memory-caches.md) — Provides an in-memory key-value store for Go applications with configurable expiration and concurrent access.
- [Concurrent Caches](https://awesome-repositories.com/f/data-databases/concurrent-write-optimizations/concurrent-caches.md) — Shares a cache safely across multiple goroutines without manual locking or race conditions.
- [Lazy Expiration](https://awesome-repositories.com/f/data-databases/database-record-management/expired-record-purging/lazy-expiration.md) — Checks expiration timestamps during cache access and periodic sweeps to remove stale entries.
- [In-Memory Caches](https://awesome-repositories.com/f/data-databases/in-memory-caches.md) — Stores frequently accessed data in memory to reduce latency and database load for single-machine applications.
- [Key-Value Stores](https://awesome-repositories.com/f/data-databases/key-value-stores.md) — Provides a simple in-memory key-value store as a lightweight alternative to external caching systems.
- [RWMutex-Guarded Stores](https://awesome-repositories.com/f/data-databases/key-value-stores/concurrent/single-writer-multi-reader-stores/rwmutex-guarded-stores.md) — Uses a sync.RWMutex to allow multiple readers or a single writer without race conditions.
- [Cache Expiration Management](https://awesome-repositories.com/f/data-databases/metadata-caching/cache-expiration-management.md) — Automatically removes cached items after a configurable duration to ensure data freshness.
- [Cache State Persistences](https://awesome-repositories.com/f/data-databases/cache-eviction-policies/disk-persistence/cache-state-persistences.md) — Saves the in-memory cache to a file and reloads it on startup for recovery after restarts.
- [Cache-to-Disk Serializations](https://awesome-repositories.com/f/data-databases/cache-eviction-policies/disk-persistence/cache-to-disk-serializations.md) — Persists the in-memory cache to a file and reloads it on startup to recover from downtime. ([source](https://cdn.jsdelivr.net/gh/patrickmn/go-cache@master/README.md))

### Web Development

- [Time-To-Live Expirations](https://awesome-repositories.com/f/web-development/data-fetching-caching/cache-invalidation-utilities/time-to-live-expirations.md) — Attaches a creation time and duration to each cached entry for precise expiration checks.
- [Background Cleanup Intervals](https://awesome-repositories.com/f/web-development/data-fetching-caching/cache-invalidation-utilities/time-to-live-expirations/background-cleanup-intervals.md) — Provides a background goroutine that periodically purges expired cache entries at configurable intervals.

### Software Engineering & Architecture

- [Cache State Serializations](https://awesome-repositories.com/f/software-engineering-architecture/workflow-persistence/state-persistence/file-based-persistence/cache-state-serializations.md) — Serializes the entire cache to a file using gob encoding for disk persistence and recovery after restarts.

### Part of an Awesome List

- [Caching Libraries](https://awesome-repositories.com/f/awesome-lists/data/caching-libraries.md) — Simple in-memory KV cache.
- [Databases and Storage](https://awesome-repositories.com/f/awesome-lists/data/databases-and-storage.md) — A simple in-memory key-value store for single-node applications.
