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

View File

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

View File

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

View File

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

View File

@ -32,7 +32,7 @@ func TestSimpleAccess(t *testing.T) {
panic(err) 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)) mb, err := cache.GetMapBlock(coords.NewMapBlockCoords(0, 0, 0))
if err != nil { if err != nil {