RFC7234-compliant HTTP cache handler for Go, with memory and disk backends (via vfs-kit), Cache-Control parsing, and Prometheus metrics. Evolved from lox/httpcache (MIT).
go get github.com/soulteary/httpcache-kitpackage main
import (
"log""net/http""net/http/httputil"
httpcache "github.com/soulteary/httpcache-kit"
)
funcmain() {
proxy:=&httputil.ReverseProxy{
Director: func(r*http.Request) {},
}
handler:=httpcache.NewHandler(httpcache.NewMemoryCache(), proxy)
handler.Shared=truelog.Print("proxy listening on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", handler))
}Disk-backed cache with config:
cache, err:=httpcache.NewDiskCacheWithConfig("/var/cache/myproxy", httpcache.DefaultCacheConfig())
iferr!=nil {
log.Fatal(err)
}
defercache.Close()
handler:=httpcache.NewHandlerWithOptions(cache, proxy, &httpcache.HandlerOptions{Logger: myLogger})- RFC7234-compliant caching (with documented caveats)
- Memory and disk storage (disk uses vfs-kit for VFS abstraction)
- Configurable TTL, max size, cleanup interval, LRU eviction
- Optional Prometheus metrics via metrics-kit
- Optional debug logging via logger-kit
- Conditional requests (e.g.
Range) are not cached.
- RFC 7234 – Caching
- lox/httpcache – Original HTTP caching library (MIT)