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)