From 8472415472c43a6d7eed8aacccf8dc97440fa3d9 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Mon, 4 Apr 2022 22:13:08 +0800 Subject: [PATCH] fix #1754 (#1757) --- core/collection/cache.go | 7 ++++++- core/collection/cache_test.go | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/core/collection/cache.go b/core/collection/cache.go index b7c887fb..35411055 100644 --- a/core/collection/cache.go +++ b/core/collection/cache.go @@ -98,13 +98,18 @@ func (c *Cache) Get(key string) (interface{}, bool) { // Set sets value into c with key. func (c *Cache) Set(key string, value interface{}) { + c.SetWithExpire(key, value, c.expire) +} + +// SetWithExpire sets value into c with key and expire with the given value. +func (c *Cache) SetWithExpire(key string, value interface{}, expire time.Duration) { c.lock.Lock() _, ok := c.data[key] c.data[key] = value c.lruCache.add(key) c.lock.Unlock() - expiry := c.unstableExpiry.AroundDuration(c.expire) + expiry := c.unstableExpiry.AroundDuration(expire) if ok { c.timingWheel.MoveTimer(key, expiry) } else { diff --git a/core/collection/cache_test.go b/core/collection/cache_test.go index bf30ee71..7fc0f465 100644 --- a/core/collection/cache_test.go +++ b/core/collection/cache_test.go @@ -18,7 +18,7 @@ func TestCacheSet(t *testing.T) { assert.Nil(t, err) cache.Set("first", "first element") - cache.Set("second", "second element") + cache.SetWithExpire("second", "second element", time.Second*3) value, ok := cache.Get("first") assert.True(t, ok)