A Go in-memory cache library with generics, automatic item expiration, and event-driven architecture.
TTLCache is a Go library that provides an in-memory key-value store with automatic expiration of items based on time-to-live (TTL). It is designed for applications requiring efficient, temporary data storage with fine-grained control over cache behavior and lifecycle events.
Go developers building applications that need efficient, temporary data storage with automatic expiration, such as web servers, APIs, or background job processors requiring caching with TTL management.
Developers choose TTLCache for its idiomatic Go API, type safety via generics, and extensibility through features like event handlers and a loader interface, offering control over caching strategies without sacrificing performance or thread safety.
An in-memory cache with item expiration and generics
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Leverages Go generics to provide compile-time type safety for any key-value pair, as demonstrated in cache instantiation like `ttlcache.New[string, string]()`.
Offers flexible TTL per item with options for automatic background cleanup via `Start()` or manual control with `DeleteExpired()`, allowing precise expiration handling.
Includes callbacks for insertion, update, and eviction events, enabling custom logic such as logging or metrics integration, shown in the `OnInsertion`, `OnUpdate`, and `OnEviction` methods.
Supports a Loader interface to fetch data on cache misses, reducing external dependencies and improving efficiency, as illustrated with `LoaderFunc`.
The custom cost function for memory usage explicitly omits internal overhead, leading to potential miscalculations in capacity management, as noted in the README example.
Automatic expiration requires managing a background goroutine with `Start()` or periodic calls to `DeleteExpired()`, adding complexity and risk of resource leaks if not properly handled.
Data is stored purely in-memory and lost on application restart, limiting suitability for scenarios requiring durability or fault tolerance.