https://github.com/tinh-tinh/cacher
⚡Cache Manager for Tinh Tinh framework
https://github.com/tinh-tinh/cacher
cache framework
Last synced: 4 months ago
JSON representation
⚡Cache Manager for Tinh Tinh framework
- Host: GitHub
- URL: https://github.com/tinh-tinh/cacher
- Owner: tinh-tinh
- License: mit
- Created: 2024-10-23T01:39:32.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-12-26T15:56:19.000Z (6 months ago)
- Last Synced: 2025-12-28T04:19:52.299Z (6 months ago)
- Topics: cache, framework
- Language: Go
- Homepage: https://tinh-tinh.github.io/docs/docs/intergrations/cache
- Size: 1.82 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cache for Tinh Tinh
## Overview
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.
## Features
- 🔌 **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
## Installation
```bash
go get -u github.com/tinh-tinh/cacher/v2
```
## Basic Usage
### Setting Up an In-Memory Cache
```go
import "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")
```
### Using Namespaces
```go
cache1 := cacher.NewSchema[string](cacher.Config{
Store: store,
Namespace: "cache1",
})
cache2 := cacher.NewSchema[string](cacher.Config{
Store: store,
Namespace: "cache2",
})
```
### Memcache Example
```go
import "github.com/tinh-tinh/cacher/storage/memcache"
cache := memcache.New(memcache.Options{
Addr: []string{"localhost:11211"},
Ttl: 15 * time.Minute,
})
```
### Redis Example
```go
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,
})
```
## API Overview
The main cache interface provides:
- `Set(key, value, opts...)`: Store a value
- `Get(key)`: Retrieve a value
- `Delete(key)`: Remove a value
- `Clear()`: Remove all values
- `MSet(...params)`: Batch set
- `MGet(...keys)`: Batch get
## Module Integration
You can register the cache as a provider in a Tinh Tinh module and inject it into controllers:
```go
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:
```go
module := core.NewModule(core.NewModuleOptions{
Imports: []core.Modules{
cacher.Register(cacher.Config{ Store: cache }),
userModule,
},
})
```
## Advanced Features
### Compression
Set `CompressAlg` in `Config` to enable data compression:
```go
cache := cacher.NewSchema[string](cacher.Config{
Store: store,
CompressAlg: "gzip", // Enable compression
})
```
### Hooks
Use the `Hooks` field to register cache lifecycle hooks:
```go
cache := cacher.NewSchema[string](cacher.Config{
Store: store,
Hooks: []cacher.Hook{
// Add your hooks here
},
})
```
### Context Operations
Use `SetCtx` and `GetCtx` for context-aware operations:
```go
ctx := context.Background()
err := cache.SetCtx(ctx, "key", value)
data, err := cache.GetCtx(ctx, "key")
```
## Testing
The repository includes comprehensive tests for all stores and features. See:
- `cacher_test.go`
- `storage/memcache/memcache_test.go`
## Contributing
We welcome contributions! Please feel free to submit a Pull Request.
## Support
If you encounter any issues or need help, you can:
- Open an issue in the GitHub repository
- Check our documentation
- Join our community discussions