Browse Source

Merge pull request #38 from msoedov/caching

Add caching
Alex Miasoiedov 6 years ago
parent
commit
c3b310d150
4 changed files with 35 additions and 14 deletions
  1. 1 0
      .gitignore
  2. 12 8
      glide.lock
  3. 1 0
      glide.yaml
  4. 21 6
      main.go

+ 1 - 0
.gitignore

@@ -26,3 +26,4 @@ slides/
 main
 node_modules
 hacker-slides
+vendor/

+ 12 - 8
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

+ 1 - 0
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

+ 21 - 6
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, "")
 	})