2019-02-10 22:27:53 +03:00
|
|
|
package mapblockaccessor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mapserver/coords"
|
|
|
|
"mapserver/eventbus"
|
|
|
|
"mapserver/mapblockparser"
|
2019-03-22 19:32:43 +03:00
|
|
|
"sync"
|
2019-02-10 22:27:53 +03:00
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2019-03-22 19:32:43 +03:00
|
|
|
var lock = &sync.RWMutex{}
|
|
|
|
|
2019-02-10 22:27:53 +03:00
|
|
|
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
|
|
|
|
key := getKey(pos)
|
|
|
|
|
2019-03-22 19:32:43 +03:00
|
|
|
//maintenance
|
2019-03-13 12:41:37 +03:00
|
|
|
cacheBlocks.Set(float64(a.blockcache.ItemCount()))
|
|
|
|
if a.blockcache.ItemCount() > a.maxcount {
|
2019-03-13 11:12:22 +03:00
|
|
|
//flush cache
|
|
|
|
fields := logrus.Fields{
|
2019-03-13 12:41:37 +03:00
|
|
|
"cached items": a.blockcache.ItemCount(),
|
2019-03-13 11:12:22 +03:00
|
|
|
"maxcount": a.maxcount,
|
|
|
|
}
|
2019-03-13 12:41:37 +03:00
|
|
|
logrus.WithFields(fields).Debug("Flushing cache")
|
2019-03-13 11:12:22 +03:00
|
|
|
|
2019-03-13 12:41:37 +03:00
|
|
|
a.blockcache.Flush()
|
2019-03-13 11:12:22 +03:00
|
|
|
}
|
|
|
|
|
2019-03-22 19:32:43 +03:00
|
|
|
//read section
|
|
|
|
lock.RLock()
|
|
|
|
|
2019-03-13 12:41:37 +03:00
|
|
|
cachedblock, found := a.blockcache.Get(key)
|
2019-02-10 22:27:53 +03:00
|
|
|
if found {
|
2019-03-22 19:32:43 +03:00
|
|
|
defer lock.RUnlock()
|
|
|
|
|
2019-02-15 10:36:48 +03:00
|
|
|
getCacheHitCount.Inc()
|
2019-03-13 23:14:04 +03:00
|
|
|
if cachedblock == nil {
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
return cachedblock.(*mapblockparser.MapBlock), nil
|
|
|
|
}
|
2019-02-10 22:27:53 +03:00
|
|
|
}
|
|
|
|
|
2019-03-22 19:32:43 +03:00
|
|
|
//end read
|
|
|
|
lock.RUnlock()
|
|
|
|
|
2019-02-15 10:36:48 +03:00
|
|
|
timer := prometheus.NewTimer(dbGetDuration)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
2019-03-22 19:32:43 +03:00
|
|
|
//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.(*mapblockparser.MapBlock), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 22:27:53 +03:00
|
|
|
block, err := a.accessor.GetBlock(pos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if block == nil {
|
2019-03-13 12:41:37 +03:00
|
|
|
//no mapblock here
|
|
|
|
cacheBlockCount.Inc()
|
|
|
|
a.blockcache.Set(key, nil, cache.DefaultExpiration)
|
2019-02-10 22:27:53 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 12:41:37 +03:00
|
|
|
getCacheMissCount.Inc()
|
|
|
|
|
2019-02-10 22:27:53 +03:00
|
|
|
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, pos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
|
|
|
|
|
2019-03-13 12:41:37 +03:00
|
|
|
cacheBlockCount.Inc()
|
|
|
|
a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
|
2019-02-10 22:27:53 +03:00
|
|
|
|
|
|
|
return mapblock, nil
|
|
|
|
}
|