From 8a84ca16d1c533ac03145d3db5a87819aec9f554 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: Fri, 7 Aug 2020 13:24:41 +0800 Subject: [PATCH 1/5] add func gcache.SetVar/gcache.SetExpire/gcacge.GetExpire. add test TestCache_Expire_SetVar. --- os/gcache/gcache.go | 16 ++++++++++++++ os/gcache/gcache_mem_cache.go | 35 +++++++++++++++++++++++++++++++ os/gcache/gcache_z_unit_1_test.go | 23 ++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/os/gcache/gcache.go b/os/gcache/gcache.go index dcd307970..a2c11a330 100644 --- a/os/gcache/gcache.go +++ b/os/gcache/gcache.go @@ -21,6 +21,12 @@ func Set(key interface{}, value interface{}, duration time.Duration) { cache.Set(key, value, duration) } +// SetVar edit cache with - , but will not change expire. +// If key is not exist return false. Nothing will change. +func SetVar(key interface{}, value interface{}) bool { + return cache.SetVar(key, value) +} + // SetIfNotExist sets cache with - pair if does not exist in the cache, // which is expired after . It does not expire if == 0. func SetIfNotExist(key interface{}, value interface{}, duration time.Duration) bool { @@ -111,3 +117,13 @@ func Values() []interface{} { func Size() int { return cache.Size() } + +// GetExpire returns the expire time with given expired duration in milliseconds. +func GetExpire(key interface{}) (int64, bool) { + return cache.GetExpire(key) +} + +// SetExpire set cache expired after . +func SetExpire(key interface{}, duration time.Duration) { + cache.SetExpire(key, duration) +} diff --git a/os/gcache/gcache_mem_cache.go b/os/gcache/gcache_mem_cache.go index 6759ed8a2..55ba72de1 100644 --- a/os/gcache/gcache_mem_cache.go +++ b/os/gcache/gcache_mem_cache.go @@ -113,6 +113,41 @@ 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.data[key] = memCacheItem{ + v: value, + e: c.data[key].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) 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) + } + return item.e, true + } + return 0, false +} + // doSetWithLockCheck sets cache with - pair if does not exist in the // cache, which is expired after . // diff --git a/os/gcache/gcache_z_unit_1_test.go b/os/gcache/gcache_z_unit_1_test.go index 7eb74b642..a9683a1ff 100644 --- a/os/gcache/gcache_z_unit_1_test.go +++ b/os/gcache/gcache_z_unit_1_test.go @@ -73,6 +73,29 @@ func TestCache_Set_Expire(t *testing.T) { }) } +func TestCache_Expire_SetVar(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + gcache.Set(1, 11, 3*time.Second) + expireBefore, okBefore := gcache.GetExpire(1) + if okBefore { + t.AssertGT(expireBefore, 0) + } + t.Assert(gcache.Get(1), 11) + gcache.SetVar(1, 12) + t.Assert(gcache.Get(1), 12) + time.Sleep(1 * time.Second) + gcache.SetExpire(1, 5*time.Second) + expireAfter, okAfter := gcache.GetExpire(1) + if okAfter { + t.Assert(expireAfter-expireBefore, 3000) + } + time.Sleep(4 * time.Second) + t.Assert(gcache.Get(1), 12) + time.Sleep(2 * time.Second) + t.Assert(gcache.Get(1), nil) + }) +} + func TestCache_Keys_Values(t *testing.T) { gtest.C(t, func(t *gtest.T) { cache := gcache.New() From 919eaf1e9a1606febd061ca249c22af84536557f 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: Fri, 7 Aug 2020 14:05:21 +0800 Subject: [PATCH 2/5] adjust TestCache_Expire_SetVar Test Code. --- os/gcache/gcache_z_unit_1_test.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/os/gcache/gcache_z_unit_1_test.go b/os/gcache/gcache_z_unit_1_test.go index a9683a1ff..dd4a384e3 100644 --- a/os/gcache/gcache_z_unit_1_test.go +++ b/os/gcache/gcache_z_unit_1_test.go @@ -75,24 +75,25 @@ func TestCache_Set_Expire(t *testing.T) { func TestCache_Expire_SetVar(t *testing.T) { gtest.C(t, func(t *gtest.T) { - gcache.Set(1, 11, 3*time.Second) - expireBefore, okBefore := gcache.GetExpire(1) + cache := gcache.New() + cache.Set(1, 11, 3*time.Second) + expireBefore, okBefore := cache.GetExpire(1) if okBefore { t.AssertGT(expireBefore, 0) } - t.Assert(gcache.Get(1), 11) - gcache.SetVar(1, 12) - t.Assert(gcache.Get(1), 12) + t.Assert(cache.Get(1), 11) + cache.SetVar(1, 12) + t.Assert(cache.Get(1), 12) time.Sleep(1 * time.Second) - gcache.SetExpire(1, 5*time.Second) - expireAfter, okAfter := gcache.GetExpire(1) + 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(gcache.Get(1), 12) + t.Assert(cache.Get(1), 12) time.Sleep(2 * time.Second) - t.Assert(gcache.Get(1), nil) + t.Assert(cache.Get(1), nil) }) } From 4871f863461df024dbc694d4ecf685f292fd4303 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: Fri, 7 Aug 2020 16:26:26 +0800 Subject: [PATCH 3/5] adjust TestCache_Expire_SetVar Test Code. --- os/gcache/gcache_z_unit_1_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/os/gcache/gcache_z_unit_1_test.go b/os/gcache/gcache_z_unit_1_test.go index dd4a384e3..b7472da9a 100644 --- a/os/gcache/gcache_z_unit_1_test.go +++ b/os/gcache/gcache_z_unit_1_test.go @@ -77,10 +77,7 @@ func TestCache_Expire_SetVar(t *testing.T) { gtest.C(t, func(t *gtest.T) { cache := gcache.New() cache.Set(1, 11, 3*time.Second) - expireBefore, okBefore := cache.GetExpire(1) - if okBefore { - t.AssertGT(expireBefore, 0) - } + expireBefore, _ := cache.GetExpire(1) t.Assert(cache.Get(1), 11) cache.SetVar(1, 12) t.Assert(cache.Get(1), 12) 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 4/5] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81.=E4=BD=BF=E7=94=A8Benchmark=E6=B5=8B?= =?UTF-8?q?=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() From bcf45e3c5a7b342d3019abbb5e71201fed930440 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 10:32:40 +0800 Subject: [PATCH 5/5] =?UTF-8?q?CI-glog=E5=A4=B1=E8=B4=A5=E9=87=8D=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- os/gcache/gcache_z_unit_1_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/os/gcache/gcache_z_unit_1_test.go b/os/gcache/gcache_z_unit_1_test.go index 8b9296d6d..5a451c227 100644 --- a/os/gcache/gcache_z_unit_1_test.go +++ b/os/gcache/gcache_z_unit_1_test.go @@ -80,8 +80,11 @@ 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) cache.SetVar(1, 12) + expireAfter, _ := cache.GetExpire(1) t.Assert(cache.GetVar(1), 12) + t.Assert(expireBefore, expireAfter) }) }