baa middleware for cache get request based on url and context.
cache enabled or not, default is false
.
cache expires in seconds, default is 60 * 10
s.
set response headers, default is nil
.
default is false
.
if set true
, baa context data will be used as request cache key.
data set to baa context by using c.Set("foo", "bar")
will be json marshaled as part of cache key.
a cache DI must be set before use this middleware:
import (
"github.com/go-baa/cache"
_ "github.com/go-baa/cache/redis"
)
b.SetDI("cache", cache.New(cache.Options{
// ...
}))
Set as the last middleware of baa:
if baa.Env == baa.PROD {
// Gzip
b.Use(gzip.Gzip(gzip.Options{CompressionLevel: 4}))
// Request Cache
b.Use(requestcache.Middleware(requestcache.Option{
Enabled: true,
Expires: requestcache.DefaultExpires,
}))
}
Or after request/content processing middlewares.
cache := requestcache.Middleware(requestcache.Option{
Enabled: !b.Debug(),
Expires: requestcache.DefaultExpires,
})
b.Group("/some-prefix", func() {
// ...
}, cache)
cache1 := requestcache.Middleware(requestcache.Option{
Enabled: !b.Debug(),
Expires: 60 * 10,
})
b.Group("/some-prefix", func() {
// ...
}, cache1)
cache2 := requestcache.Middleware(requestcache.Option{
Enabled: !b.Debug(),
Expires: 60 * 30,
})
b.Group("/some-prefix-2", func() {
// ...
}, cache2)