mapserver/mapblockaccessor/mapblockaccessor.go

82 lines
1.7 KiB
Go
Raw Normal View History

2019-01-10 19:50:31 +03:00
package mapblockaccessor
import (
2019-01-13 18:37:03 +03:00
"fmt"
"github.com/patrickmn/go-cache"
2019-01-10 19:50:31 +03:00
"mapserver/coords"
"mapserver/db"
"mapserver/mapblockparser"
2019-01-11 11:07:31 +03:00
"time"
2019-01-10 19:50:31 +03:00
)
type MapBlockAccessor struct {
accessor db.DBAccessor
2019-01-13 18:37:03 +03:00
c *cache.Cache
2019-01-11 11:07:31 +03:00
}
func getKey(pos coords.MapBlockCoords) string {
return fmt.Sprintf("Coord %d/%d/%d", pos.X, pos.Y, pos.Z)
2019-01-10 19:50:31 +03:00
}
func NewMapBlockAccessor(accessor db.DBAccessor) *MapBlockAccessor {
2019-01-11 11:07:31 +03:00
c := cache.New(5*time.Minute, 10*time.Minute)
return &MapBlockAccessor{accessor: accessor, c: c}
2019-01-10 19:50:31 +03:00
}
func (a *MapBlockAccessor) Update(pos coords.MapBlockCoords, mb *mapblockparser.MapBlock) {
2019-01-11 11:07:31 +03:00
key := getKey(pos)
a.c.Set(key, mb, cache.DefaultExpiration)
2019-01-10 19:50:31 +03:00
}
2019-01-18 15:07:01 +03:00
func (a *MapBlockAccessor) FindLatestMapBlocks(mintime int64, limit int) ([]*mapblockparser.MapBlock, error){
blocks, err := a.accessor.FindLatestBlocks(mintime, limit)
if err != nil {
return nil, err
}
mblist := make([]*mapblockparser.MapBlock, 0)
for _, block := range(blocks) {
key := getKey(block.Pos)
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime)
if err != nil {
return nil, err
}
a.c.Set(key, mapblock, cache.DefaultExpiration)
mblist = append(mblist, mapblock)
}
return mblist, nil
}
2019-01-10 19:50:31 +03:00
func (a *MapBlockAccessor) GetMapBlock(pos coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
2019-01-11 11:07:31 +03:00
key := getKey(pos)
cachedblock, found := a.c.Get(key)
if found {
return cachedblock.(*mapblockparser.MapBlock), nil
}
2019-01-10 19:50:31 +03:00
block, err := a.accessor.GetBlock(pos)
if err != nil {
return nil, err
}
2019-01-11 11:07:31 +03:00
if block == nil {
return nil, nil
}
2019-01-18 15:07:01 +03:00
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime)
2019-01-10 19:50:31 +03:00
if err != nil {
return nil, err
}
2019-01-11 11:07:31 +03:00
a.c.Set(key, mapblock, cache.DefaultExpiration)
2019-01-10 19:50:31 +03:00
return mapblock, nil
}