mapserver/server/mapblockaccessor/get.go

58 lines
1.1 KiB
Go
Raw Normal View History

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-03-13 11:12:22 +03:00
"github.com/sirupsen/logrus"
2019-02-10 22:27:53 +03:00
)
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
key := getKey(pos)
2019-03-13 11:12:22 +03:00
if a.c.ItemCount() > a.maxcount {
//flush cache
fields := logrus.Fields{
"cached items": a.c.ItemCount(),
"maxcount": a.maxcount,
}
logrus.WithFields(fields).Warn("Flushing cache")
a.c.Flush()
}
2019-02-10 22:27:53 +03:00
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
}