kos/pkg/cache/memcache.go

34 lines
683 B
Go
Raw Permalink Normal View History

2023-04-23 17:57:36 +08:00
package cache
import (
"context"
"github.com/patrickmn/go-cache"
"time"
)
type MemCache struct {
engine *cache.Cache
}
func (cache *MemCache) Set(ctx context.Context, key string, value any) {
cache.engine.Set(key, value, 0)
}
func (cache *MemCache) SetEx(ctx context.Context, key string, value any, expire time.Duration) {
cache.engine.Set(key, value, expire)
}
func (cache *MemCache) Get(ctx context.Context, key string) (value any, ok bool) {
return cache.engine.Get(key)
}
func (cache *MemCache) Del(ctx context.Context, key string) {
cache.engine.Delete(key)
}
func NewMemCache() *MemCache {
return &MemCache{
engine: cache.New(time.Hour, time.Minute*90),
}
}