2019-02-10 22:27:53 +03:00
|
|
|
package mapblockaccessor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mapserver/coords"
|
|
|
|
"mapserver/eventbus"
|
|
|
|
"mapserver/mapblockparser"
|
|
|
|
|
|
|
|
cache "github.com/patrickmn/go-cache"
|
2019-02-15 10:36:48 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-02-10 22:27:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
|
|
|
|
key := getKey(pos)
|
|
|
|
|
|
|
|
cachedblock, found := a.c.Get(key)
|
|
|
|
if found {
|
2019-02-15 10:36:48 +03:00
|
|
|
getCacheHitCount.Inc()
|
2019-02-10 22:27:53 +03:00
|
|
|
return cachedblock.(*mapblockparser.MapBlock), nil
|
|
|
|
}
|
|
|
|
|
2019-02-15 10:36:48 +03:00
|
|
|
getCacheMissCount.Inc()
|
|
|
|
|
|
|
|
timer := prometheus.NewTimer(dbGetDuration)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
2019-02-10 22:27:53 +03:00
|
|
|
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
|
|
|
|
}
|