Skip to content
Snippets Groups Projects
Verified Commit 84b18ef5 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Implement caching

parent a56542f8
No related branches found
No related tags found
No related merge requests found
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
...@@ -3,6 +3,6 @@ module git.kuschku.de/justjanne/bahn-proxy ...@@ -3,6 +3,6 @@ module git.kuschku.de/justjanne/bahn-proxy
go 1.12 go 1.12
require ( require (
git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307 git.kuschku.de/justjanne/bahn-api v0.0.0-20190504113537-452e8eed7129
golang.org/x/text v0.3.2 // indirect github.com/patrickmn/go-cache v2.1.0+incompatible
) )
git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307 h1:4SlG8aPNwNO94pAaG3B3yiWu7HEF1vqCdwemCbguD/I= git.kuschku.de/justjanne/bahn-api v0.0.0-20190504113537-452e8eed7129/go.mod h1:WQsAQzBR+0dlaVVuwwkx8j5uMoWDIdc8xtX4vlRrp5E=
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=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= 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/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/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= 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= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
......
...@@ -37,6 +37,9 @@ func main() { ...@@ -37,6 +37,9 @@ func main() {
HttpClient: &http.Client{ HttpClient: &http.Client{
Timeout: time.Second * 10, Timeout: time.Second * 10,
}, },
Caches: []bahn.CacheBackend{
NewMemoryCache(5 * time.Minute),
},
} }
MaxResults := 20 MaxResults := 20
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment