From b46e9bb68ae5212aff54fc33d381d8534be80658 Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 16 Sep 2021 21:17:03 +0800 Subject: [PATCH] improve package gcache --- os/gcache/gcache.go | 40 -------- os/gcache/gcache_cache.go | 50 ---------- os/gcache/gcache_cache_adapter.go | 10 -- os/gcache/gcache_z_unit_basic_test.go | 132 -------------------------- os/gsession/gsession_manager.go | 2 +- 5 files changed, 1 insertion(+), 233 deletions(-) diff --git a/os/gcache/gcache.go b/os/gcache/gcache.go index f4805fbad..7b10fe52d 100644 --- a/os/gcache/gcache.go +++ b/os/gcache/gcache.go @@ -77,8 +77,6 @@ func SetIfNotExistFuncLock(key interface{}, f func() (interface{}, error), durat // Get retrieves and returns the associated value of given `key`. // It returns nil if it does not exist, or its value is nil, or it's expired. // If you would like to check if the `key` exists in the cache, it's better using function Contains. -// -// It is suggested using GetVar instead for compatibility of different adapters purpose. func Get(key interface{}) (interface{}, error) { return defaultCache.Get(key) } @@ -95,8 +93,6 @@ func GetVar(key interface{}) (*gvar.Var, error) { // It does not expire if `duration` == 0. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing // if `value` is a function and the function result is nil. -// -// It is suggested using GetVarOrSet instead for compatibility of different adapters purpose. func GetOrSet(key interface{}, value interface{}, duration time.Duration) (interface{}, error) { return defaultCache.GetOrSet(key, value, duration) } @@ -108,8 +104,6 @@ func GetOrSet(key interface{}, value interface{}, duration time.Duration) (inter // It does not expire if `duration` == 0. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing // if `value` is a function and the function result is nil. -// -// It is suggested using GetVarOrSetFunc instead for compatibility of different adapters purpose. func GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) { return defaultCache.GetOrSetFunc(key, f, duration) } @@ -124,30 +118,10 @@ func GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time. // // Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within // writing mutex lock for concurrent safety purpose. -// -// It is suggested using GetVarOrSetFuncLock instead for compatibility of different adapters purpose. func GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) { return defaultCache.GetOrSetFuncLock(key, f, duration) } -// GetVarOrSet acts as function GetOrSet except it returns value as type gvar.Var. -// Also see GetOrSet. -func GetVarOrSet(key interface{}, value interface{}, duration time.Duration) (*gvar.Var, error) { - return defaultCache.GetVarOrSet(key, value, duration) -} - -// GetVarOrSetFunc acts as function GetOrSetFunc except it returns value as type gvar.Var. -// Also see GetOrSetFunc. -func GetVarOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, error) { - return defaultCache.GetVarOrSetFunc(key, f, duration) -} - -// GetVarOrSetFuncLock acts as function GetOrSetFuncLock except it returns value as type gvar.Var. -// Also see GetOrSetFuncLock. -func GetVarOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, error) { - return defaultCache.GetVarOrSetFunc(key, f, duration) -} - // Contains checks and returns true if `key` exists in the cache, or else returns false. func Contains(key interface{}) (bool, error) { return defaultCache.Contains(key) @@ -164,18 +138,10 @@ func GetExpire(key interface{}) (time.Duration, error) { // Remove deletes one or more keys from cache, and returns its value. // If multiple keys are given, it returns the value of the last deleted item. -// -// It is suggested using RemoveVar instead for compatibility of different adapters purpose. func Remove(keys ...interface{}) (value interface{}, err error) { return defaultCache.Remove(keys...) } -// RemoveVar acts as function Remove except it returns value as type gvar.Var. -// Also see Remove. -func RemoveVar(keys ...interface{}) (*gvar.Var, error) { - return defaultCache.RemoveVar(keys...) -} - // Removes deletes `keys` in the cache. func Removes(keys []interface{}) error { return defaultCache.Removes(keys) @@ -190,12 +156,6 @@ func Update(key interface{}, value interface{}) (oldValue interface{}, exist boo return defaultCache.Update(key, value) } -// UpdateVar acts as function Update except it returns value as type gvar.Var. -// Also see Update. -func UpdateVar(key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) { - return defaultCache.UpdateVar(key, value) -} - // UpdateExpire updates the expiration of `key` and returns the old expiration duration value. // // It returns -1 and does nothing if the `key` does not exist in the cache. diff --git a/os/gcache/gcache_cache.go b/os/gcache/gcache_cache.go index 2fd34aff2..b0a7d647d 100644 --- a/os/gcache/gcache_cache.go +++ b/os/gcache/gcache_cache.go @@ -69,62 +69,12 @@ func (c *Cache) GetVar(key interface{}) (*gvar.Var, error) { return gvar.New(v), err } -// GetVarOrSet acts as function GetOrSet except it returns value as type gvar.Var. -// Also see GetOrSet. -func (c *Cache) GetVarOrSet(key interface{}, value interface{}, duration time.Duration) (*gvar.Var, error) { - v, err := c.GetOrSet(key, value, duration) - if err != nil { - return nil, err - } - return gvar.New(v), nil -} - -// GetVarOrSetFunc acts as function GetOrSetFunc except it returns value as type gvar.Var. -// Also see GetOrSetFunc. -func (c *Cache) GetVarOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, error) { - v, err := c.GetOrSetFunc(key, f, duration) - if err != nil { - return nil, err - } - return gvar.New(v), nil -} - -// GetVarOrSetFuncLock acts as function GetOrSetFuncLock except it returns value as type gvar.Var. -// Also see GetOrSetFuncLock. -func (c *Cache) GetVarOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, error) { - v, err := c.GetOrSetFuncLock(key, f, duration) - if err != nil { - return nil, err - } - return gvar.New(v), nil -} - -// RemoveVar acts as function Remove except it returns value as type gvar.Var. -// Also see Remove. -func (c *Cache) RemoveVar(keys ...interface{}) (*gvar.Var, error) { - v, err := c.Remove(keys...) - if err != nil { - return nil, err - } - return gvar.New(v), nil -} - // Removes deletes `keys` in the cache. func (c *Cache) Removes(keys []interface{}) error { _, err := c.Remove(keys...) return err } -// UpdateVar acts as function Update except it returns value as type gvar.Var. -// Also see Update. -func (c *Cache) UpdateVar(key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) { - v, exist, err := c.Update(key, value) - if err != nil { - return nil, exist, err - } - return gvar.New(v), exist, err -} - // KeyStrings returns all keys in the cache as string slice. func (c *Cache) KeyStrings() ([]string, error) { keys, err := c.Keys() diff --git a/os/gcache/gcache_cache_adapter.go b/os/gcache/gcache_cache_adapter.go index 7cd5970c4..31eab0f0f 100644 --- a/os/gcache/gcache_cache_adapter.go +++ b/os/gcache/gcache_cache_adapter.go @@ -63,8 +63,6 @@ func (c *Cache) SetIfNotExistFuncLock(key interface{}, f func() (interface{}, er // Get retrieves and returns the associated value of given `key`. // It returns nil if it does not exist, or its value is nil, or it's expired. // If you would like to check if the `key` exists in the cache, it's better using function Contains. -// -// It is suggested using GetVar instead for compatibility of different adapters purpose. func (c *Cache) Get(key interface{}) (interface{}, error) { return c.adapter.Get(c.getCtx(), key) } @@ -76,8 +74,6 @@ func (c *Cache) Get(key interface{}) (interface{}, error) { // It does not expire if `duration` == 0. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing // if `value` is a function and the function result is nil. -// -// It is suggested using GetVarOrSet instead for compatibility of different adapters purpose. func (c *Cache) GetOrSet(key interface{}, value interface{}, duration time.Duration) (interface{}, error) { return c.adapter.GetOrSet(c.getCtx(), key, value, duration) } @@ -89,8 +85,6 @@ func (c *Cache) GetOrSet(key interface{}, value interface{}, duration time.Durat // It does not expire if `duration` == 0. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing // if `value` is a function and the function result is nil. -// -// It is suggested using GetVarOrSetFunc instead for compatibility of different adapters purpose. func (c *Cache) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) { return c.adapter.GetOrSetFunc(c.getCtx(), key, f, duration) } @@ -105,8 +99,6 @@ func (c *Cache) GetOrSetFunc(key interface{}, f func() (interface{}, error), dur // // Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within // writing mutex lock for concurrent safety purpose. -// -// It is suggested using GetVarOrSetFuncLock instead for compatibility of different adapters purpose. func (c *Cache) GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) { return c.adapter.GetOrSetFuncLock(c.getCtx(), key, f, duration) } @@ -127,8 +119,6 @@ func (c *Cache) GetExpire(key interface{}) (time.Duration, error) { // Remove deletes one or more keys from cache, and returns its value. // If multiple keys are given, it returns the value of the last deleted item. -// -// It is suggested using RemoveVar instead for compatibility of different adapters purpose. func (c *Cache) Remove(keys ...interface{}) (value interface{}, err error) { return c.adapter.Remove(c.getCtx(), keys...) } diff --git a/os/gcache/gcache_z_unit_basic_test.go b/os/gcache/gcache_z_unit_basic_test.go index 79776f25a..44708742f 100644 --- a/os/gcache/gcache_z_unit_basic_test.go +++ b/os/gcache/gcache_z_unit_basic_test.go @@ -125,40 +125,6 @@ func TestCache_Update(t *testing.T) { }) } -func TestCache_UpdateVar(t *testing.T) { - // gcache - gtest.C(t, func(t *gtest.T) { - key := guid.S() - t.AssertNil(gcache.Set(key, 11, 3*time.Second)) - expire1, _ := gcache.GetExpire(key) - oldValue, exist, err := gcache.UpdateVar(key, 12) - t.AssertNil(err) - t.Assert(oldValue, 11) - t.Assert(exist, true) - - expire2, _ := gcache.GetExpire(key) - v, _ := gcache.GetVar(key) - t.Assert(v, 12) - t.Assert(math.Ceil(expire1.Seconds()), math.Ceil(expire2.Seconds())) - }) - // gcache.Cache - gtest.C(t, func(t *gtest.T) { - cache := gcache.New() - t.AssertNil(cache.Set(1, 11, 3*time.Second)) - - oldValue, exist, err := cache.UpdateVar(1, 12) - t.AssertNil(err) - t.Assert(oldValue, 11) - t.Assert(exist, true) - - expire1, _ := cache.GetExpire(1) - expire2, _ := cache.GetExpire(1) - v, _ := cache.GetVar(1) - t.Assert(v, 12) - t.Assert(math.Ceil(expire1.Seconds()), math.Ceil(expire2.Seconds())) - }) -} - func TestCache_UpdateExpire(t *testing.T) { // gcache gtest.C(t, func(t *gtest.T) { @@ -485,104 +451,6 @@ func TestCache_GetOrSetFuncLock(t *testing.T) { }) } -func TestCache_GetVarOrSet(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - cache := gcache.New() - value, err := cache.GetVarOrSet(1, 11, 0) - t.AssertNil(err) - t.Assert(value, 11) - - v, _ := cache.GetVar(1) - t.Assert(v, 11) - value, err = cache.GetVarOrSet(1, 111, 0) - t.AssertNil(err) - t.Assert(value, 11) - - v, _ = cache.GetVar(1) - t.Assert(v, 11) - }) - - gtest.C(t, func(t *gtest.T) { - gcache.Remove(g.Slice{1, 2, 3}...) - value, err := gcache.GetVarOrSet(1, 11, 0) - t.AssertNil(err) - t.Assert(value, 11) - - v, err := gcache.GetVar(1) - t.AssertNil(err) - t.Assert(v, 11) - - value, err = gcache.GetVarOrSet(1, 111, 0) - t.AssertNil(err) - t.Assert(value, 11) - - v, err = gcache.GetVar(1) - t.AssertNil(err) - t.Assert(v, 11) - }) -} - -func TestCache_GetVarOrSetFunc(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - cache := gcache.New() - cache.GetVarOrSetFunc(1, func() (interface{}, error) { - return 11, nil - }, 0) - v, _ := cache.GetVar(1) - t.Assert(v, 11) - - cache.GetVarOrSetFunc(1, func() (interface{}, error) { - return 111, nil - }, 0) - v, _ = cache.GetVar(1) - t.Assert(v, 11) - - gcache.RemoveVar(g.Slice{1, 2, 3}...) - - gcache.GetVarOrSetFunc(1, func() (interface{}, error) { - return 11, nil - }, 0) - v, _ = cache.GetVar(1) - t.Assert(v, 11) - - gcache.GetVarOrSetFunc(1, func() (interface{}, error) { - return 111, nil - }, 0) - v, _ = cache.GetVar(1) - t.Assert(v, 11) - }) -} - -func TestCache_GetVarOrSetFuncLock(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - cache := gcache.New() - cache.GetVarOrSetFuncLock(1, func() (interface{}, error) { - return 11, nil - }, 0) - v, _ := cache.GetVar(1) - t.Assert(v, 11) - - cache.GetVarOrSetFuncLock(1, func() (interface{}, error) { - return 111, nil - }, 0) - v, _ = cache.GetVar(1) - t.Assert(v, 11) - - gcache.Remove(g.Slice{1, 2, 3}...) - gcache.GetVarOrSetFuncLock(1, func() (interface{}, error) { - return 11, nil - }, 0) - v, _ = cache.GetVar(1) - t.Assert(v, 11) - - gcache.GetVarOrSetFuncLock(1, func() (interface{}, error) { - return 111, nil - }, 0) - v, _ = cache.GetVar(1) - t.Assert(v, 11) - }) -} - func TestCache_Clear(t *testing.T) { gtest.C(t, func(t *gtest.T) { cache := gcache.New() diff --git a/os/gsession/gsession_manager.go b/os/gsession/gsession_manager.go index 81b308d52..f5eb2e530 100644 --- a/os/gsession/gsession_manager.go +++ b/os/gsession/gsession_manager.go @@ -20,7 +20,7 @@ type Manager struct { storage Storage // Storage interface for session storage. // sessionData is the memory data cache for session TTL, - // which is available only if the Storage does not stores any session data in synchronizing. + // which is available only if the Storage does not store any session data in synchronizing. // Please refer to the implements of StorageFile, StorageMemory and StorageRedis. sessionData *gcache.Cache }