修正方法代码.使用Benchmark测试.

This commit is contained in:
糖水不加糖
2020-08-12 09:57:47 +08:00
parent 4871f86346
commit 0a3cd1d2ab
3 changed files with 49 additions and 31 deletions

View File

@ -124,6 +124,6 @@ func GetExpire(key interface{}) (int64, bool) {
}
// SetExpire set cache expired after <duration>.
func SetExpire(key interface{}, duration time.Duration) {
cache.SetExpire(key, duration)
func SetExpire(key interface{}, duration time.Duration) bool {
return cache.SetExpire(key, duration)
}

View File

@ -115,34 +115,45 @@ func (c *memCache) Set(key interface{}, value interface{}, duration time.Duratio
// SetVar retrieves and set the value of <key>.
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 <key>.
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

View File

@ -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()