focus/backend/utils/cache.go

27 lines
503 B
Go
Raw Permalink Normal View History

2024-01-04 14:13:27 +01:00
package utils
import (
"time"
"github.com/gofiber/fiber/v2"
)
2024-01-04 16:20:00 +01:00
func Cache(c *fiber.Ctx, lastEdit *time.Time) (bool, error) {
ifModifiedSince := c.Get("If-Modified-Since")
if ifModifiedSince == "" {
return false, nil
}
clientLast, err := time.Parse(time.RFC1123, ifModifiedSince)
2024-01-04 14:13:27 +01:00
if err != nil {
return false, err
}
2024-01-04 16:20:00 +01:00
if !lastEdit.After(clientLast) {
2024-01-04 14:13:27 +01:00
c.SendStatus(fiber.StatusNotModified)
return true, nil
}
2024-01-04 16:20:00 +01:00
c.Set("Last-Modified", lastEdit.Format(time.RFC1123))
2024-01-04 14:13:27 +01:00
return false, nil
}