From 0a3cd1d2ab2550df261ff16db5eb5d4e32b5cde7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E6=B0=B4=E4=B8=8D=E5=8A=A0=E7=B3=96?= <641008175@qq.com> Date: Wed, 12 Aug 2020 09:57:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=96=B9=E6=B3=95=E4=BB=A3?= =?UTF-8?q?=E7=A0=81.=E4=BD=BF=E7=94=A8Benchmark=E6=B5=8B=E8=AF=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- os/gcache/gcache.go | 4 +-- os/gcache/gcache_mem_cache.go | 41 ++++++++++++++++++++----------- os/gcache/gcache_z_unit_1_test.go | 35 +++++++++++++++----------- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/os/gcache/gcache.go b/os/gcache/gcache.go index a2c11a330..cdcf00515 100644 --- a/os/gcache/gcache.go +++ b/os/gcache/gcache.go @@ -124,6 +124,6 @@ func GetExpire(key interface{}) (int64, bool) { } // SetExpire set cache expired after . -func SetExpire(key interface{}, duration time.Duration) { - cache.SetExpire(key, duration) +func SetExpire(key interface{}, duration time.Duration) bool { + return cache.SetExpire(key, duration) } diff --git a/os/gcache/gcache_mem_cache.go b/os/gcache/gcache_mem_cache.go index 55ba72de1..ead470d91 100644 --- a/os/gcache/gcache_mem_cache.go +++ b/os/gcache/gcache_mem_cache.go @@ -115,34 +115,45 @@ func (c *memCache) Set(key interface{}, value interface{}, duration time.Duratio // SetVar retrieves and set the value of . func (c *memCache) SetVar(key interface{}, value interface{}) bool { - if c.Contains(key) { - c.dataMu.Lock() + c.dataMu.Lock() + defer c.dataMu.Unlock() + if item, ok := c.data[key]; ok { c.data[key] = memCacheItem{ v: value, - e: c.data[key].e, + e: item.e, } - c.dataMu.Unlock() return true } return false } // SetExpire retrieves and set the value of . -func (c *memCache) SetExpire(key interface{}, duration time.Duration) { - value := c.GetVar(key) - c.Remove(key) - c.Set(key, value, duration) +func (c *memCache) SetExpire(key interface{}, duration time.Duration) bool { + c.dataMu.Lock() + defer c.dataMu.Unlock() + if item, ok := c.data[key]; ok { + c.eventList.PushBack(&memCacheEvent{ + k: key, + e: gtime.TimestampMilli() - 1000, + }) + newExpireTime := c.getInternalExpire(duration) + c.data[key] = memCacheItem{ + v: item.v, + e: newExpireTime, + } + c.eventList.PushBack(&memCacheEvent{ + k: key, + e: newExpireTime, + }) + return true + } + return false } func (c *memCache) GetExpire(key interface{}) (int64, bool) { c.dataMu.RLock() - item, ok := c.data[key] - c.dataMu.RUnlock() - if ok { - // Adding to LRU history if LRU feature is enabled. - if c.cap > 0 { - c.lruGetList.PushBack(key) - } + defer c.dataMu.RUnlock() + if item, ok := c.data[key]; ok { return item.e, true } return 0, false diff --git a/os/gcache/gcache_z_unit_1_test.go b/os/gcache/gcache_z_unit_1_test.go index b7472da9a..8b9296d6d 100644 --- a/os/gcache/gcache_z_unit_1_test.go +++ b/os/gcache/gcache_z_unit_1_test.go @@ -9,6 +9,9 @@ package gcache_test import ( + "fmt" + "github.com/gogf/gf/os/glog" + "github.com/gogf/gf/util/grand" "testing" "time" @@ -73,27 +76,31 @@ func TestCache_Set_Expire(t *testing.T) { }) } -func TestCache_Expire_SetVar(t *testing.T) { +func TestCache_SetVar(t *testing.T) { gtest.C(t, func(t *gtest.T) { cache := gcache.New() cache.Set(1, 11, 3*time.Second) - expireBefore, _ := cache.GetExpire(1) - t.Assert(cache.Get(1), 11) cache.SetVar(1, 12) - t.Assert(cache.Get(1), 12) - time.Sleep(1 * time.Second) - cache.SetExpire(1, 5*time.Second) - expireAfter, okAfter := cache.GetExpire(1) - if okAfter { - t.Assert(expireAfter-expireBefore, 3000) - } - time.Sleep(4 * time.Second) - t.Assert(cache.Get(1), 12) - time.Sleep(2 * time.Second) - t.Assert(cache.Get(1), nil) + t.Assert(cache.GetVar(1), 12) }) } +func BenchmarkMemCache_GetSetExpire(b *testing.B) { + cache := gcache.New() + cache.Set(1, 11, 3*time.Second) + if expire, ok := cache.GetExpire(1); ok { + glog.Println(expire) + } + for i := 0; i < b.N; i++ { + r := time.Duration(grand.N(5, 10)) + cache.SetExpire(1, r*time.Second) + //cache.SetExpire(1, 7*time.Second) + if _, ok := cache.GetExpire(1); !ok { + panic(fmt.Sprintf("[ERROR] %s", "GetExpire error")) + } + } +} + func TestCache_Keys_Values(t *testing.T) { gtest.C(t, func(t *gtest.T) { cache := gcache.New()