change returned value by Get* and Update/Remove functions from type interface{} to *gvar.Var for package gcache

This commit is contained in:
John Guo
2021-09-16 22:13:00 +08:00
parent b46e9bb68a
commit 1455e30350
10 changed files with 64 additions and 92 deletions

View File

@ -539,7 +539,7 @@ func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, e
if len(cacheKey) == 0 {
cacheKey = sql + ", @PARAMS:" + gconv.String(args)
}
if v, _ := cacheObj.GetVar(cacheKey); !v.IsNil() {
if v, _ := cacheObj.Get(cacheKey); !v.IsNil() {
if result, ok := v.Val().(Result); ok {
// In-memory cache.
return result, nil

View File

@ -56,7 +56,7 @@ func (s *Server) getHandlersWithCache(r *Request) (parsedItems []*handlerParsedI
return nil, nil
}, routeCacheDuration)
if value != nil {
item := value.(*handlerCacheItem)
item := value.Val().(*handlerCacheItem)
return item.parsedItems, item.hasHook, item.hasServe
}
return

View File

@ -77,15 +77,10 @@ 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.
func Get(key interface{}) (interface{}, error) {
func Get(key interface{}) (*gvar.Var, error) {
return defaultCache.Get(key)
}
// GetVar retrieves and returns the value of `key` as gvar.Var.
func GetVar(key interface{}) (*gvar.Var, error) {
return defaultCache.GetVar(key)
}
// GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and
// returns `value` if `key` does not exist in the cache. The key-value pair expires
// after `duration`.
@ -93,7 +88,7 @@ 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.
func GetOrSet(key interface{}, value interface{}, duration time.Duration) (interface{}, error) {
func GetOrSet(key interface{}, value interface{}, duration time.Duration) (*gvar.Var, error) {
return defaultCache.GetOrSet(key, value, duration)
}
@ -104,7 +99,7 @@ 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.
func GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
func GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, error) {
return defaultCache.GetOrSetFunc(key, f, duration)
}
@ -118,7 +113,7 @@ 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.
func GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
func GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, error) {
return defaultCache.GetOrSetFuncLock(key, f, duration)
}
@ -138,7 +133,7 @@ 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.
func Remove(keys ...interface{}) (value interface{}, err error) {
func Remove(keys ...interface{}) (value *gvar.Var, err error) {
return defaultCache.Remove(keys...)
}
@ -152,7 +147,7 @@ func Removes(keys []interface{}) error {
//
// It deletes the `key` if given `value` is nil.
// It does nothing if `key` does not exist in the cache.
func Update(key interface{}, value interface{}) (oldValue interface{}, exist bool, err error) {
func Update(key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
return defaultCache.Update(key, value)
}

View File

@ -8,6 +8,7 @@ package gcache
import (
"context"
"github.com/gogf/gf/container/gvar"
"time"
)
@ -60,7 +61,7 @@ type Adapter interface {
// 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.
Get(ctx context.Context, key interface{}) (interface{}, error)
Get(ctx context.Context, key interface{}) (*gvar.Var, error)
// GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and
// returns `value` if `key` does not exist in the cache. The key-value pair expires
@ -69,7 +70,7 @@ type Adapter interface {
// 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.
GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result interface{}, err error)
GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error)
// GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of
// function `f` and returns its result if `key` does not exist in the cache. The key-value
@ -78,7 +79,7 @@ type Adapter interface {
// 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.
GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result interface{}, err error)
GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error)
// GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of
// function `f` and returns its result if `key` does not exist in the cache. The key-value
@ -90,35 +91,11 @@ type Adapter interface {
//
// Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within
// writing mutex lock for concurrent safety purpose.
GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result interface{}, err error)
GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error)
// Contains checks and returns true if `key` exists in the cache, or else returns false.
Contains(ctx context.Context, key interface{}) (bool, error)
// GetExpire retrieves and returns the expiration of `key` in the cache.
//
// Note that,
// It returns 0 if the `key` does not expire.
// It returns -1 if the `key` does not exist in the cache.
GetExpire(ctx context.Context, 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.
Remove(ctx context.Context, keys ...interface{}) (lastValue interface{}, err error)
// Update updates the value of `key` without changing its expiration and returns the old value.
// The returned value `exist` is false if the `key` does not exist in the cache.
//
// It deletes the `key` if given `value` is nil.
// It does nothing if `key` does not exist in the cache.
Update(ctx context.Context, key interface{}, value interface{}) (oldValue interface{}, exist bool, err error)
// 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.
// It deletes the `key` if `duration` < 0.
UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)
// Size returns the number of items in the cache.
Size(ctx context.Context) (size int, err error)
@ -133,6 +110,30 @@ type Adapter interface {
// Values returns all values in the cache as slice.
Values(ctx context.Context) (values []interface{}, err error)
// Update updates the value of `key` without changing its expiration and returns the old value.
// The returned value `exist` is false if the `key` does not exist in the cache.
//
// It deletes the `key` if given `value` is nil.
// It does nothing if `key` does not exist in the cache.
Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error)
// 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.
// It deletes the `key` if `duration` < 0.
UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)
// GetExpire retrieves and returns the expiration of `key` in the cache.
//
// Note that,
// It returns 0 if the `key` does not expire.
// It returns -1 if the `key` does not exist in the cache.
GetExpire(ctx context.Context, 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.
Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error)
// Clear clears all data of the cache.
// Note that this function is sensitive and should be carefully used.
Clear(ctx context.Context) error

View File

@ -8,6 +8,7 @@ package gcache
import (
"context"
"github.com/gogf/gf/container/gvar"
"math"
"time"
@ -178,14 +179,14 @@ func (c *AdapterMemory) SetIfNotExistFuncLock(ctx context.Context, key interface
// 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.
func (c *AdapterMemory) Get(ctx context.Context, key interface{}) (interface{}, error) {
func (c *AdapterMemory) Get(ctx context.Context, key interface{}) (*gvar.Var, error) {
item, ok := c.data.Get(key)
if ok && !item.IsExpired() {
// Adding to LRU history if LRU feature is enabled.
if c.cap > 0 {
c.lruGetList.PushBack(key)
}
return item.v, nil
return gvar.New(item.v), nil
}
return nil, nil
}
@ -197,7 +198,7 @@ func (c *AdapterMemory) Get(ctx context.Context, key interface{}) (interface{},
// 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.
func (c *AdapterMemory) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (interface{}, error) {
func (c *AdapterMemory) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (*gvar.Var, error) {
v, err := c.Get(ctx, key)
if err != nil {
return nil, err
@ -216,7 +217,7 @@ func (c *AdapterMemory) GetOrSet(ctx context.Context, key interface{}, value int
// 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.
func (c *AdapterMemory) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
func (c *AdapterMemory) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, error) {
v, err := c.Get(ctx, key)
if err != nil {
return nil, err
@ -245,7 +246,7 @@ func (c *AdapterMemory) GetOrSetFunc(ctx context.Context, key interface{}, f fun
//
// Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within
// writing mutex lock for concurrent safety purpose.
func (c *AdapterMemory) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
func (c *AdapterMemory) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, error) {
v, err := c.Get(ctx, key)
if err != nil {
return nil, err
@ -280,11 +281,11 @@ func (c *AdapterMemory) GetExpire(ctx context.Context, key interface{}) (time.Du
// 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.
func (c *AdapterMemory) Remove(ctx context.Context, keys ...interface{}) (value interface{}, err error) {
func (c *AdapterMemory) Remove(ctx context.Context, keys ...interface{}) (*gvar.Var, error) {
var removedKeys []interface{}
removedKeys, value, err = c.data.Remove(keys...)
removedKeys, value, err := c.data.Remove(keys...)
if err != nil {
return
return nil, err
}
for _, key := range removedKeys {
c.eventList.PushBack(&adapterMemoryEvent{
@ -292,7 +293,7 @@ func (c *AdapterMemory) Remove(ctx context.Context, keys ...interface{}) (value
e: gtime.TimestampMilli() - 1000000,
})
}
return
return gvar.New(value), nil
}
// Update updates the value of `key` without changing its expiration and returns the old value.
@ -300,8 +301,9 @@ func (c *AdapterMemory) Remove(ctx context.Context, keys ...interface{}) (value
//
// It deletes the `key` if given `value` is nil.
// It does nothing if `key` does not exist in the cache.
func (c *AdapterMemory) Update(ctx context.Context, key interface{}, value interface{}) (oldValue interface{}, exist bool, err error) {
return c.data.Update(key, value)
func (c *AdapterMemory) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
v, exist, err := c.data.Update(key, value)
return gvar.New(v), exist, err
}
// UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
@ -367,11 +369,11 @@ func (c *AdapterMemory) Close(ctx context.Context) error {
//
// It doubly checks the `key` whether exists in the cache using mutex writing lock
// before setting it to the cache.
func (c *AdapterMemory) doSetWithLockCheck(key interface{}, value interface{}, duration time.Duration) (result interface{}, err error) {
func (c *AdapterMemory) doSetWithLockCheck(key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) {
expireTimestamp := c.getInternalExpire(duration)
result, err = c.data.SetWithLock(key, value, expireTimestamp)
v, err := c.data.SetWithLock(key, value, expireTimestamp)
c.eventList.PushBack(&adapterMemoryEvent{k: key, e: expireTimestamp})
return
return gvar.New(v), err
}
// getInternalExpire converts and returns the expiration time with given expired duration in milliseconds.

View File

@ -8,7 +8,6 @@ package gcache
import (
"context"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/os/gtimer"
"github.com/gogf/gf/util/gconv"
"time"
@ -63,12 +62,6 @@ func (c *Cache) SetAdapter(adapter Adapter) {
c.adapter = adapter
}
// GetVar retrieves and returns the value of `key` as gvar.Var.
func (c *Cache) GetVar(key interface{}) (*gvar.Var, error) {
v, err := c.Get(key)
return gvar.New(v), err
}
// Removes deletes `keys` in the cache.
func (c *Cache) Removes(keys []interface{}) error {
_, err := c.Remove(keys...)

View File

@ -7,6 +7,7 @@
package gcache
import (
"github.com/gogf/gf/container/gvar"
"time"
)
@ -63,7 +64,7 @@ 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.
func (c *Cache) Get(key interface{}) (interface{}, error) {
func (c *Cache) Get(key interface{}) (*gvar.Var, error) {
return c.adapter.Get(c.getCtx(), key)
}
@ -74,7 +75,7 @@ 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.
func (c *Cache) GetOrSet(key interface{}, value interface{}, duration time.Duration) (interface{}, error) {
func (c *Cache) GetOrSet(key interface{}, value interface{}, duration time.Duration) (*gvar.Var, error) {
return c.adapter.GetOrSet(c.getCtx(), key, value, duration)
}
@ -85,7 +86,7 @@ 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.
func (c *Cache) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
func (c *Cache) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, error) {
return c.adapter.GetOrSetFunc(c.getCtx(), key, f, duration)
}
@ -99,7 +100,7 @@ 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.
func (c *Cache) GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
func (c *Cache) GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, error) {
return c.adapter.GetOrSetFuncLock(c.getCtx(), key, f, duration)
}
@ -119,7 +120,7 @@ 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.
func (c *Cache) Remove(keys ...interface{}) (value interface{}, err error) {
func (c *Cache) Remove(keys ...interface{}) (value *gvar.Var, err error) {
return c.adapter.Remove(c.getCtx(), keys...)
}
@ -128,7 +129,7 @@ func (c *Cache) Remove(keys ...interface{}) (value interface{}, err error) {
//
// It deletes the `key` if given `value` is nil.
// It does nothing if `key` does not exist in the cache.
func (c *Cache) Update(key interface{}, value interface{}) (oldValue interface{}, exist bool, err error) {
func (c *Cache) Update(key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
return c.adapter.Update(c.getCtx(), key, value)
}

View File

@ -45,26 +45,6 @@ func TestCache_Set(t *testing.T) {
})
}
func TestCache_GetVar(t *testing.T) {
c := gcache.New()
defer c.Close()
gtest.C(t, func(t *gtest.T) {
t.Assert(c.Set(1, 11, 0), nil)
v, _ := c.Get(1)
t.Assert(v, 11)
b, _ := c.Contains(1)
t.Assert(b, true)
})
gtest.C(t, func(t *gtest.T) {
v, _ := c.GetVar(1)
t.Assert(v.Int(), 11)
v, _ = c.GetVar(2)
t.Assert(v.Int(), 0)
t.Assert(v.IsNil(), true)
t.Assert(v.IsEmpty(), true)
})
}
func TestCache_Set_Expire(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
cache := gcache.New()
@ -103,7 +83,7 @@ func TestCache_Update(t *testing.T) {
t.Assert(exist, true)
expire2, _ := gcache.GetExpire(key)
v, _ := gcache.GetVar(key)
v, _ := gcache.Get(key)
t.Assert(v, 12)
t.Assert(math.Ceil(expire1.Seconds()), math.Ceil(expire2.Seconds()))
})
@ -119,7 +99,7 @@ func TestCache_Update(t *testing.T) {
expire1, _ := cache.GetExpire(1)
expire2, _ := cache.GetExpire(1)
v, _ := cache.GetVar(1)
v, _ := cache.Get(1)
t.Assert(v, 12)
t.Assert(math.Ceil(expire1.Seconds()), math.Ceil(expire2.Seconds()))
})

View File

@ -55,7 +55,7 @@ func GetBytesWithCache(path string, duration ...time.Duration) []byte {
return b, nil
}, expire)
if r != nil {
return r.([]byte)
return r.Bytes()
}
return nil
}

View File

@ -44,7 +44,7 @@ func (s *Session) init() {
if s.id != "" {
// Retrieve memory session data from manager.
if r, _ := s.manager.sessionData.Get(s.id); r != nil {
s.data = r.(*gmap.StrAnyMap)
s.data = r.Val().(*gmap.StrAnyMap)
intlog.Print(s.ctx, "session init data:", s.data)
}
// Retrieve stored session data from storage.