chore: 并入 iotlcm-admin-svr 历史到 admin-svr/(原提交 49818c6)
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// Package mcache
|
||||
// @Link https://github.com/xiujiecn/xiu-admin
|
||||
// @Copyright Copyright (c) 2025 LiXiujie
|
||||
// @Author Lxj <xxx@example.com>
|
||||
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
||||
// 内存缓存管理
|
||||
package mcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
)
|
||||
|
||||
// cache 缓存驱动
|
||||
var (
|
||||
mcache *gcache.Cache
|
||||
cacheOnce sync.Once
|
||||
)
|
||||
|
||||
// Instance 缓存实例
|
||||
func Instance() *gcache.Cache {
|
||||
cacheOnce.Do(func() {
|
||||
mcache = gcache.NewWithAdapter(gcache.NewAdapterMemory())
|
||||
})
|
||||
return mcache
|
||||
}
|
||||
|
||||
func Set(ctx context.Context, key string, value interface{}, timeout time.Duration) error {
|
||||
return Instance().Set(ctx, key, value, timeout)
|
||||
}
|
||||
|
||||
func Get(ctx context.Context, key string) (interface{}, error) {
|
||||
return Instance().Get(ctx, key)
|
||||
}
|
||||
|
||||
func Remove(ctx context.Context, key string) error {
|
||||
_, err := Instance().Remove(ctx, key)
|
||||
return err
|
||||
}
|
||||
|
||||
// Clear invalidates every derived in-process cache entry. It is used after the
|
||||
// authoritative SQLite memory database is rebuilt.
|
||||
func Clear(ctx context.Context) error {
|
||||
keys := Instance().MustKeys(ctx)
|
||||
if len(keys) == 0 {
|
||||
return nil
|
||||
}
|
||||
return Instance().Removes(ctx, keys)
|
||||
}
|
||||
Reference in New Issue
Block a user