From 731a057075e6f23c737eae08b4ccf7dfa2150e27 Mon Sep 17 00:00:00 2001 From: aping Date: Tue, 7 Nov 2023 17:16:23 +0800 Subject: [PATCH] fix: file cache, concurrent map writes (#116) --- driver_file.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/driver_file.go b/driver_file.go index beba85f..4d990a7 100644 --- a/driver_file.go +++ b/driver_file.go @@ -56,15 +56,15 @@ func (c *FileCache) Has(key string) bool { // Get value by key func (c *FileCache) Get(key string) any { - c.lock.RLock() - defer c.lock.RUnlock() - return c.get(key) } func (c *FileCache) get(key string) any { // read cache from memory - if val := c.MemoryCache.get(key); val != nil { + c.lock.RLock() + val := c.MemoryCache.get(key) + c.lock.RUnlock() + if val != nil { return val } @@ -87,7 +87,9 @@ func (c *FileCache) get(key string) any { return nil } + c.lock.Lock() c.caches[key] = item // save to memory. + c.lock.Unlock() return item.Val }