本地缓存工具

This commit is contained in:
TsMask
2023-08-29 16:13:02 +08:00
parent 5f33e26036
commit 40bc098910

43
lib/core/cache/lcoal.go vendored Normal file
View File

@@ -0,0 +1,43 @@
package cache
import (
"time"
"github.com/patrickmn/go-cache"
)
// 创建一个全局的不过期缓存对象
var cNoExpiration = cache.New(cache.NoExpiration, cache.NoExpiration)
// 将数据放入缓存,并设置永不过期
func SetLocal(key string, value any) {
cNoExpiration.Set(key, value, cache.NoExpiration)
}
// 从缓存中获取数据
func GetLocal(key string) (any, bool) {
return cNoExpiration.Get(key)
}
// 从缓存中删除数据
func DeleteLocal(key string) {
cNoExpiration.Delete(key)
}
// 创建一个全局的过期缓存对象
var cTTL = cache.New(6*time.Hour, 12*time.Hour)
// 设置具有过期时间的缓存项
func SetLocalTTL(key string, value any, expiration time.Duration) {
cTTL.Set(key, value, expiration)
}
// 从缓存中获取数据
func GetLocalTTL(key string) (any, bool) {
return cTTL.Get(key)
}
// 从缓存中删除数据
func DeleteLocalTTL(key string) {
cTTL.Delete(key)
}