1
0
forked from MTSR/mapserver

add option to disable cache

This commit is contained in:
BuckarooBanzay 2023-10-11 17:33:41 +02:00
parent 0fd52738d0
commit ecad16ae7e
2 changed files with 58 additions and 44 deletions

View File

@ -65,7 +65,7 @@
"mapblockaccessor": { "mapblockaccessor": {
"expiretime": "15s", "expiretime": "15s",
"purgetime": "30s", "purgetime": "30s",
"maxitems": 500 "maxitems": 0
}, },
"defaultoverlays": [ "defaultoverlays": [
"mapserver_poi", "mapserver_poi",
@ -75,5 +75,8 @@
"skins": { "skins": {
"enableskinsdb": false, "enableskinsdb": false,
"skinspath": "" "skinspath": ""
} },
"worldpath": "./",
"datapath": "./",
"ColorsTxtPath": "./"
} }

View File

@ -15,55 +15,60 @@ import (
var lock = &sync.RWMutex{} var lock = &sync.RWMutex{}
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.MapBlock, error) { func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.MapBlock, error) {
cache_enabled := a.maxcount > 0
key := getKey(pos) key := getKey(pos)
//maintenance if cache_enabled {
cacheBlocks.Set(float64(a.blockcache.ItemCount()))
if a.blockcache.ItemCount() > a.maxcount { //maintenance
//flush cache cacheBlocks.Set(float64(a.blockcache.ItemCount()))
fields := logrus.Fields{ if a.blockcache.ItemCount() > a.maxcount {
"cached items": a.blockcache.ItemCount(), //flush cache
"maxcount": a.maxcount, fields := logrus.Fields{
"cached items": a.blockcache.ItemCount(),
"maxcount": a.maxcount,
}
logrus.WithFields(fields).Debug("Flushing cache")
a.blockcache.Flush()
} }
logrus.WithFields(fields).Debug("Flushing cache")
a.blockcache.Flush() //read section
} lock.RLock()
//read section cachedblock, found := a.blockcache.Get(key)
lock.RLock() if found {
defer lock.RUnlock()
cachedblock, found := a.blockcache.Get(key) getCacheHitCount.Inc()
if found { if cachedblock == nil {
defer lock.RUnlock() return nil, nil
} else {
getCacheHitCount.Inc() return cachedblock.(*mapparser.MapBlock), nil
if cachedblock == nil { }
return nil, nil
} else {
return cachedblock.(*mapparser.MapBlock), nil
} }
}
//end read //end read
lock.RUnlock() lock.RUnlock()
timer := prometheus.NewTimer(dbGetDuration) timer := prometheus.NewTimer(dbGetDuration)
defer timer.ObserveDuration() defer timer.ObserveDuration()
//write section //write section
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
//try read //try read
cachedblock, found = a.blockcache.Get(key) cachedblock, found = a.blockcache.Get(key)
if found { if found {
getCacheHitCount.Inc() getCacheHitCount.Inc()
if cachedblock == nil { if cachedblock == nil {
return nil, nil return nil, nil
} else { } else {
return cachedblock.(*mapparser.MapBlock), nil return cachedblock.(*mapparser.MapBlock), nil
}
} }
} }
block, err := a.accessor.GetBlock(pos) block, err := a.accessor.GetBlock(pos)
@ -73,12 +78,16 @@ func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.M
if block == nil { if block == nil {
//no mapblock here //no mapblock here
cacheBlockCount.Inc() if cache_enabled {
a.blockcache.Set(key, nil, cache.DefaultExpiration) cacheBlockCount.Inc()
a.blockcache.Set(key, nil, cache.DefaultExpiration)
}
return nil, nil return nil, nil
} }
getCacheMissCount.Inc() if cache_enabled {
getCacheMissCount.Inc()
}
mapblock, err := mapparser.Parse(block.Data) mapblock, err := mapparser.Parse(block.Data)
if err != nil { if err != nil {
@ -87,8 +96,10 @@ func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.M
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, pos)) a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, pos))
cacheBlockCount.Inc() if cache_enabled {
a.blockcache.Set(key, mapblock, cache.DefaultExpiration) cacheBlockCount.Inc()
a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
}
return mapblock, nil return mapblock, nil
} }