本地缓存工具
This commit is contained in:
43
lib/core/cache/lcoal.go
vendored
Normal file
43
lib/core/cache/lcoal.go
vendored
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user