The Cache Manager provides a unified API to manage caching in Tinh Tinh applications. It supports memory, Memcache, and Redis backends, and can be flexibly configured and injected into your modules and controllers.
- 🔌 Pluggable Backends: Supports in-memory, Memcache, and Redis stores
- 🛡️ Type Safety: Generic interface for strong typing
- 🏷️ Namespace Support: Isolate cache by logical namespace
- 📦 Compression: Optional data compression
- 🌐 Context-aware: Supports context propagation for advanced use cases
- 🎣 Hooks: Register hooks for cache lifecycle events
go get -u github.com/tinh-tinh/cacher/v2import "github.com/tinh-tinh/cacher/v2"
cache := cacher.NewSchema[string](cacher.Config{
Store: cacher.NewInMemory(cacher.StoreOptions{
Ttl: 15 * time.Minute,
}),
})
err := cache.Set("users", "John")
data, err := cache.Get("users")cache1 := cacher.NewSchema[string](cacher.Config{
Store: store,
Namespace: "cache1",
})
cache2 := cacher.NewSchema[string](cacher.Config{
Store: store,
Namespace: "cache2",
})import "github.com/tinh-tinh/cacher/storage/memcache"
cache := memcache.New(memcache.Options{
Addr: []string{"localhost:11211"},
Ttl: 15 * time.Minute,
})import (
"github.com/tinh-tinh/cacher/storage/redis"
redis_store "github.com/redis/go-redis/v9"
)
cache := redis.New(redis.Options{
Connect: &redis_store.Options{
Addr: "localhost:6379",
},
Ttl: 15 * time.Minute,
})The main cache interface provides:
Set(key, value, opts...): Store a valueGet(key): Retrieve a valueDelete(key): Remove a valueClear(): Remove all valuesMSet(...params): Batch setMGet(...keys): Batch get
You can register the cache as a provider in a Tinh Tinh module and inject it into controllers:
import (
"github.com/tinh-tinh/cacher/v2"
"github.com/tinh-tinh/tinhtinh/v2/core"
)
func userController(module core.Module) core.Controller {
cache := cacher.Inject[[]byte](module)
ctrl := module.NewController("users")
ctrl.Get("", func(ctx core.Ctx) error {
data, err := cache.Get("users")
// handle data
})
return ctrl
}To register the cache provider:
module := core.NewModule(core.NewModuleOptions{
Imports: []core.Modules{
cacher.Register(cacher.Config{ Store: cache }),
userModule,
},
})Set CompressAlg in Config to enable data compression:
cache := cacher.NewSchema[string](cacher.Config{
Store: store,
CompressAlg: "gzip", // Enable compression
})Use the Hooks field to register cache lifecycle hooks:
cache := cacher.NewSchema[string](cacher.Config{
Store: store,
Hooks: []cacher.Hook{
// Add your hooks here
},
})Use SetCtx and GetCtx for context-aware operations:
ctx := context.Background()
err := cache.SetCtx(ctx, "key", value)
data, err := cache.GetCtx(ctx, "key")The repository includes comprehensive tests for all stores and features. See:
cacher_test.gostorage/memcache/memcache_test.go
We welcome contributions! Please feel free to submit a Pull Request.
If you encounter any issues or need help, you can:
- Open an issue in the GitHub repository
- Check our documentation
- Join our community discussions