From 79a7a4eef828ab3d89cfeee6d486ea0ef80fc776 Mon Sep 17 00:00:00 2001 From: Alex Myasoedov Date: Sat, 24 Feb 2018 13:06:48 -0500 Subject: [PATCH] Add caching --- .gitignore | 1 + glide.lock | 20 ++++++++++++-------- glide.yaml | 1 + main.go | 27 +++++++++++++++++++++------ 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 1479763..c5a6ab0 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ slides/ main node_modules hacker-slides +vendor/ diff --git a/glide.lock b/glide.lock index dd9f6be..79b6915 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 28df98214117e374782469412ff3b544502b6bdb218592cdd36eb73870ef7df5 -updated: 2018-02-12T17:19:10.669586-05:00 +hash: 0e3d28470984716f7203fe14687de6123130847959243865dd98b14def60cce9 +updated: 2018-02-24T13:05:48.213797-05:00 imports: - name: github.com/atrox/haikunatorgo version: a67e32b6e89f0f2d91d81d300787fc01ca0dfd86 @@ -17,7 +17,7 @@ imports: - internal - redis - name: github.com/gin-contrib/sessions - version: cccdeef56346e7037ca92de250c2b55ef5e7ffe3 + version: fda3be6efa2da56e31a1a72bffd65279191e8009 - name: github.com/gin-contrib/sse version: 22d885f9ecc78bf4ee5d72b937e4bbcdc58e8cae - name: github.com/gin-gonic/gin @@ -34,7 +34,11 @@ imports: - name: github.com/gorilla/securecookie version: e59506cc896acb7f7bf732d4fdf5e25f7ccd8983 - name: github.com/gorilla/sessions - version: 6ba88b7f1c1e2c8298ec7fb2efda9ee411375c80 + version: 7087b4d669d1bc3da42fb4e2eda73ae139a24439 +- name: github.com/hashicorp/golang-lru + version: 0fb14efe8c47ae851c0034ed7a448854d3d34cf3 + subpackages: + - simplelru - name: github.com/kidstuff/mongostore version: 256d65ac5b0e35e7c5ebb3f175c0bed1e5c2b253 - name: github.com/mattn/go-isatty @@ -42,15 +46,15 @@ imports: - name: github.com/Sirupsen/logrus version: d682213848ed68c0a260ca37d6dd5ace8423f5ba - name: github.com/ugorji/go - version: b5b949564861e43a03568a0b134c135cf318e5c8 + version: 16f09ef744fd4227190f626f14cfdefb14362b3b subpackages: - codec - name: golang.org/x/crypto - version: 9de5f2eaf759b4c4550b3db39fed2e9e5f86f45c + version: 49796115aa4b964c318aad4f3084fdb41e9aa067 subpackages: - ssh/terminal - name: golang.org/x/sys - version: 37707fdb30a5b38865cfb95e5aab41707daec7fd + version: 88d2dcc510266da9f7f8c7f34e1940716cab5f5c subpackages: - unix - windows @@ -64,7 +68,7 @@ imports: - internal/sasl - internal/scram - name: gopkg.in/yaml.v2 - version: d670f9405373e636a5a2765eea47fac0c9bc91a4 + version: 7f97868eec74b32b0982dd158a51a446d1da7eb5 testImports: - name: github.com/franela/goblin version: 74c9fe110d4bfd04c222a089a309e0a97e258534 diff --git a/glide.yaml b/glide.yaml index a1c2b8d..64eab40 100644 --- a/glide.yaml +++ b/glide.yaml @@ -7,6 +7,7 @@ import: - package: github.com/gin-contrib/sessions - package: github.com/gin-gonic/gin version: ^1.2.0 +- package: github.com/hashicorp/golang-lru testImport: - package: github.com/franela/goblin version: ^0.0.1 diff --git a/main.go b/main.go index b35c521..7a6b603 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( haikunator "github.com/atrox/haikunatorgo" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" + cache "github.com/hashicorp/golang-lru" "github.com/msoedov/hacker-slides/auth" "github.com/msoedov/hacker-slides/files" ) @@ -27,6 +28,10 @@ func NewApp() *gin.Engine { r := gin.Default() store := sessions.NewCookieStore([]byte("secret")) + arc, err := cache.NewARC(10) + if err != nil { + log.Fatalf("Failied to allocate cache %#v", err) + } r.Use(sessions.Sessions(sessionHeader, store)) r.Use(auth.BasicAuth()) @@ -100,11 +105,20 @@ func NewApp() *gin.Engine { return } - body, err := ioutil.ReadFile(path) - if err != nil { - panic(err) + var slide string + cached, ok := arc.Get(path) + if ok { + slide = string(cached.([]byte)) + } else { + body, err := ioutil.ReadFile(path) + if err != nil { + log.Errorf("Failied to read file %#v", err) + c.Abort() + return + } + slide = string(body) } - c.String(200, string(body)) + c.String(200, slide) }) r.PUT("/slides.md", func(c *gin.Context) { @@ -113,11 +127,12 @@ func NewApp() *gin.Engine { return } body, _ := ioutil.ReadAll(c.Request.Body) - ioutil.WriteFile(path, body, 0644) + arc.Add(path, body) + go ioutil.WriteFile(path, body, 0644) log.WithFields(log.Fields{ "size": len(body), "file": path, - }).Info("Wrote to file") + }).Info("Async wrote to file") c.String(200, "") })