mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 21:46:11 +05:00
fix race in DBReadCache
This commit is contained in:
@@ -1,11 +1,15 @@
|
|||||||
package settings
|
package settings
|
||||||
|
|
||||||
import "server/log"
|
import (
|
||||||
|
"server/log"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type DBReadCache struct {
|
type DBReadCache struct {
|
||||||
db TorrServerDB
|
db TorrServerDB
|
||||||
listCache map[string][]string
|
listCache map[string][]string
|
||||||
dataCache map[[2]string][]byte
|
dataCache map[[2]string][]byte
|
||||||
|
dataCacheMutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDBReadCache(db TorrServerDB) TorrServerDB {
|
func NewDBReadCache(db TorrServerDB) TorrServerDB {
|
||||||
@@ -26,11 +30,16 @@ func (v *DBReadCache) CloseDB() {
|
|||||||
|
|
||||||
func (v *DBReadCache) Get(xPath, name string) []byte {
|
func (v *DBReadCache) Get(xPath, name string) []byte {
|
||||||
cacheKey := v.makeDataCacheKey(xPath, name)
|
cacheKey := v.makeDataCacheKey(xPath, name)
|
||||||
|
v.dataCacheMutex.RLock()
|
||||||
|
defer v.dataCacheMutex.RUnlock()
|
||||||
if data, ok := v.dataCache[cacheKey]; ok {
|
if data, ok := v.dataCache[cacheKey]; ok {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
v.dataCacheMutex.RUnlock()
|
||||||
data := v.db.Get(xPath, name)
|
data := v.db.Get(xPath, name)
|
||||||
|
v.dataCacheMutex.Lock()
|
||||||
v.dataCache[cacheKey] = data
|
v.dataCache[cacheKey] = data
|
||||||
|
v.dataCacheMutex.Unlock()
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +49,9 @@ func (v *DBReadCache) Set(xPath, name string, value []byte) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
cacheKey := v.makeDataCacheKey(xPath, name)
|
cacheKey := v.makeDataCacheKey(xPath, name)
|
||||||
|
v.dataCacheMutex.Lock()
|
||||||
v.dataCache[cacheKey] = value
|
v.dataCache[cacheKey] = value
|
||||||
|
v.dataCacheMutex.Unlock()
|
||||||
delete(v.listCache, xPath)
|
delete(v.listCache, xPath)
|
||||||
v.db.Set(xPath, name, value)
|
v.db.Set(xPath, name, value)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user