From 4553f90a834ea32ff844303da3f6f18c9f091635 Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 25 Nov 2022 10:45:56 +0800 Subject: [PATCH] improve lru clearing for package gcache (#2327) --- os/gcache/gcache_adapter_memory.go | 15 +++++++++------ os/gcache/gcache_adapter_memory_lru.go | 2 -- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/os/gcache/gcache_adapter_memory.go b/os/gcache/gcache_adapter_memory.go index 2d0fb6929..707b04bd9 100644 --- a/os/gcache/gcache_adapter_memory.go +++ b/os/gcache/gcache_adapter_memory.go @@ -427,14 +427,17 @@ func (c *AdapterMemory) syncEventAndClearExpired(ctx context.Context) { } } // Processing expired keys from LRU. - if c.cap > 0 && c.lruGetList.Len() > 0 { - for { - if v := c.lruGetList.PopFront(); v != nil { - c.lru.Push(v) - } else { - break + if c.cap > 0 { + if c.lruGetList.Len() > 0 { + for { + if v := c.lruGetList.PopFront(); v != nil { + c.lru.Push(v) + } else { + break + } } } + c.lru.SyncAndClear(ctx) } // ======================== // Data Cleaning up. diff --git a/os/gcache/gcache_adapter_memory_lru.go b/os/gcache/gcache_adapter_memory_lru.go index 819954825..6583ec968 100644 --- a/os/gcache/gcache_adapter_memory_lru.go +++ b/os/gcache/gcache_adapter_memory_lru.go @@ -8,7 +8,6 @@ package gcache import ( "context" - "time" "github.com/gogf/gf/v2/container/glist" "github.com/gogf/gf/v2/container/gmap" @@ -35,7 +34,6 @@ func newMemCacheLru(cache *AdapterMemory) *adapterMemoryLru { rawList: glist.New(true), closed: gtype.NewBool(), } - gtimer.AddSingleton(context.Background(), time.Second, lru.SyncAndClear) return lru }