diff --git a/g/os/gcache/gcache_mem_cache.go b/g/os/gcache/gcache_mem_cache.go index fcdee9716..edb648c10 100644 --- a/g/os/gcache/gcache_mem_cache.go +++ b/g/os/gcache/gcache_mem_cache.go @@ -7,14 +7,15 @@ package gcache import ( + "math" + "sync" + "github.com/gogf/gf/g/container/glist" "github.com/gogf/gf/g/container/gset" "github.com/gogf/gf/g/container/gtype" "github.com/gogf/gf/g/os/gtime" "github.com/gogf/gf/g/os/gtimer" "github.com/gogf/gf/g/util/gconv" - "math" - "sync" ) // Internal cache object. @@ -121,8 +122,8 @@ func (c *memCache) Set(key interface{}, value interface{}, expire int) { func (c *memCache) doSetWithLockCheck(key interface{}, value interface{}, expire int) interface{} { expireTimestamp := c.getInternalExpire(expire) c.dataMu.Lock() + defer c.dataMu.Unlock() if v, ok := c.data[key]; ok && !v.IsExpired() { - c.dataMu.Unlock() return v.v } if f, ok := value.(func() interface{}); ok { @@ -132,7 +133,6 @@ func (c *memCache) doSetWithLockCheck(key interface{}, value interface{}, expire return nil } c.data[key] = memCacheItem{v: value, e: expireTimestamp} - c.dataMu.Unlock() c.eventList.PushBack(&memCacheEvent{k: key, e: expireTimestamp}) return value } diff --git a/geg/other/test.go b/geg/other/test.go index 4a403748a..42a1b9963 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,8 +1,28 @@ package main -import "fmt" +import ( + "github.com/gogf/gf/g/os/glog" + + "github.com/gogf/gf/g/os/gcache" +) + +func localCache() { + result := gcache.GetOrSetFunc("test.key.1", func() interface{} { + return nil + }, 1000*60*2) + if result == nil { + glog.Error("未获取到值") + } else { + glog.Infofln("result is $v", result) + } +} + +func TestCache() { + for i := 0; i < 100; i++ { + localCache() + } +} func main() { - s := "123" - fmt.Println([]byte(s)) + TestCache() }