Prevents cache stampede https://en.wikipedia.org/wiki/Cache_stampede by only running a single data fetch operation per expired / missing key regardless of number of requests to that key.
import (
"log/slog"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/stampede"
memcache "github.com/goware/cachestore-mem"
)
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("index"))
})
cache, err := memcache.NewBackend(1000)
if err != nil {
panic(err)
}
cacheMiddleware := stampede.Handler(
slog.Default(), cache, 5*time.Second,
stampede.WithHTTPCacheKeyRequestHeaders([]string{"AuthorizatioN"}),
)
r.With(cacheMiddleware).Get("/cached", func(w http.ResponseWriter, r *http.Request) {
// processing..
time.Sleep(1 * time.Second)
w.WriteHeader(200)
w.Write([]byte("...hi"))
})
http.ListenAndServe(":3333", r)
}
ttl
time duration for subequence requests, which offers further caching. You may also
use a ttl value of 0 if you want the response to be as fresh as possible, and still
prevent a stampede scenario on your handler.keyFunc to the stampede.Handler and
split the cache by an account's id. NOTE: we do avoid caching response headers
for CORS, set-cookie and x-ratelimit.See example for a variety of examples.
MIT
Go
98.3%
Makefile
1.7%
Prevents cache stampede https://en.wikipedia.org/wiki/Cache_stampede by only running a single data fetch operation per expired / missing key regardless of number of requests to that key.
import (
"log/slog"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/stampede"
memcache "github.com/goware/cachestore-mem"
)
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("index"))
})
cache, err := memcache.NewBackend(1000)
if err != nil {
panic(err)
}
cacheMiddleware := stampede.Handler(
slog.Default(), cache, 5*time.Second,
stampede.WithHTTPCacheKeyRequestHeaders([]string{"AuthorizatioN"}),
)
r.With(cacheMiddleware).Get("/cached", func(w http.ResponseWriter, r *http.Request) {
// processing..
time.Sleep(1 * time.Second)
w.WriteHeader(200)
w.Write([]byte("...hi"))
})
http.ListenAndServe(":3333", r)
}
ttl
time duration for subequence requests, which offers further caching. You may also
use a ttl value of 0 if you want the response to be as fresh as possible, and still
prevent a stampede scenario on your handler.keyFunc to the stampede.Handler and
split the cache by an account's id. NOTE: we do avoid caching response headers
for CORS, set-cookie and x-ratelimit.See example for a variety of examples.
MIT
Go
98.3%
Makefile
1.7%