diff --git a/container/gvar/gvar_map.go b/container/gvar/gvar_map.go index 8b4282b0a..311e45db3 100644 --- a/container/gvar/gvar_map.go +++ b/container/gvar/gvar_map.go @@ -60,26 +60,26 @@ func (v *Var) Maps(tags ...string) []map[string]interface{} { return gconv.Maps(v.Val(), tags...) } -// MapToMap converts map type variable to another map type variable . +// MapToMap converts any map type variable to another map type variable . // See gconv.MapToMap. func (v *Var) MapToMap(pointer interface{}) (err error) { return gconv.MapToMap(v.Val(), pointer) } -// MapToMapDeep converts map type variable to another map type variable +// MapToMapDeep converts any map type variable to another map type variable // recursively. // See gconv.MapToMapDeep. func (v *Var) MapToMapDeep(pointer interface{}) (err error) { return gconv.MapToMapDeep(v.Val(), pointer) } -// MapToMaps converts map type variable to another map type variable . +// MapToMaps converts any map type variable to another map type variable . // See gconv.MapToMaps. func (v *Var) MapToMaps(pointer interface{}, mapping ...map[string]string) (err error) { return gconv.MapToMaps(v.Val(), pointer, mapping...) } -// MapToMapsDeep converts map type variable to another map type variable +// MapToMapsDeep converts any map type variable to another map type variable // recursively. // See gconv.MapToMapsDeep. func (v *Var) MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error) { diff --git a/internal/empty/empty.go b/internal/empty/empty.go index 52a4bf86f..4225085fe 100644 --- a/internal/empty/empty.go +++ b/internal/empty/empty.go @@ -4,7 +4,7 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -// Package empty provides checks for empty variables. +// Package empty provides functions for checking empty variables. package empty import ( diff --git a/internal/intlog/intlog.go b/internal/intlog/intlog.go index c91ca11ec..de62a0978 100644 --- a/internal/intlog/intlog.go +++ b/internal/intlog/intlog.go @@ -4,7 +4,7 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -// Package intlog provides internal logging for GF development usage only. +// Package intlog provides internal logging for GoFrame development usage only. package intlog import ( diff --git a/internal/mutex/mutex.go b/internal/mutex/mutex.go index 98a9d30a3..50eb5b210 100644 --- a/internal/mutex/mutex.go +++ b/internal/mutex/mutex.go @@ -9,7 +9,7 @@ package mutex import "sync" -// Mutex is a sync.Mutex with a switch of concurrent safe feature. +// Mutex is a sync.Mutex with a switch for concurrent safe feature. type Mutex struct { sync.Mutex safe bool diff --git a/internal/rwmutex/rwmutex.go b/internal/rwmutex/rwmutex.go index d1fed095f..6bd21e49e 100644 --- a/internal/rwmutex/rwmutex.go +++ b/internal/rwmutex/rwmutex.go @@ -9,7 +9,7 @@ package rwmutex import "sync" -// RWMutex is a sync.RWMutex with a switch of concurrent safe feature. +// RWMutex is a sync.RWMutex with a switch for concurrent safe feature. // If its attribute *sync.RWMutex is not nil, it means it's in concurrent safety usage. // Its attribute *sync.RWMutex is nil in default, which makes this struct mush lightweight. type RWMutex struct { diff --git a/os/gcache/gcache.go b/os/gcache/gcache.go index 31b010686..90b9c504d 100644 --- a/os/gcache/gcache.go +++ b/os/gcache/gcache.go @@ -7,7 +7,10 @@ // Package gcache provides high performance and concurrent-safe in-memory cache for process. package gcache -import "time" +import ( + "github.com/gogf/gf/container/gvar" + "time" +) // Default cache object. var cache = New() @@ -37,6 +40,11 @@ func Get(key interface{}) interface{} { return cache.Get(key) } +// GetVar retrieves and returns the value of as *gvar.Var. +func GetVar(key interface{}) *gvar.Var { + return cache.GetVar(key) +} + // GetOrSet returns the value of , // or sets - pair and returns if does not exist in the cache. // The key-value pair expires after . @@ -67,12 +75,14 @@ func Contains(key interface{}) bool { return cache.Contains(key) } -// Remove deletes the in the cache, and returns its value. -func Remove(key interface{}) interface{} { - return cache.Remove(key) +// Remove deletes the one or more keys from cache, and returns its value. +// If multiple keys are given, it returns the value of the deleted last item. +func Remove(keys ...interface{}) (value interface{}) { + return cache.Remove(keys...) } // Removes deletes in the cache. +// Deprecated, use Remove instead. func Removes(keys []interface{}) { cache.Removes(keys) } diff --git a/os/gcache/gcache_mem_cache.go b/os/gcache/gcache_mem_cache.go index b1c39cdf0..4afb5c321 100644 --- a/os/gcache/gcache_mem_cache.go +++ b/os/gcache/gcache_mem_cache.go @@ -7,6 +7,7 @@ package gcache import ( + "github.com/gogf/gf/container/gvar" "math" "sync" "time" @@ -222,6 +223,11 @@ func (c *memCache) Get(key interface{}) interface{} { return nil } +// GetVar retrieves and returns the value of as *gvar.Var. +func (c *memCache) GetVar(key interface{}) *gvar.Var { + return gvar.New(c.Get(key)) +} + // GetOrSet returns the value of , or sets - pair and returns if // does not exist in the cache. The key-value pair expires after . It does not expire // if == 0. @@ -268,29 +274,29 @@ func (c *memCache) Contains(key interface{}) bool { return c.Get(key) != nil } -// Remove deletes the in the cache, and returns its value. -func (c *memCache) Remove(key interface{}) (value interface{}) { - c.dataMu.RLock() - item, ok := c.data[key] - c.dataMu.RUnlock() - if ok { - value = item.v - c.dataMu.Lock() - delete(c.data, key) - c.dataMu.Unlock() - c.eventList.PushBack(&memCacheEvent{ - k: key, - e: gtime.TimestampMilli() - 1000, - }) +// Remove deletes the one or more keys from cache, and returns its value. +// If multiple keys are given, it returns the value of the deleted last item. +func (c *memCache) Remove(keys ...interface{}) (value interface{}) { + c.dataMu.Lock() + defer c.dataMu.Unlock() + for _, key := range keys { + item, ok := c.data[key] + if ok { + value = item.v + delete(c.data, key) + c.eventList.PushBack(&memCacheEvent{ + k: key, + e: gtime.TimestampMilli() - 1000, + }) + } } return } // Removes deletes in the cache. +// Deprecated, use Remove instead. func (c *memCache) Removes(keys []interface{}) { - for _, key := range keys { - c.Remove(key) - } + c.Remove(keys...) } // Data returns a copy of all key-value pairs in the cache as map type. diff --git a/os/gcache/gcache_z_unit_1_test.go b/os/gcache/gcache_z_unit_1_test.go index 64267bb72..7eb74b642 100644 --- a/os/gcache/gcache_z_unit_1_test.go +++ b/os/gcache/gcache_z_unit_1_test.go @@ -19,23 +19,36 @@ import ( "github.com/gogf/gf/test/gtest" ) -//clear 用于清除全局缓存,因gcache api 暂未暴露 Clear 方法 -//暂定所有测试用例key的集合为1,2,3,避免不同测试用例间因全局cache共享带来的问题,每个测试用例在测试gcache.XXX之前,先调用clear() -func clear() { - gcache.Removes(g.Slice{1, 2, 3}) +func TestCache_GCache_Set(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + gcache.Set(1, 11, 0) + defer gcache.Removes(g.Slice{1, 2, 3}) + t.Assert(gcache.Get(1), 11) + t.Assert(gcache.Contains(1), true) + }) } func TestCache_Set(t *testing.T) { gtest.C(t, func(t *gtest.T) { - cache := gcache.New() - cache.Set(1, 11, 0) - t.Assert(cache.Get(1), 11) - t.Assert(cache.Contains(1), true) + c := gcache.New() + defer c.Close() + c.Set(1, 11, 0) + t.Assert(c.Get(1), 11) + t.Assert(c.Contains(1), true) + }) +} - clear() - gcache.Set(1, 11, 0) - t.Assert(gcache.Get(1), 11) - t.Assert(gcache.Contains(1), true) +func TestCache_GetVar(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + c := gcache.New() + defer c.Close() + c.Set(1, 11, 0) + t.Assert(c.Get(1), 11) + t.Assert(c.Contains(1), true) + t.Assert(c.GetVar(1).Int(), 11) + t.Assert(c.GetVar(2).Int(), 0) + t.Assert(c.GetVar(2).IsNil(), true) + t.Assert(c.GetVar(2).IsEmpty(), true) }) } @@ -108,7 +121,7 @@ func TestCache_SetIfNotExist(t *testing.T) { cache.SetIfNotExist(2, 22, 0) t.Assert(cache.Get(2), 22) - clear() + gcache.Removes(g.Slice{1, 2, 3}) gcache.SetIfNotExist(1, 11, 0) t.Assert(gcache.Get(1), 11) gcache.SetIfNotExist(1, 22, 0) @@ -122,7 +135,7 @@ func TestCache_Sets(t *testing.T) { cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) t.Assert(cache.Get(1), 11) - clear() + gcache.Removes(g.Slice{1, 2, 3}) gcache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) t.Assert(gcache.Get(1), 11) }) @@ -136,7 +149,7 @@ func TestCache_GetOrSet(t *testing.T) { cache.GetOrSet(1, 111, 0) t.Assert(cache.Get(1), 11) - clear() + gcache.Removes(g.Slice{1, 2, 3}) gcache.GetOrSet(1, 11, 0) t.Assert(gcache.Get(1), 11) gcache.GetOrSet(1, 111, 0) @@ -156,7 +169,7 @@ func TestCache_GetOrSetFunc(t *testing.T) { }, 0) t.Assert(cache.Get(1), 11) - clear() + gcache.Removes(g.Slice{1, 2, 3}) gcache.GetOrSetFunc(1, func() interface{} { return 11 }, 0) @@ -180,7 +193,7 @@ func TestCache_GetOrSetFuncLock(t *testing.T) { }, 0) t.Assert(cache.Get(1), 11) - clear() + gcache.Removes(g.Slice{1, 2, 3}) gcache.GetOrSetFuncLock(1, func() interface{} { return 11 }, 0) @@ -256,7 +269,7 @@ func TestCache_Basic(t *testing.T) { t.Assert(cache.Size(), 0) } - clear() + gcache.Removes(g.Slice{1, 2, 3}) { gcache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) t.Assert(gcache.Contains(1), true) diff --git a/util/gconv/gconv_map.go b/util/gconv/gconv_map.go index 686165f60..7e76515ef 100644 --- a/util/gconv/gconv_map.go +++ b/util/gconv/gconv_map.go @@ -292,21 +292,21 @@ func MapStrStrDeep(value interface{}, tags ...string) map[string]string { return nil } -// MapToMap converts map type variable to another map type variable using -// reflect. +// MapToMap converts any map type variable to another map type variable +// using reflect. // See doMapToMap. func MapToMap(params interface{}, pointer interface{}, mapping ...map[string]string) error { return doMapToMap(params, pointer, false, mapping...) } -// MapToMapDeep converts map type variable to another map type variable using -// reflect recursively. +// MapToMapDeep converts any map type variable to another map type variable +// using reflect recursively. // See doMapToMap. func MapToMapDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error { return doMapToMap(params, pointer, true, mapping...) } -// doMapToMap converts map type variable to another map type variable . +// doMapToMap converts any map type variable to another map type variable . // // The parameter can be any type of map, like: // map[string]string, map[string]struct, , map[string]*struct, etc. @@ -400,20 +400,20 @@ func doMapToMap(params interface{}, pointer interface{}, deep bool, mapping ...m return nil } -// MapToMaps converts map type variable to another map type variable . +// MapToMaps converts any map type variable to another map type variable . // See doMapToMaps. func MapToMaps(params interface{}, pointer interface{}, mapping ...map[string]string) error { return doMapToMaps(params, pointer, false, mapping...) } -// MapToMapsDeep converts map type variable to another map type variable +// MapToMapsDeep converts any map type variable to another map type variable // recursively. // See doMapToMaps. func MapToMapsDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error { return doMapToMaps(params, pointer, true, mapping...) } -// doMapToMaps converts map type variable to another map type variable . +// doMapToMaps converts any map type variable to another map type variable . // // The parameter can be any type of map, of which the item type is slice map, like: // map[int][]map, map[string][]map.