diff --git a/server/app/config.go b/server/app/config.go index 707b73e..c3c5dc7 100644 --- a/server/app/config.go +++ b/server/app/config.go @@ -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{ diff --git a/server/app/setup.go b/server/app/setup.go index 869021f..dfae773 100644 --- a/server/app/setup.go +++ b/server/app/setup.go @@ -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() diff --git a/server/mapblockaccessor/get.go b/server/mapblockaccessor/get.go index 7647444..331a6a1 100644 --- a/server/mapblockaccessor/get.go +++ b/server/mapblockaccessor/get.go @@ -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() diff --git a/server/mapblockaccessor/mapblockaccessor.go b/server/mapblockaccessor/mapblockaccessor.go index 20b7845..701f257 100644 --- a/server/mapblockaccessor/mapblockaccessor.go +++ b/server/mapblockaccessor/mapblockaccessor.go @@ -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, } } diff --git a/server/mapblockaccessor/mapblockaccessor_test.go b/server/mapblockaccessor/mapblockaccessor_test.go index 0bcc037..af9a2f8 100644 --- a/server/mapblockaccessor/mapblockaccessor_test.go +++ b/server/mapblockaccessor/mapblockaccessor_test.go @@ -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 {