1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00
Files
go-mixed/cache/free_cache.go
2022-04-26 17:11:45 +08:00

56 lines
938 B
Go

package cache
import (
"errors"
"time"
"github.com/coocood/freecache"
)
const defaultSize = 10 * 1024 * 1024 // 10M
var _ MemCache = &freeCache{}
type freeCache struct {
cache *freecache.Cache
}
func NewFreeCache(size int) *freeCache {
if size < defaultSize {
size = defaultSize
}
// debug.SetGCPercent(20)
c := freecache.NewCache(size)
return &freeCache{
cache: c,
}
}
func (c *freeCache) Get(key string) ([]byte, error) {
return c.cache.Get([]byte(key))
}
func (c *freeCache) Set(key string, value []byte, d time.Duration) error {
s := int(d.Seconds())
return c.cache.Set([]byte(key), value, s)
}
func (c *freeCache) Delete(key string) error {
affected := c.cache.Del([]byte(key))
if !affected {
return errors.New("不存在")
}
return nil
}
func (c *freeCache) Exist(key string) error {
return nil
}
func (c *freeCache) IsNotFound(err error) bool {
return errors.Is(err, freecache.ErrNotFound)
}