Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

7 Commits

Repository files navigation

go-cachewrapper

Trivial wrapper for http.Handler that applies Cache-Control headers in the style of DropWizard.

This uses a range of option functions to configure the cache header pragmas.

Example

package main
import (
"fmt""log""net/http""time"
cw "github.com/bigkevmcd/go-cachewrapper"
)
funchelloWorld(w http.ResponseWriter, r*http.Request) {
fmt.Fprintf(w, "Hello, World!\n")
}
constcacheTime=time.Minute*60funcmain() {
http.Handle("/", cw.Cached(http.HandlerFunc(helloWorld), cw.MaxAge(cacheTime), cw.NoTransform())
log.Fatal(http.ListenAndServe(":8000", nil))
}

It's easy to see the result using curl.

$ curl -i http://localhost:8000/
HTTP/1.1 200 OK
Cache-Control: no-transform, max-age=3600
Date: Tue, 29 Apr 2014 12:14:09 GMT
Content-Length: 14
Content-Type: text/plain; charset=utf-8
Hello, World!

Backwards compatibility

If you need backwards compatibility with CacheOptions configuration, you can wrap it in a Config option with...

http.Handle("/", cw.Cached(http.HandlerFunc(helloWorld), cw.Config(cw.CacheOptions{MaxAge: time.Hour*24*13, NoTransform: true})))

About

Trivial wrapper for http.Handler that applies Cache-Control headers in the style of DropWizard.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages