Skip to content

Commit

Permalink
add option to disable cache
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Oct 11, 2023
1 parent 0fd5273 commit ecad16a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 46 deletions.
7 changes: 5 additions & 2 deletions dev/mapserver.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"mapblockaccessor": {
"expiretime": "15s",
"purgetime": "30s",
"maxitems": 500
"maxitems": 0
},
"defaultoverlays": [
"mapserver_poi",
Expand All @@ -75,5 +75,8 @@
"skins": {
"enableskinsdb": false,
"skinspath": ""
}
},
"worldpath": "./",
"datapath": "./",
"ColorsTxtPath": "./"
}
99 changes: 55 additions & 44 deletions mapblockaccessor/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,60 @@ import (
var lock = &sync.RWMutex{}

func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.MapBlock, error) {
cache_enabled := a.maxcount > 0
key := getKey(pos)

//maintenance
cacheBlocks.Set(float64(a.blockcache.ItemCount()))
if a.blockcache.ItemCount() > a.maxcount {
//flush cache
fields := logrus.Fields{
"cached items": a.blockcache.ItemCount(),
"maxcount": a.maxcount,
}
logrus.WithFields(fields).Debug("Flushing cache")

a.blockcache.Flush()
}
if cache_enabled {

//read section
lock.RLock()
//maintenance
cacheBlocks.Set(float64(a.blockcache.ItemCount()))
if a.blockcache.ItemCount() > a.maxcount {
//flush cache
fields := logrus.Fields{
"cached items": a.blockcache.ItemCount(),
"maxcount": a.maxcount,
}
logrus.WithFields(fields).Debug("Flushing cache")

cachedblock, found := a.blockcache.Get(key)
if found {
defer lock.RUnlock()

getCacheHitCount.Inc()
if cachedblock == nil {
return nil, nil
} else {
return cachedblock.(*mapparser.MapBlock), nil
a.blockcache.Flush()
}
}

//end read
lock.RUnlock()
//read section
lock.RLock()

timer := prometheus.NewTimer(dbGetDuration)
defer timer.ObserveDuration()
cachedblock, found := a.blockcache.Get(key)
if found {
defer lock.RUnlock()

//write section
lock.Lock()
defer lock.Unlock()
getCacheHitCount.Inc()
if cachedblock == nil {
return nil, nil
} else {
return cachedblock.(*mapparser.MapBlock), nil
}
}

//try read
cachedblock, found = a.blockcache.Get(key)
if found {
getCacheHitCount.Inc()
if cachedblock == nil {
return nil, nil
} else {
return cachedblock.(*mapparser.MapBlock), nil
//end read
lock.RUnlock()

timer := prometheus.NewTimer(dbGetDuration)
defer timer.ObserveDuration()

//write section
lock.Lock()
defer lock.Unlock()

//try read
cachedblock, found = a.blockcache.Get(key)
if found {
getCacheHitCount.Inc()
if cachedblock == nil {
return nil, nil
} else {
return cachedblock.(*mapparser.MapBlock), nil
}
}

}

block, err := a.accessor.GetBlock(pos)
Expand All @@ -73,12 +78,16 @@ func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.M

if block == nil {
//no mapblock here
cacheBlockCount.Inc()
a.blockcache.Set(key, nil, cache.DefaultExpiration)
if cache_enabled {
cacheBlockCount.Inc()
a.blockcache.Set(key, nil, cache.DefaultExpiration)
}
return nil, nil
}

getCacheMissCount.Inc()
if cache_enabled {
getCacheMissCount.Inc()
}

mapblock, err := mapparser.Parse(block.Data)
if err != nil {
Expand All @@ -87,8 +96,10 @@ func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.M

a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, pos))

cacheBlockCount.Inc()
a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
if cache_enabled {
cacheBlockCount.Inc()
a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
}

return mapblock, nil
}

0 comments on commit ecad16a

Please sign in to comment.