From 84b18ef517777eaef939099b7ced9c0f817e5145 Mon Sep 17 00:00:00 2001 From: Janne Koschinski <janne@kuschku.de> Date: Sat, 4 May 2019 13:26:52 +0200 Subject: [PATCH] Implement caching --- cache_memory.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 ++-- go.sum | 7 ++----- main.go | 3 +++ 4 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 cache_memory.go diff --git a/cache_memory.go b/cache_memory.go new file mode 100644 index 0000000..803bf96 --- /dev/null +++ b/cache_memory.go @@ -0,0 +1,46 @@ +package main + +import ( + "encoding/json" + "git.kuschku.de/justjanne/bahn-api" + "time" +) +import "github.com/patrickmn/go-cache" + +type MemoryCache struct { + backend *cache.Cache +} + +func (m MemoryCache) Set(key string, value interface{}) error { + var err error + + var serialized []byte + if serialized, err = json.Marshal(&value); err != nil { + return err + } + + m.backend.SetDefault(key, serialized) + + return nil +} + +func (m MemoryCache) Get(key string, value interface{}) error { + var err error + + var serialized []byte + if raw, found := m.backend.Get(key); found { + serialized = raw.([]byte) + } + + if err = json.Unmarshal(serialized, &value); err != nil { + return err + } + + return nil +} + +func NewMemoryCache(expirationTime time.Duration) bahn.CacheBackend { + return MemoryCache{ + backend: cache.New(expirationTime, expirationTime*2), + } +} \ No newline at end of file diff --git a/go.mod b/go.mod index bf208d5..cdd9b02 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module git.kuschku.de/justjanne/bahn-proxy go 1.12 require ( - git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307 - golang.org/x/text v0.3.2 // indirect + git.kuschku.de/justjanne/bahn-api v0.0.0-20190504113537-452e8eed7129 + github.com/patrickmn/go-cache v2.1.0+incompatible ) diff --git a/go.sum b/go.sum index ce5f171..9d84ba5 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,7 @@ -git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307 h1:4SlG8aPNwNO94pAaG3B3yiWu7HEF1vqCdwemCbguD/I= -git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307/go.mod h1:iDtXOKb4//BILqGtwpne1dgx88H+D++ho3x+iVMImMs= -github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o= +git.kuschku.de/justjanne/bahn-api v0.0.0-20190504113537-452e8eed7129/go.mod h1:WQsAQzBR+0dlaVVuwwkx8j5uMoWDIdc8xtX4vlRrp5E= github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= -golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01 h1:po1f06KS05FvIQQA2pMuOWZAUXiy1KYdIf0ElUU2Hhc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/main.go b/main.go index f638321..daaf489 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,9 @@ func main() { HttpClient: &http.Client{ Timeout: time.Second * 10, }, + Caches: []bahn.CacheBackend{ + NewMemoryCache(5 * time.Minute), + }, } MaxResults := 20 -- GitLab