Skip to content
Snippets Groups Projects
Verified Commit 9448c67b 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
type CacheBackend interface {
Set(namespace string, key string, value interface{}) error
Get(namespace string, key string, value interface{}) error
}
\ No newline at end of file
package main
import (
"encoding/json"
"fmt"
"time"
)
import "github.com/patrickmn/go-cache"
type MemoryCache struct {
backend *cache.Cache
}
func (m MemoryCache) Set(namespace string, key string, value interface{}) error {
var err error
var serialized []byte
if serialized, err = json.Marshal(&value); err != nil {
return err
}
m.backend.SetDefault(fmt.Sprintf("%s:%s", namespace, key), serialized)
return nil
}
func (m MemoryCache) Get(namespace string, key string, value interface{}) error {
var err error
var serialized []byte
if raw, found := m.backend.Get(fmt.Sprintf("%s:%s", namespace, key)); found {
serialized = raw.([]byte)
}
if err = json.Unmarshal(serialized, &value); err != nil {
return err
}
return nil
}
func NewMemoryCache(expirationTime time.Duration) CacheBackend {
return MemoryCache{
backend: cache.New(expirationTime, expirationTime*2),
}
}
\ No newline at end of file
...@@ -4,5 +4,6 @@ go 1.12 ...@@ -4,5 +4,6 @@ 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-20190503215445-3da40decb307
github.com/patrickmn/go-cache v2.1.0+incompatible
golang.org/x/text v0.3.2 // indirect golang.org/x/text v0.3.2 // indirect
) )
...@@ -2,6 +2,8 @@ git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307 h1:4SlG8aPN ...@@ -2,6 +2,8 @@ git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307 h1:4SlG8aPN
git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307/go.mod h1:iDtXOKb4//BILqGtwpne1dgx88H+D++ho3x+iVMImMs= 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 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=
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=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01 h1:po1f06KS05FvIQQA2pMuOWZAUXiy1KYdIf0ElUU2Hhc= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01 h1:po1f06KS05FvIQQA2pMuOWZAUXiy1KYdIf0ElUU2Hhc=
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 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment