GO 缓存库,支持 go-redis 和 go-cache 作为缓存驱动。它提供了一个简单的接口,允许您轻松地在您的项目中实现缓存。
使用以下命令将 cacheit
添加到您的 Go 项目中:
go get github.com/feymanlee/cacheit
type Driver[V any] interface {
// Add Store an item in the cache if the key doesn't exist.
Add(key string, value V, t time.Duration) error
// Set Store an item in the cache for a given number of seconds.
Set(key string, value V, t time.Duration) error
// SetMany Store multiple items in the cache for a given number of seconds.
SetMany(many []Many[V]) error
// Forever Store an item in the cache indefinitely.
Forever(key string, value V) error
// Forget Remove an item from the cache.
Forget(key string) error
// Flush Remove all items from the cache.
Flush() error
// Get Retrieve an item from the cache by key.
Get(key string) (V, error)
// Has Determined if an item exists in the cache.
Has(key string) (bool, error)
// Many Retrieve multiple items from the cache by key.
// Items not found in the cache will have a zero value.
Many(keys []string) (map[string]V, error)
// SetNumber set the number value of an item in the cache.
SetNumber(key string, value V, t time.Duration) error
// Increment the value of an item in the cache.
Increment(key string, n V) (V, error)
// Decrement the value of an item in the cache.
Decrement(key string, n V) (V, error)
// Remember Get an item from the cache, or execute the given Closure and store the result.
Remember(key string, ttl time.Duration, callback func () (V, error)) (V, error)
// RememberForever Get an item from the cache, or execute the given Closure and store the result forever.
RememberForever(key string, callback func () (V, error)) (V, error)
// TTL Get cache ttl
TTL(key string) (time.Duration, error)
// WithCtx with context
WithCtx(ctx context.Context) Driver[V]
// WithWithSerializer with cache serializer
WithSerializer(serializer Serializer) Driver[V]
}
package main
import (
"fmt"
"log"
"time"
"github.com/feymanlee/cacheit"
"github.com/go-redis/redis/v8"
gocache "github.com/patrickmn/go-cache"
)
func main() {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
err := cacheit.RegisterRedisDriver("redis_test", redisClient, "cache_prefix")
if err != nil {
log.Fatal(err)
}
// go-cache 客户端
memCache := gocache.New(5*time.Minute, 10*time.Minute)
err = cacheit.RegisterGoCacheDriver("memory_test", memCache, "cache_prefix")
if err != nil {
log.Fatal(err)
}
// set default cache
cacheit.SetDefault("redis_test")
driver, err := cacheit.Use[string]("memory_test")
if err != nil {
log.Fatal(err)
}
err = driver.Set("cache_key", "cache_value", time.Minute)
if err != nil {
log.Fatal(err)
}
get, err := cacheit.UseDefault[string]().Get("cache_key")
if err != nil {
return
}
fmt.Println(get)
}
Store an item in the cache if the key doesn't exist.
err = driver.Add("key", "value", time.Minute*10)
if err != nil {
log.Println("Error adding cache:", err)
}
Store an item in the cache for a given number of seconds.
err = driver.Set("key2", "value2", time.Minute*5)
if err != nil {
log.Println("Error setting cache:", err)
}
Retrieve an item from the cache by key.
value, err := driver.Get("key2")
if err != nil {
log.Println("Error getting cache:", err)
} else {
log.Println("Cache value:", value)
}
Store multiple items in the cache for a given number of seconds.
err = driver.SetMany([]cacheit.Many[string]{
{Key: "key3", Value: "value3", TTL: time.Minute * 10},
{Key: "key4", Value: "value4", TTL: time.Minute * 15},
})
if err != nil {
log.Println("Error setting many caches:", err)
}
Retrieve multiple items from the cache by key. Items not found in the cache will have a zero value.
values, err := driver.Many([]string{"key2", "key3", "key4"})
if err != nil {
log.Println("Error getting many caches:", err)
} else {
log.Println("Cache values:", values)
}
Store an item in the cache indefinitely.
err = driver.Forever("key5", "value5")
if err != nil {
log.Println("Error storing cache forever:", err)
}
Remove an item from the cache.
err = driver.Forget("key")
if err != nil {
log.Println("Error removing cache:", err)
}
Remove all items from the cache.
err = driver.Flush()
if err != nil {
log.Println("Error flushing cache:", err)
}
Determined if an item exists in the cache.
has, err := driver.Has("key2")
if err != nil {
log.Println("Error checking if cache exists:", err)
} else if has {
log.Println("Cache key2 exists")
} else {
log.Println("Cache key2 does not exist")
}
Set the number value of an item in the cache.
err = driver.SetNumber("number_key", 1, time.Minute*10)
if err != nil {
log.Println("Error setting number cache:", err)
}
Increment the value of an item in the cache.
newValue, err := driver.Increment("number_key", 1)
if err != nil {
log.Println("Error incrementing cache value:", err)
} else {
log.Println("Incremented cache value:", newValue)
}
newValue, err := driver.Increment("number_key", 1)
if err != nil {
log.Println("Error incrementing cache value:", err)
} else {
log.Println("Incremented cache value:", newValue)
}
rememberValue, err := driver.Remember("remember_key", time.Minute*10, func () (string, error) {
time.Sleep(time.Millisecond * 50)
return "remember_value", nil
})
if err != nil {
log.Println("Error remembering cache:", err)
} else {
log.Println("Remember cache value:", rememberValue)
}
rememberForeverValue, err := driver.RememberForever("remember_forever_key", func() (string, error) {
time.Sleep(time.Millisecond * 50)
return "remember_forever_value", nil
})
if err != nil {
log.Println("Error remembering cache forever:", err)
} else {
log.Println("Remember cache value forever:", rememberForeverValue)
}
ttl, err := driver.TTL("key2")
if err != nil {
log.Println("Error getting cache TTL:", err)
} else {
log.Println("Cache TTL:", ttl)
}
err = driver.WithCtx(context.TODO()).Set("key2", "value2", time.Minute*5)
if err != nil {
log.Println("Error setting cache:", err)
}
请查阅源代码以了解更多功能和用法。
欢迎向项目贡献代码、提交 bug 报告或提出新功能建议。请务必遵循贡献指南。
GoLand A Go IDE with extended support for JavaScript, TypeScript, and databases。
特别感谢 JetBrains 为开源项目提供免费的 GoLand 等 IDE 的授权
本项目基于 MIT 许可证 发布。