fix: file cache, concurrent map writes (#116)

This commit is contained in:
aping
2023-11-07 17:16:23 +08:00
committed by GitHub
parent 1d2eb020e2
commit 731a057075

View File

@@ -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
}