mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
add function GetVar for package gcache
This commit is contained in:
@ -60,26 +60,26 @@ func (v *Var) Maps(tags ...string) []map[string]interface{} {
|
||||
return gconv.Maps(v.Val(), tags...)
|
||||
}
|
||||
|
||||
// MapToMap converts map type variable <params> to another map type variable <pointer>.
|
||||
// MapToMap converts any map type variable <params> to another map type variable <pointer>.
|
||||
// See gconv.MapToMap.
|
||||
func (v *Var) MapToMap(pointer interface{}) (err error) {
|
||||
return gconv.MapToMap(v.Val(), pointer)
|
||||
}
|
||||
|
||||
// MapToMapDeep converts map type variable <params> to another map type variable
|
||||
// MapToMapDeep converts any map type variable <params> to another map type variable
|
||||
// <pointer> recursively.
|
||||
// See gconv.MapToMapDeep.
|
||||
func (v *Var) MapToMapDeep(pointer interface{}) (err error) {
|
||||
return gconv.MapToMapDeep(v.Val(), pointer)
|
||||
}
|
||||
|
||||
// MapToMaps converts map type variable <params> to another map type variable <pointer>.
|
||||
// MapToMaps converts any map type variable <params> to another map type variable <pointer>.
|
||||
// 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 <params> to another map type variable
|
||||
// MapToMapsDeep converts any map type variable <params> to another map type variable
|
||||
// <pointer> recursively.
|
||||
// See gconv.MapToMapsDeep.
|
||||
func (v *Var) MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
|
||||
@ -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 (
|
||||
|
||||
@ -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 (
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 <key> as *gvar.Var.
|
||||
func GetVar(key interface{}) *gvar.Var {
|
||||
return cache.GetVar(key)
|
||||
}
|
||||
|
||||
// GetOrSet 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>.
|
||||
@ -67,12 +75,14 @@ func Contains(key interface{}) bool {
|
||||
return cache.Contains(key)
|
||||
}
|
||||
|
||||
// Remove deletes the <key> 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 <keys> in the cache.
|
||||
// Deprecated, use Remove instead.
|
||||
func Removes(keys []interface{}) {
|
||||
cache.Removes(keys)
|
||||
}
|
||||
|
||||
@ -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 <key> as *gvar.Var.
|
||||
func (c *memCache) GetVar(key interface{}) *gvar.Var {
|
||||
return gvar.New(c.Get(key))
|
||||
}
|
||||
|
||||
// GetOrSet 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>. It does not expire
|
||||
// if <duration> == 0.
|
||||
@ -268,29 +274,29 @@ func (c *memCache) Contains(key interface{}) bool {
|
||||
return c.Get(key) != nil
|
||||
}
|
||||
|
||||
// Remove deletes the <key> 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 <keys> 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.
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -292,21 +292,21 @@ func MapStrStrDeep(value interface{}, tags ...string) map[string]string {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MapToMap converts map type variable <params> to another map type variable <pointer> using
|
||||
// reflect.
|
||||
// MapToMap converts any map type variable <params> to another map type variable <pointer>
|
||||
// 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 <params> to another map type variable <pointer> using
|
||||
// reflect recursively.
|
||||
// MapToMapDeep converts any map type variable <params> to another map type variable <pointer>
|
||||
// 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 <params> to another map type variable <pointer>.
|
||||
// doMapToMap converts any map type variable <params> to another map type variable <pointer>.
|
||||
//
|
||||
// The parameter <params> 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 <params> to another map type variable <pointer>.
|
||||
// MapToMaps converts any map type variable <params> to another map type variable <pointer>.
|
||||
// See doMapToMaps.
|
||||
func MapToMaps(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapToMaps(params, pointer, false, mapping...)
|
||||
}
|
||||
|
||||
// MapToMapsDeep converts map type variable <params> to another map type variable
|
||||
// MapToMapsDeep converts any map type variable <params> to another map type variable
|
||||
// <pointer> 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 <params> to another map type variable <pointer>.
|
||||
// doMapToMaps converts any map type variable <params> to another map type variable <pointer>.
|
||||
//
|
||||
// The parameter <params> can be any type of map, of which the item type is slice map, like:
|
||||
// map[int][]map, map[string][]map.
|
||||
|
||||
Reference in New Issue
Block a user