From 5a017984817fd1292232ebc455765c5b919992cf Mon Sep 17 00:00:00 2001 From: Gin Date: Wed, 3 Jan 2024 20:03:19 +0800 Subject: [PATCH] fix: memory leak when gcache.NewAdapterMemory with lru (#3241) --- os/gcache/gcache_adapter_memory.go | 3 +++ os/gcache/gcache_cache.go | 5 ----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/os/gcache/gcache_adapter_memory.go b/os/gcache/gcache_adapter_memory.go index 707b04bd90d..9502b549137 100644 --- a/os/gcache/gcache_adapter_memory.go +++ b/os/gcache/gcache_adapter_memory.go @@ -67,6 +67,9 @@ func NewAdapterMemory(lruCap ...int) Adapter { c.cap = lruCap[0] c.lru = newMemCacheLru(c) } + // Here may be a "timer leak" if adapter is manually changed from memory adapter. + // Do not worry about this, as adapter is less changed, and it does nothing if it's not used. + gtimer.AddSingleton(context.Background(), time.Second, c.syncEventAndClearExpired) return c } diff --git a/os/gcache/gcache_cache.go b/os/gcache/gcache_cache.go index 9a039457c66..bd3d2ac4b3e 100644 --- a/os/gcache/gcache_cache.go +++ b/os/gcache/gcache_cache.go @@ -8,9 +8,7 @@ package gcache import ( "context" - "time" - "github.com/gogf/gf/v2/os/gtimer" "github.com/gogf/gf/v2/util/gconv" ) @@ -29,9 +27,6 @@ func New(lruCap ...int) *Cache { c := &Cache{ localAdapter: memAdapter, } - // Here may be a "timer leak" if adapter is manually changed from memory adapter. - // Do not worry about this, as adapter is less changed, and it does nothing if it's not used. - gtimer.AddSingleton(context.Background(), time.Second, memAdapter.(*AdapterMemory).syncEventAndClearExpired) return c }