mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 04:07:47 +08:00
improve package gcache
This commit is contained in:
parent
55a9c0738b
commit
b46e9bb68a
@ -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.
|
||||
|
@ -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()
|
||||
|
@ -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...)
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user