Files
TorrServerJellyfin/server/settings/dbreadcache.go
nikk gitanes b46f996a3d Squashed commit of the following:
commit 218d84b7905d23dce3c8f842ed78b8eac8ea9580
Author: nikk gitanes <tsynik@gmail.com>
Date:   Mon Feb 5 01:10:19 2024 +0300

    add read-only DB logging

commit 826fde8e8444a9169afeb7f777e1803fcf6bd435
Author: nikk gitanes <tsynik@gmail.com>
Date:   Sat Feb 3 22:50:19 2024 +0300

    update msx doc

commit 98b7d61bd98269b0e456cb71a5d50a8f98203aba
Merge: 490ee26 963c7da
Author: nikk gitanes <tsynik@gmail.com>
Date:   Sat Feb 3 11:21:45 2024 +0300

    Merge branch 'master' into jsondb

commit 490ee26d3f14316cb1ad3b872ebe408a5aa2a91b
Merge: 83c7ed1 efb17ee
Author: nikk gitanes <tsynik@gmail.com>
Date:   Sat Feb 3 10:50:41 2024 +0300

    Merge branch 'master' into jsondb
2024-02-06 12:44:49 +03:00

71 lines
1.5 KiB
Go

package settings
import "server/log"
type DBReadCache struct {
db TorrServerDB
listCache map[string][]string
dataCache map[[2]string][]byte
}
func NewDBReadCache(db TorrServerDB) TorrServerDB {
cdb := &DBReadCache{
db: db,
listCache: map[string][]string{},
dataCache: map[[2]string][]byte{},
}
return cdb
}
func (v *DBReadCache) CloseDB() {
v.db.CloseDB()
v.db = nil
v.listCache = nil
v.dataCache = nil
}
func (v *DBReadCache) Get(xPath, name string) []byte {
cacheKey := v.makeDataCacheKey(xPath, name)
if data, ok := v.dataCache[cacheKey]; ok {
return data
}
data := v.db.Get(xPath, name)
v.dataCache[cacheKey] = data
return data
}
func (v *DBReadCache) Set(xPath, name string, value []byte) {
if ReadOnly {
log.TLogln("DB.Set: Read-only DB mode!", name)
return
}
cacheKey := v.makeDataCacheKey(xPath, name)
v.dataCache[cacheKey] = value
delete(v.listCache, xPath)
v.db.Set(xPath, name, value)
}
func (v *DBReadCache) List(xPath string) []string {
if names, ok := v.listCache[xPath]; ok {
return names
}
names := v.db.List(xPath)
v.listCache[xPath] = names
return names
}
func (v *DBReadCache) Rem(xPath, name string) {
if ReadOnly {
log.TLogln("DB.Rem: Read-only DB mode!", name)
return
}
cacheKey := v.makeDataCacheKey(xPath, name)
delete(v.dataCache, cacheKey)
delete(v.listCache, xPath)
v.db.Rem(xPath, name)
}
func (v *DBReadCache) makeDataCacheKey(xPath, name string) [2]string {
return [2]string{xPath, name}
}