caching.go 705 B

123456789101112131415161718192021222324
  1. package middleware
  2. import (
  3. "net/http"
  4. "strconv"
  5. "github.com/owncast/owncast/utils"
  6. )
  7. // DisableCache writes the disable cache header on the responses.
  8. func DisableCache(w http.ResponseWriter) {
  9. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  10. w.Header().Set("Expires", "Thu, 1 Jan 1970 00:00:00 GMT")
  11. }
  12. func setCacheSeconds(seconds int, w http.ResponseWriter) {
  13. secondsStr := strconv.Itoa(seconds)
  14. w.Header().Set("Cache-Control", "public, max-age="+secondsStr)
  15. }
  16. // SetCachingHeaders will set the cache control header of a response.
  17. func SetCachingHeaders(w http.ResponseWriter, r *http.Request) {
  18. setCacheSeconds(utils.GetCacheDurationSecondsForPath(r.URL.Path), w)
  19. }