$ show gookit/cache
gookit/cache is a Go library that provides a unified caching interface (`Cache`) and a registry manager (`Manager`) to wrap multiple backend drivers. The core is minimal and well-abstracted, allowing users to switch between in-memory, file-based, Redis (via go-redis or redigo), Memcached, BuntDB, BoltDB, and other cache systems with a single API. The library includes two built-in drivers (memory and file) and partial stubs for Badger, LevelDB, and NutsDB. The codebase is cleanly structured with a `BaseDriver` that standardizes options like key prefixing, serialization, and logging. The `Manager` pattern is a reusable registry for any service-driven architecture. While several external drivers are outdated or incomplete, the core library is production-ready and well-tested, making it a solid choice for projects needing a simple, pluggable caching layer.
The LLM identified 6 files worth highlighting for reuse:
Provides `BaseDriver` with configurable options (prefix, encode, debug, logger) and helper methods for marshaling/unmarshaling, key building, and error logging. This is a reusable foundation for any cache driver that needs these cross-cutting concerns, and can be extracted into a standalone package.
Implements the `Manager` struct that registers, unregisters, and provides access to named cache drivers. It sets the last registered driver as default and offers quick-use functions. This pattern is readily reusable as a generic service registry (e.g., for database, queue, or logger backends) with minimal adaptation.
A simple but complete in-memory cache with TTL expiration, using `sync.RWMutex` for concurrency safety. It implements the `Cache` interface fully and can be used standalone or as a building block (e.g., in `FileCache`). The `Item` struct with `Expired()` check is a clean pattern for TTL-based caching.
A file-based cache that also keeps an in-memory layer for fast reads. It uses MD5 hashing to generate a deterministic file path, writes serialized JSON, and checks TTL on each read. The structure demonstrates a hybrid disk+memory caching pattern and can be extracted as a standalone persistent cache module.
A Redis driver using the `redigo` package with a connection pool, debug logging, and optional marshaling. It supports `GetAs` for unmarshaling into typed pointers. The use of `BaseDriver` for options makes it consistent with other drivers. This is a well-structured example of wrapping a Redis client into a `Cache` interface.
A complete driver for BuntDB (an embedded key-value store) that supports TTL, multi-get/set, and clear operations. It correctly uses `BaseDriver` for marshaling and prefixing. The implementation is a good reference for wrapping any embedded database into a cache interface.