1
0
forked from MTSR/mapserver
mapserver/server/mapblockaccessor/get.go
2019-02-15 08:36:48 +01:00

46 lines
913 B
Go

package mapblockaccessor
import (
"mapserver/coords"
"mapserver/eventbus"
"mapserver/mapblockparser"
cache "github.com/patrickmn/go-cache"
"github.com/prometheus/client_golang/prometheus"
)
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
key := getKey(pos)
cachedblock, found := a.c.Get(key)
if found {
getCacheHitCount.Inc()
return cachedblock.(*mapblockparser.MapBlock), nil
}
getCacheMissCount.Inc()
timer := prometheus.NewTimer(dbGetDuration)
defer timer.ObserveDuration()
block, err := a.accessor.GetBlock(pos)
if err != nil {
return nil, err
}
if block == nil {
return nil, nil
}
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, pos)
if err != nil {
return nil, err
}
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
a.c.Set(key, mapblock, cache.DefaultExpiration)
return mapblock, nil
}