1
0
forked from MTSR/mapserver

accessor cache

This commit is contained in:
NatureFreshMilk 2019-01-11 09:07:31 +01:00
parent 8eac64571a
commit 5ebc282bf9
3 changed files with 29 additions and 2 deletions

1
go.mod
View File

@ -2,5 +2,6 @@ module mapserver
require ( require (
github.com/mattn/go-sqlite3 v1.10.0 github.com/mattn/go-sqlite3 v1.10.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/sirupsen/logrus v1.3.0 github.com/sirupsen/logrus v1.3.0
) )

2
go.sum
View File

@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o= github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME= github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=

View File

@ -4,30 +4,54 @@ import (
"mapserver/coords" "mapserver/coords"
"mapserver/db" "mapserver/db"
"mapserver/mapblockparser" "mapserver/mapblockparser"
"github.com/patrickmn/go-cache"
"time"
"fmt"
) )
type MapBlockAccessor struct { type MapBlockAccessor struct {
accessor db.DBAccessor accessor db.DBAccessor
c *cache.Cache
}
func getKey(pos coords.MapBlockCoords) string {
return fmt.Sprintf("Coord %d/%d/%d", pos.X, pos.Y, pos.Z)
} }
func NewMapBlockAccessor(accessor db.DBAccessor) *MapBlockAccessor { func NewMapBlockAccessor(accessor db.DBAccessor) *MapBlockAccessor {
return &MapBlockAccessor{accessor: accessor} c := cache.New(5*time.Minute, 10*time.Minute)
return &MapBlockAccessor{accessor: accessor, c: c}
} }
func (a *MapBlockAccessor) Update(pos coords.MapBlockCoords, mb *mapblockparser.MapBlock) { func (a *MapBlockAccessor) Update(pos coords.MapBlockCoords, mb *mapblockparser.MapBlock) {
//TODO: cache key := getKey(pos)
a.c.Set(key, mb, cache.DefaultExpiration)
} }
func (a *MapBlockAccessor) GetMapBlock(pos coords.MapBlockCoords) (*mapblockparser.MapBlock, error) { func (a *MapBlockAccessor) GetMapBlock(pos coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
key := getKey(pos)
cachedblock, found := a.c.Get(key)
if found {
return cachedblock.(*mapblockparser.MapBlock), nil
}
block, err := a.accessor.GetBlock(pos) block, err := a.accessor.GetBlock(pos)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if block == nil {
return nil, nil
}
mapblock, err := mapblockparser.Parse(block.Data) mapblock, err := mapblockparser.Parse(block.Data)
if err != nil { if err != nil {
return nil, err return nil, err
} }
a.c.Set(key, mapblock, cache.DefaultExpiration)
return mapblock, nil return mapblock, nil
} }