mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
Merge pull request #860 from XWR940711/master
add func gcache.SetVar/gcache.SetExpire/gcacge.GetExpire.
This commit is contained in:
@ -21,6 +21,12 @@ func Set(key interface{}, value interface{}, duration time.Duration) {
|
||||
cache.Set(key, value, duration)
|
||||
}
|
||||
|
||||
// SetVar edit cache with <key>-<value> , 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 <key>-<value> pair if <key> does not exist in the cache,
|
||||
// which is expired after <duration>. It does not expire if <duration> == 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 <duration>.
|
||||
func SetExpire(key interface{}, duration time.Duration) bool {
|
||||
return cache.SetExpire(key, duration)
|
||||
}
|
||||
|
||||
@ -113,6 +113,52 @@ 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 {
|
||||
c.dataMu.Lock()
|
||||
defer c.dataMu.Unlock()
|
||||
if item, ok := c.data[key]; ok {
|
||||
c.data[key] = memCacheItem{
|
||||
v: value,
|
||||
e: item.e,
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// SetExpire retrieves and set the value of <key>.
|
||||
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()
|
||||
defer c.dataMu.RUnlock()
|
||||
if item, ok := c.data[key]; ok {
|
||||
return item.e, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// doSetWithLockCheck sets cache with <key>-<value> pair if <key> does not exist in the
|
||||
// cache, which is expired after <duration>.
|
||||
//
|
||||
|
||||
@ -9,6 +9,9 @@
|
||||
package gcache_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/util/grand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -73,6 +76,34 @@ func TestCache_Set_Expire(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)
|
||||
cache.SetVar(1, 12)
|
||||
expireAfter, _ := cache.GetExpire(1)
|
||||
t.Assert(cache.GetVar(1), 12)
|
||||
t.Assert(expireBefore, expireAfter)
|
||||
})
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user