maxitem config for mapblock cache

This commit is contained in:
NatureFreshMilk 2019-03-13 09:12:22 +01:00
parent 0440dd11e1
commit 7533036967
5 changed files with 24 additions and 5 deletions

View File

@ -27,6 +27,7 @@ type Config struct {
type MapBlockAccessorConfig struct {
Expiretime string `json:"expiretime"`
Purgetime string `json:"purgetime"`
MaxItems int `json:"maxitems"`
}
type MapObjectConfig struct {
@ -128,8 +129,9 @@ func ParseConfig(filename string) (*Config, error) {
}
mapblockaccessor := MapBlockAccessorConfig{
Expiretime: "500ms",
Purgetime: "1000ms",
Expiretime: "15s",
Purgetime: "30s",
MaxItems: 5000,
}
cfg := Config{

View File

@ -69,7 +69,10 @@ func Setup(p params.ParamsType, cfg *Config) *App {
panic(err)
}
a.BlockAccessor = mapblockaccessor.NewMapBlockAccessor(a.Blockdb, expireDuration, purgeDuration)
a.BlockAccessor = mapblockaccessor.NewMapBlockAccessor(
a.Blockdb,
expireDuration, purgeDuration,
cfg.MapBlockAccessorCfg.MaxItems)
//color mapping
a.Colormapping = colormapping.NewColorMapping()

View File

@ -7,11 +7,23 @@ import (
cache "github.com/patrickmn/go-cache"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
key := getKey(pos)
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()
}
cachedblock, found := a.c.Get(key)
if found {
getCacheHitCount.Inc()

View File

@ -15,18 +15,20 @@ type MapBlockAccessor struct {
accessor db.DBAccessor
c *cache.Cache
Eventbus *eventbus.Eventbus
maxcount int
}
func getKey(pos *coords.MapBlockCoords) string {
return fmt.Sprintf("Coord %d/%d/%d", pos.X, pos.Y, pos.Z)
}
func NewMapBlockAccessor(accessor db.DBAccessor, expiretime, purgetime time.Duration) *MapBlockAccessor {
func NewMapBlockAccessor(accessor db.DBAccessor, expiretime, purgetime time.Duration, maxcount int) *MapBlockAccessor {
c := cache.New(expiretime, purgetime)
return &MapBlockAccessor{
accessor: accessor,
c: c,
Eventbus: eventbus.New(),
maxcount: maxcount,
}
}

View File

@ -32,7 +32,7 @@ func TestSimpleAccess(t *testing.T) {
panic(err)
}
cache := NewMapBlockAccessor(a, 500*time.Millisecond, 1000*time.Millisecond)
cache := NewMapBlockAccessor(a, 500*time.Millisecond, 1000*time.Millisecond, 1000)
mb, err := cache.GetMapBlock(coords.NewMapBlockCoords(0, 0, 0))
if err != nil {