From 40bc0989107f2a07fea42716672b667c350d4ef2 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Tue, 29 Aug 2023 16:13:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E7=BC=93=E5=AD=98=E5=B7=A5?= =?UTF-8?q?=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/cache/lcoal.go | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/core/cache/lcoal.go diff --git a/lib/core/cache/lcoal.go b/lib/core/cache/lcoal.go new file mode 100644 index 00000000..62f776a4 --- /dev/null +++ b/lib/core/cache/lcoal.go @@ -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) +}