saasitone/middleware/cache.go

95 lines
2.3 KiB
Go
Raw Normal View History

2021-12-03 05:18:40 -08:00
package middleware
import (
"fmt"
"net/http"
2021-12-06 11:53:28 -08:00
"time"
2021-12-03 05:18:40 -08:00
2022-01-01 07:44:18 -08:00
"github.com/mikestefanello/pagoda/context"
2021-12-07 18:36:57 -08:00
"github.com/eko/gocache/v2/cache"
"github.com/eko/gocache/v2/marshaler"
"github.com/go-redis/redis/v8"
2021-12-03 05:18:40 -08:00
"github.com/labstack/echo/v4"
)
2021-12-21 17:49:05 -08:00
// CachedPage is what is used to store a rendered Page in the cache
2021-12-07 18:36:57 -08:00
type CachedPage struct {
2021-12-21 17:49:05 -08:00
// URL stores the URL of the requested page
URL string
// HTML stores the complete HTML of the rendered Page
HTML []byte
// StatusCode stores the HTTP status code
2021-12-07 18:36:57 -08:00
StatusCode int
2021-12-21 17:49:05 -08:00
// Headers stores the HTTP headers
Headers map[string]string
2021-12-07 18:36:57 -08:00
}
2021-12-21 17:49:05 -08:00
// ServeCachedPage attempts to load a page from the cache by matching on the complete request URL
// If a page is cached for the requested URL, it will be served here and the request terminated.
// Any request made by an authenticated user or that is not a GET will be skipped.
func ServeCachedPage(ch *cache.Cache) echo.MiddlewareFunc {
2021-12-07 18:36:57 -08:00
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// Skip non GET requests
if c.Request().Method != http.MethodGet {
return next(c)
}
// Skip if the user is authenticated
if c.Get(context.AuthenticatedUserKey) != nil {
return next(c)
}
2021-12-21 17:49:05 -08:00
// Attempt to load from cache
res, err := marshaler.New(ch).Get(
c.Request().Context(),
c.Request().URL.String(),
new(CachedPage),
)
2021-12-07 18:36:57 -08:00
if err != nil {
if err == redis.Nil {
2021-12-21 17:49:05 -08:00
c.Logger().Info("no cached page found")
2021-12-07 18:36:57 -08:00
} else {
c.Logger().Errorf("failed getting cached page: %v", err)
2021-12-07 18:36:57 -08:00
}
return next(c)
}
page, ok := res.(*CachedPage)
if !ok {
c.Logger().Errorf("failed casting cached page")
return next(c)
}
2021-12-07 18:36:57 -08:00
2021-12-21 17:49:05 -08:00
// Set any headers
2021-12-07 18:36:57 -08:00
if page.Headers != nil {
for k, v := range page.Headers {
c.Response().Header().Set(k, v)
}
}
2021-12-21 17:49:05 -08:00
2021-12-19 11:56:00 -08:00
c.Logger().Info("serving cached page")
2021-12-07 18:36:57 -08:00
return c.HTMLBlob(page.StatusCode, page.HTML)
}
}
}
2021-12-21 17:49:05 -08:00
// CacheControl sets a Cache-Control header with a given max age
2021-12-06 11:53:28 -08:00
func CacheControl(maxAge time.Duration) echo.MiddlewareFunc {
2021-12-03 05:18:40 -08:00
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
v := "no-cache, no-store"
if maxAge > 0 {
2021-12-06 11:53:28 -08:00
v = fmt.Sprintf("public, max-age=%.0f", maxAge.Seconds())
2021-12-03 05:18:40 -08:00
}
c.Response().Header().Set("Cache-Control", v)
return next(c)
}
}
}