Browse Source

Removed unnecessary duplicate static caching

pull/73/head
andreimarcu 9 years ago
parent
commit
be15ba076d
  1. 14
      fileserve.go

14
fileserve.go

@ -1,8 +1,6 @@
package main package main
import ( import (
"bytes"
"io"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -40,8 +38,6 @@ func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filePath) http.ServeFile(w, r, filePath)
} }
var staticCache = make(map[string][]byte)
func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) { func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
if path[len(path)-1:] == "/" { if path[len(path)-1:] == "/" {
@ -53,23 +49,15 @@ func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
} }
filePath := strings.TrimPrefix(path, "/static/") filePath := strings.TrimPrefix(path, "/static/")
_, exists := staticCache[filePath]
if !exists {
file, err := staticBox.Open(filePath) file, err := staticBox.Open(filePath)
if err != nil { if err != nil {
notFoundHandler(c, w, r) notFoundHandler(c, w, r)
return return
} }
buf := bytes.NewBuffer(nil)
io.Copy(buf, file)
staticCache[filePath] = buf.Bytes()
}
w.Header().Set("Etag", timeStartedStr) w.Header().Set("Etag", timeStartedStr)
w.Header().Set("Cache-Control", "max-age=86400") w.Header().Set("Cache-Control", "max-age=86400")
http.ServeContent(w, r, filePath, timeStarted, bytes.NewReader(staticCache[filePath]))
http.ServeContent(w, r, filePath, timeStarted, file)
return return
} }
} }

Loading…
Cancel
Save