Go-Cache is a hybrid caching library that uses both in-memory and Redis for caching. It provides a simple interface to set, get, and delete cache entries.
- In-memory caching using Ristretto
- Redis caching
- Set and get cache entries
- Delete cache entries
- Delete multiple keys by pattern
- Generate cache keys using SHA256
To install Go-Cache, use go get:
go get github.com/xplore-run/go-cacheexport REDIS_HOST="localhost:6379"
export REDIS_PASSWORD="yhft67egjdkd"import (
"github.com/yourusername/go-cache"
"github.com/redis/go-redis/v9"
"time"
)
func main() {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
cache := cache.NewHybridCache(cache.HybridCacheOption{
ExpiresInMemory: 5 * time.Minute,
ExpiresRedis: 10 * time.Minute,
Prefix: "myapp",
Redis: redisClient,
MaxCost: 1000,
})
}cache.Set("key", []byte("value"))
value, found := cache.Get("key")
if found {
fmt.Println("Cache hit:", string(value))
} else {
fmt.Println("Cache miss")
}cache.Del("key")err := cache.DeleteKeysByPatternFromRedis(context.Background(), "myapp:*")
if err != nil {
log.Fatalf("Error deleting keys: %v", err)
}cacheKey := cache.GetCacheKey("some data")
fmt.Println("Generated cache key:", cacheKey)cache.Close()This project is licensed under the MIT License - see the LICENSE file for details.