A simple global cache for go apps. Includes a mutex so it's concurrency safe.
$ go get github.com/karlpokus/cachelib
Create the cache
import "github.com/karlpokus/cachelib"
cache := cachelib.New("10s")
http.ListenAndServe(":8090", route(cache))
Use the cache
import "github.com/karlpokus/cachelib"
func route(cache cachelib.Cache) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if cache.Fresh() {
fmt.Fprintf(w, "%s", cache.Contents(true))
return
}
// cache stale. fetch data.
fmt.Fprintf(w, "%s", cache.Update(data))
}
}
$ go test
- tests
- godoc
- cache headers
- maybe a middleware opt
- remove fresh bool
MIT