A multi-driver cache library for Go with a unified key/value API. Switch between embedded, in-memory, or Redis storage by changing a single option — no code changes needed.
| Driver | Backend | Storage |
|---|---|---|
kind.BUNTDB |
tidwall/buntdb | File-based (embedded) |
kind.REDIS |
redis/go-redis v9 | Redis server |
kind.GOCACHE |
patrickmn/go-cache | In-memory |
go get github.com/thiagozs/go-cacheimport (
"github.com/thiagozs/go-cache"
"github.com/thiagozs/go-cache/kind"
)c, err := cache.New(
cache.OptDriver(kind.BUNTDB),
cache.OptFolder("./data"),
cache.OptFile("cache.db"),
)c, err := cache.New(
cache.OptDriver(kind.REDIS),
cache.OptHost("localhost"),
cache.OptPort(6379),
)c, err := cache.New(
cache.OptDriver(kind.GOCACHE),
cache.OptTimeExpiration(5 * time.Minute),
cache.OptTimeCleanUpInt(10 * time.Minute),
)// Write
c.WriteKeyVal(key, value string) error
c.WriteKeyValTTL(key, value string, ttlSeconds int) error
c.WriteKeyValAsJSON(key string, value any) error
c.WriteKeyValAsJSONTTL(key string, value any, ttlSeconds int) error
// Read / Delete
c.GetVal(key string) (string, error)
c.DeleteKey(key string) (string, error)
// Counters (atomic)
c.Incr(key string) (int64, error)
c.Decr(key string) (int64, error)
// Meta
c.GetDriver() kind.DriverWriteKeyValAsJSON / WriteKeyValAsJSONTTL marshal any value to JSON before storing — the stored string is retrieved unchanged via GetVal.
Incr / Decr expect the stored value to be a numeric string (e.g. "0"). They are mutex-protected on BuntDB and GoCache; Redis uses its native INCR/DECR commands.
Pass OptCache(repo) to bypass driver construction and inject any CacheRepo implementation directly — useful for testing with mocks:
ctrl := gomock.NewController(t)
mock := cache.NewMockCacheRepo(ctrl)
c, _ := cache.New(cache.OptCache(mock))| Option | Description |
|---|---|
OptDriver(kind.Driver) |
Selects the backend |
OptLogDebug(bool) |
Enable debug logging |
OptLogDisable(bool) |
Disable all logging |
OptCache(CacheRepo) |
Inject a custom implementation |
| Option | Description |
|---|---|
OptFile(string) |
Database file name |
OptFolder(string) |
Directory for the database file |
OptTTL(int) |
Default TTL in seconds |
| Option | Description |
|---|---|
OptHost(string) |
Server host |
OptPort(int) |
Server port |
OptPassword(string) |
Server password |
OptUser(string) |
Server username |
OptDatabase(int) |
Database index |
OptTTL(int) |
Default TTL in seconds |
OptRedis(*redis.Client) |
Inject an existing client |
| Option | Description |
|---|---|
OptTimeExpiration(time.Duration) |
Item expiration duration |
OptTimeCleanUpInt(time.Duration) |
Cleanup interval |
Full working examples are in the examples/ directory:
Version numbers follow semantic versioning. See tags for available releases. License: MIT.
2026, Thiago Zilli Sarmento. ❤️