From 35c671f1e121aa4d3883d5d57e7dc122c0e92c3d Mon Sep 17 00:00:00 2001 From: Yun Date: Fri, 12 Apr 2024 16:44:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E5=89=8D=E7=BC=80=E7=9A=84?= =?UTF-8?q?=E8=BE=93=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cachex.go | 17 ++++------------- cachex_test.go | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/cachex.go b/cachex.go index 60af6ed..16f85b3 100644 --- a/cachex.go +++ b/cachex.go @@ -12,8 +12,7 @@ import ( // 到设定的点就删除 type cache struct { - prefix string - store sync.Map + store sync.Map } type cacheData struct { @@ -24,11 +23,9 @@ type cacheData struct { var ErrorEmpty error = errors.New("empty cache") -func NewCache(prefix string) *cache { +func NewCache() *cache { - c := &cache{ - prefix: prefix, - } + c := &cache{} go func() { for { @@ -51,13 +48,11 @@ func (c *cache) Set(key string, value interface{}, expire time.Duration) { expire = time.Hour * 24 * 365 } cd := &cacheData{key, value, time.Now().Add(expire)} - key = c.prefix + key c.store.Store(key, cd) } // 读取缓存 func (c *cache) Get(key string) (interface{}, error) { - key = c.prefix + key if v, ok := c.store.Load(key); ok { cc := v.(*cacheData) @@ -72,17 +67,13 @@ func (c *cache) Get(key string) (interface{}, error) { // 删除缓存 func (c *cache) Delete(key string) { - key = c.prefix + key c.store.Delete(key) } // 清空缓存 func (c *cache) Clear() { c.store.Range(func(key, value interface{}) bool { - // 根据前缀删除 - if key.(string)[:len(c.prefix)] == c.prefix { - c.store.Delete(key) - } + c.store.Delete(key) return true }) } diff --git a/cachex_test.go b/cachex_test.go index 054b402..3dd321c 100644 --- a/cachex_test.go +++ b/cachex_test.go @@ -9,7 +9,7 @@ import ( ) func TestCache(t *testing.T) { - c := cachex.NewCache("") + c := cachex.NewCache() c.Set("test", "test", time.Second*5)