Files
iotlcm/server/internal/library/mcache/mcache.go
T
2026-10-29 14:36:49 +08:00

53 lines
1.2 KiB
Go

// 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)
}