https://github.com/johejo/cachehandler
Package cachehandler provides net/http middleware that caches HTTP responses.
https://github.com/johejo/cachehandler
cache caching go golang handler http middleware
Last synced: 7 months ago
JSON representation
Package cachehandler provides net/http middleware that caches HTTP responses.
- Host: GitHub
- URL: https://github.com/johejo/cachehandler
- Owner: johejo
- License: mit
- Created: 2021-02-19T18:06:08.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-08-19T09:05:20.000Z (over 3 years ago)
- Last Synced: 2024-06-21T11:01:22.474Z (almost 2 years ago)
- Topics: cache, caching, go, golang, handler, http, middleware
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cachehandler
Package cachehandler provides net/http middleware that caches HTTP responses.
Inspired by go-chi/stampede. https://github.com/go-chi/stampede
The HTTP response is stored in the cache and calls to handlers with the same key are merged.
[](https://github.com/johejo/cachehandler/actions?query=workflow%3Aci)
[](https://pkg.go.dev/github.com/johejo/cachehandler)
[](https://codecov.io/gh/johejo/cachehandler)
[](https://goreportcard.com/report/github.com/johejo/cachehandler)
## Example
```go
package cachehandler_test
import (
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/johejo/cachehandler"
)
func Example() {
mux := http.NewServeMux()
m := cachehandler.NewMiddleware(1000, 1*time.Hour, cachehandler.BasicKeyFunc())
var called int
mux.Handle("/", m.Wrap(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
called++
})))
ts := httptest.NewServer(mux)
defer ts.Close()
resp1, err := http.Get(ts.URL + "/foo")
if err != nil {
panic(err)
}
defer resp1.Body.Close()
resp2, err := http.Get(ts.URL + "/foo")
if err != nil {
panic(err)
}
defer resp2.Body.Close()
fmt.Printf("called %d", called)
// Output:
// called 1
}
```
## License
MIT
## Author
Mitsuo Heijo