Loading cache/cache.go 0 → 100644 +22 −0 Original line number Diff line number Diff line package Cache import ( "fmt" "image/color" "net/http" "strings" tgColors "github.com/taironas/tinygraphs/colors" ) func IsCached(w *http.ResponseWriter, r *http.Request, key string, colors []color.RGBA, size int) bool { e := `"` + key + tgColors.ArrayToHexString(colors) + fmt.Sprintf("%d", size) + `"` (*w).Header().Set("Etag", e) (*w).Header().Set("Cache-Control", "max-age=2592000") // 30 days if match := r.Header.Get("If-None-Match"); match != "" { if strings.Contains(match, e) { return true } } return false } controllers/spaceinvaders/spaceinvaders.go +3 −16 Original line number Diff line number Diff line Loading @@ -3,14 +3,12 @@ package spaceinvaders import ( "crypto/md5" "fmt" "image/color" "io" "log" "net/http" "strings" "github.com/taironas/route" tgColors "github.com/taironas/tinygraphs/colors" "github.com/taironas/tinygraphs/cache" "github.com/taironas/tinygraphs/draw/spaceinvaders" "github.com/taironas/tinygraphs/extract" "github.com/taironas/tinygraphs/write" Loading @@ -31,7 +29,8 @@ func SpaceInvaders(w http.ResponseWriter, r *http.Request) { colors := extract.Colors(r) size := extract.Size(r) if isCached(&w, r, key, colors, size) { if Cache.IsCached(&w, r, key, colors, size) { w.WriteHeader(http.StatusNotModified) return } Loading @@ -39,15 +38,3 @@ func SpaceInvaders(w http.ResponseWriter, r *http.Request) { write.ImageSVG(w) spaceinvaders.SpaceInvaders(w, key, colors, size) } func isCached(w *http.ResponseWriter, r *http.Request, key string, colors []color.RGBA, size int) bool { e := `"` + key + tgColors.ArrayToHexString(colors) + fmt.Sprintf("%d", size) + `"` (*w).Header().Set("Etag", e) (*w).Header().Set("Cache-Control", "max-age=2592000") // 30 days if match := r.Header.Get("If-None-Match"); match != "" { if strings.Contains(match, e) { return true } } return false } Loading
cache/cache.go 0 → 100644 +22 −0 Original line number Diff line number Diff line package Cache import ( "fmt" "image/color" "net/http" "strings" tgColors "github.com/taironas/tinygraphs/colors" ) func IsCached(w *http.ResponseWriter, r *http.Request, key string, colors []color.RGBA, size int) bool { e := `"` + key + tgColors.ArrayToHexString(colors) + fmt.Sprintf("%d", size) + `"` (*w).Header().Set("Etag", e) (*w).Header().Set("Cache-Control", "max-age=2592000") // 30 days if match := r.Header.Get("If-None-Match"); match != "" { if strings.Contains(match, e) { return true } } return false }
controllers/spaceinvaders/spaceinvaders.go +3 −16 Original line number Diff line number Diff line Loading @@ -3,14 +3,12 @@ package spaceinvaders import ( "crypto/md5" "fmt" "image/color" "io" "log" "net/http" "strings" "github.com/taironas/route" tgColors "github.com/taironas/tinygraphs/colors" "github.com/taironas/tinygraphs/cache" "github.com/taironas/tinygraphs/draw/spaceinvaders" "github.com/taironas/tinygraphs/extract" "github.com/taironas/tinygraphs/write" Loading @@ -31,7 +29,8 @@ func SpaceInvaders(w http.ResponseWriter, r *http.Request) { colors := extract.Colors(r) size := extract.Size(r) if isCached(&w, r, key, colors, size) { if Cache.IsCached(&w, r, key, colors, size) { w.WriteHeader(http.StatusNotModified) return } Loading @@ -39,15 +38,3 @@ func SpaceInvaders(w http.ResponseWriter, r *http.Request) { write.ImageSVG(w) spaceinvaders.SpaceInvaders(w, key, colors, size) } func isCached(w *http.ResponseWriter, r *http.Request, key string, colors []color.RGBA, size int) bool { e := `"` + key + tgColors.ArrayToHexString(colors) + fmt.Sprintf("%d", size) + `"` (*w).Header().Set("Etag", e) (*w).Header().Set("Cache-Control", "max-age=2592000") // 30 days if match := r.Header.Get("If-None-Match"); match != "" { if strings.Contains(match, e) { return true } } return false }