1
0
forked from MTSR/mapserver
mapserver/mapblockaccessor/get.go

94 lines
1.8 KiB
Go
Raw Normal View History

2019-02-10 22:27:53 +03:00
package mapblockaccessor
import (
"mapserver/coords"
"mapserver/eventbus"
"sync"
2019-02-10 22:27:53 +03:00
2021-10-12 08:44:07 +03:00
"github.com/minetest-go/mapparser"
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
)
var lock = &sync.RWMutex{}
2021-10-12 08:44:07 +03:00
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.MapBlock, error) {
2019-02-10 22:27:53 +03:00
key := getKey(pos)
//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
}
//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 {
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 {
2021-10-12 08:44:07 +03:00
return cachedblock.(*mapparser.MapBlock), nil
2019-03-13 23:14:04 +03:00
}
2019-02-10 22:27:53 +03:00
}
//end read
lock.RUnlock()
2019-02-15 10:36:48 +03:00
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 {
2021-10-12 08:44:07 +03:00
return cachedblock.(*mapparser.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()
2021-10-12 08:44:07 +03:00
mapblock, err := mapparser.Parse(block.Data)
2019-02-10 22:27:53 +03:00
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
}