Skip to content

Instantly share code, notes, and snippets.

@djherbis
Last active August 29, 2015 14:17
Show Gist options
  • Save djherbis/be170c3f3a5e489ce216 to your computer and use it in GitHub Desktop.
Save djherbis/be170c3f3a5e489ce216 to your computer and use it in GitHub Desktop.
FsCache Example.go
import (
"io"
"log"
"net/http"
"github.com/djherbis/fscache"
)
// This will only get called on a cache-miss
func Handler(w http.Response, r *http.Request) {
io.Copy(w, expensiveProcessReader(r.URL.String()))
}
func main(){
// Create a new cache directory, expire items after 6 hours of inactivity
c, err := fscache.New("./cache", 0700, 6*time.Hour)
if err != nil {
log.Fatal(err.Error())
}
// That's it? Yep.
http.LisenAndServe(":8080", fscache.Handler(c, http.HandlerFunc(Handler)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment