Add caching
This commit is contained in:
parent
df65ad18c1
commit
79a7a4eef8
4 changed files with 35 additions and 14 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -26,3 +26,4 @@ slides/
|
||||||
main
|
main
|
||||||
node_modules
|
node_modules
|
||||||
hacker-slides
|
hacker-slides
|
||||||
|
vendor/
|
||||||
|
|
20
glide.lock
generated
20
glide.lock
generated
|
@ -1,5 +1,5 @@
|
||||||
hash: 28df98214117e374782469412ff3b544502b6bdb218592cdd36eb73870ef7df5
|
hash: 0e3d28470984716f7203fe14687de6123130847959243865dd98b14def60cce9
|
||||||
updated: 2018-02-12T17:19:10.669586-05:00
|
updated: 2018-02-24T13:05:48.213797-05:00
|
||||||
imports:
|
imports:
|
||||||
- name: github.com/atrox/haikunatorgo
|
- name: github.com/atrox/haikunatorgo
|
||||||
version: a67e32b6e89f0f2d91d81d300787fc01ca0dfd86
|
version: a67e32b6e89f0f2d91d81d300787fc01ca0dfd86
|
||||||
|
@ -17,7 +17,7 @@ imports:
|
||||||
- internal
|
- internal
|
||||||
- redis
|
- redis
|
||||||
- name: github.com/gin-contrib/sessions
|
- name: github.com/gin-contrib/sessions
|
||||||
version: cccdeef56346e7037ca92de250c2b55ef5e7ffe3
|
version: fda3be6efa2da56e31a1a72bffd65279191e8009
|
||||||
- name: github.com/gin-contrib/sse
|
- name: github.com/gin-contrib/sse
|
||||||
version: 22d885f9ecc78bf4ee5d72b937e4bbcdc58e8cae
|
version: 22d885f9ecc78bf4ee5d72b937e4bbcdc58e8cae
|
||||||
- name: github.com/gin-gonic/gin
|
- name: github.com/gin-gonic/gin
|
||||||
|
@ -34,7 +34,11 @@ imports:
|
||||||
- name: github.com/gorilla/securecookie
|
- name: github.com/gorilla/securecookie
|
||||||
version: e59506cc896acb7f7bf732d4fdf5e25f7ccd8983
|
version: e59506cc896acb7f7bf732d4fdf5e25f7ccd8983
|
||||||
- name: github.com/gorilla/sessions
|
- name: github.com/gorilla/sessions
|
||||||
version: 6ba88b7f1c1e2c8298ec7fb2efda9ee411375c80
|
version: 7087b4d669d1bc3da42fb4e2eda73ae139a24439
|
||||||
|
- name: github.com/hashicorp/golang-lru
|
||||||
|
version: 0fb14efe8c47ae851c0034ed7a448854d3d34cf3
|
||||||
|
subpackages:
|
||||||
|
- simplelru
|
||||||
- name: github.com/kidstuff/mongostore
|
- name: github.com/kidstuff/mongostore
|
||||||
version: 256d65ac5b0e35e7c5ebb3f175c0bed1e5c2b253
|
version: 256d65ac5b0e35e7c5ebb3f175c0bed1e5c2b253
|
||||||
- name: github.com/mattn/go-isatty
|
- name: github.com/mattn/go-isatty
|
||||||
|
@ -42,15 +46,15 @@ imports:
|
||||||
- name: github.com/Sirupsen/logrus
|
- name: github.com/Sirupsen/logrus
|
||||||
version: d682213848ed68c0a260ca37d6dd5ace8423f5ba
|
version: d682213848ed68c0a260ca37d6dd5ace8423f5ba
|
||||||
- name: github.com/ugorji/go
|
- name: github.com/ugorji/go
|
||||||
version: b5b949564861e43a03568a0b134c135cf318e5c8
|
version: 16f09ef744fd4227190f626f14cfdefb14362b3b
|
||||||
subpackages:
|
subpackages:
|
||||||
- codec
|
- codec
|
||||||
- name: golang.org/x/crypto
|
- name: golang.org/x/crypto
|
||||||
version: 9de5f2eaf759b4c4550b3db39fed2e9e5f86f45c
|
version: 49796115aa4b964c318aad4f3084fdb41e9aa067
|
||||||
subpackages:
|
subpackages:
|
||||||
- ssh/terminal
|
- ssh/terminal
|
||||||
- name: golang.org/x/sys
|
- name: golang.org/x/sys
|
||||||
version: 37707fdb30a5b38865cfb95e5aab41707daec7fd
|
version: 88d2dcc510266da9f7f8c7f34e1940716cab5f5c
|
||||||
subpackages:
|
subpackages:
|
||||||
- unix
|
- unix
|
||||||
- windows
|
- windows
|
||||||
|
@ -64,7 +68,7 @@ imports:
|
||||||
- internal/sasl
|
- internal/sasl
|
||||||
- internal/scram
|
- internal/scram
|
||||||
- name: gopkg.in/yaml.v2
|
- name: gopkg.in/yaml.v2
|
||||||
version: d670f9405373e636a5a2765eea47fac0c9bc91a4
|
version: 7f97868eec74b32b0982dd158a51a446d1da7eb5
|
||||||
testImports:
|
testImports:
|
||||||
- name: github.com/franela/goblin
|
- name: github.com/franela/goblin
|
||||||
version: 74c9fe110d4bfd04c222a089a309e0a97e258534
|
version: 74c9fe110d4bfd04c222a089a309e0a97e258534
|
||||||
|
|
|
@ -7,6 +7,7 @@ import:
|
||||||
- package: github.com/gin-contrib/sessions
|
- package: github.com/gin-contrib/sessions
|
||||||
- package: github.com/gin-gonic/gin
|
- package: github.com/gin-gonic/gin
|
||||||
version: ^1.2.0
|
version: ^1.2.0
|
||||||
|
- package: github.com/hashicorp/golang-lru
|
||||||
testImport:
|
testImport:
|
||||||
- package: github.com/franela/goblin
|
- package: github.com/franela/goblin
|
||||||
version: ^0.0.1
|
version: ^0.0.1
|
||||||
|
|
27
main.go
27
main.go
|
@ -12,6 +12,7 @@ import (
|
||||||
haikunator "github.com/atrox/haikunatorgo"
|
haikunator "github.com/atrox/haikunatorgo"
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
cache "github.com/hashicorp/golang-lru"
|
||||||
"github.com/msoedov/hacker-slides/auth"
|
"github.com/msoedov/hacker-slides/auth"
|
||||||
"github.com/msoedov/hacker-slides/files"
|
"github.com/msoedov/hacker-slides/files"
|
||||||
)
|
)
|
||||||
|
@ -27,6 +28,10 @@ func NewApp() *gin.Engine {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
store := sessions.NewCookieStore([]byte("secret"))
|
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(sessions.Sessions(sessionHeader, store))
|
||||||
r.Use(auth.BasicAuth())
|
r.Use(auth.BasicAuth())
|
||||||
|
|
||||||
|
@ -100,11 +105,20 @@ func NewApp() *gin.Engine {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadFile(path)
|
var slide string
|
||||||
if err != nil {
|
cached, ok := arc.Get(path)
|
||||||
panic(err)
|
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) {
|
r.PUT("/slides.md", func(c *gin.Context) {
|
||||||
|
@ -113,11 +127,12 @@ func NewApp() *gin.Engine {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
body, _ := ioutil.ReadAll(c.Request.Body)
|
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{
|
log.WithFields(log.Fields{
|
||||||
"size": len(body),
|
"size": len(body),
|
||||||
"file": path,
|
"file": path,
|
||||||
}).Info("Wrote to file")
|
}).Info("Async wrote to file")
|
||||||
c.String(200, "")
|
c.String(200, "")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue