add sync.RWMutex to listCache

This commit is contained in:
nikk gitanes
2024-04-10 14:46:05 +03:00
parent d3af9fbbcb
commit 654b2618ae

View File

@@ -8,6 +8,7 @@ import (
type DBReadCache struct { type DBReadCache struct {
db TorrServerDB db TorrServerDB
listCache map[string][]string listCache map[string][]string
listCacheMutex sync.RWMutex
dataCache map[[2]string][]byte dataCache map[[2]string][]byte
dataCacheMutex sync.RWMutex dataCacheMutex sync.RWMutex
} }
@@ -57,11 +58,16 @@ func (v *DBReadCache) Set(xPath, name string, value []byte) {
} }
func (v *DBReadCache) List(xPath string) []string { func (v *DBReadCache) List(xPath string) []string {
v.listCacheMutex.RLock()
if names, ok := v.listCache[xPath]; ok { if names, ok := v.listCache[xPath]; ok {
defer v.listCacheMutex.RUnlock()
return names return names
} }
v.listCacheMutex.RUnlock()
names := v.db.List(xPath) names := v.db.List(xPath)
v.listCacheMutex.Lock()
v.listCache[xPath] = names v.listCache[xPath] = names
v.listCacheMutex.Unlock()
return names return names
} }