From 9448c67b09c302f196fe404530996c5bba11f114 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.go        |  6 ++++++
 cache_memory.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 go.mod          |  1 +
 go.sum          |  2 ++
 4 files changed, 55 insertions(+)
 create mode 100644 cache.go
 create mode 100644 cache_memory.go

diff --git a/cache.go b/cache.go
new file mode 100644
index 0000000..3158b34
--- /dev/null
+++ b/cache.go
@@ -0,0 +1,6 @@
+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
diff --git a/cache_memory.go b/cache_memory.go
new file mode 100644
index 0000000..c38c2dc
--- /dev/null
+++ b/cache_memory.go
@@ -0,0 +1,46 @@
+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
diff --git a/go.mod b/go.mod
index bf208d5..9ed9abf 100644
--- a/go.mod
+++ b/go.mod
@@ -4,5 +4,6 @@ go 1.12
 
 require (
 	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
 )
diff --git a/go.sum b/go.sum
index ce5f171..418658f 100644
--- a/go.sum
+++ b/go.sum
@@ -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=
 github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
 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/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
 golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
-- 
GitLab