From f1778c39c5fd8ba88389e61ae339d318c9177a4e Mon Sep 17 00:00:00 2001 From: Yun Date: Fri, 29 Dec 2023 16:22:07 +0800 Subject: [PATCH] =?UTF-8?q?get=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cachex.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/cachex.go b/cachex.go index 57b843e..3c91c9a 100644 --- a/cachex.go +++ b/cachex.go @@ -1,6 +1,7 @@ package cachex import ( + "errors" "sync" "time" ) @@ -10,11 +11,12 @@ import ( // 1.全局单实例 // 到设定的点就删除 -type cache struct{ +type cache struct { store sync.Map } + var one sync.Once -var c cache +var c *cache type cacheData struct { key string @@ -22,11 +24,13 @@ type cacheData struct { expire time.Time } +var ErrorEmpty error = errors.New("empty cache") + func NewCache() *cache { one.Do(func() { - c = cache{} + c = &cache{} - go func(){ + go func() { for { c.store.Range(func(key, value interface{}) bool { if value.(*cacheData).expire.Before(time.Now()) { @@ -34,32 +38,35 @@ func NewCache() *cache { } return true }) - + time.Sleep(time.Second * 5) } }() }) - return &c + return c } // 设置缓存 func (c *cache) Set(key string, value interface{}, expire time.Duration) { + if expire == 0 { + expire = time.Hour * 24 * 365 + } cd := &cacheData{key, value, time.Now().Add(expire)} c.store.Store(key, cd) } // 读取缓存 -func (c *cache) Get(key string) interface{} { +func (c *cache) Get(key string) (interface{}, error) { if v, ok := c.store.Load(key); ok { cc := v.(*cacheData) if cc.expire.Before(time.Now()) { c.store.Delete(key) - return nil + return nil, ErrorEmpty } - return cc.data + return cc.data, nil } - return nil + return nil, ErrorEmpty } // 删除缓存