chore: reverting the current implementation of http response caching
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/core"
|
||||
@@ -14,24 +13,8 @@ import (
|
||||
"github.com/owncast/owncast/models"
|
||||
"github.com/owncast/owncast/router/middleware"
|
||||
"github.com/owncast/owncast/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
cache "github.com/victorspringer/http-cache"
|
||||
"github.com/victorspringer/http-cache/adapter/memory"
|
||||
)
|
||||
|
||||
var (
|
||||
hlsCacheAdapter *cache.Adapter
|
||||
hlsResponseCache *cache.Client
|
||||
)
|
||||
|
||||
type FileServerHandler struct {
|
||||
HLSPath string
|
||||
}
|
||||
|
||||
func (fsh *FileServerHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(rw, r, fsh.HLSPath)
|
||||
}
|
||||
|
||||
// HandleHLSRequest will manage all requests to HLS content.
|
||||
func HandleHLSRequest(w http.ResponseWriter, r *http.Request) {
|
||||
// Sanity check to limit requests to HLS file types.
|
||||
@@ -40,32 +23,6 @@ func HandleHLSRequest(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if hlsCacheAdapter == nil {
|
||||
ca, err := memory.NewAdapter(
|
||||
memory.AdapterWithAlgorithm(memory.LRU),
|
||||
memory.AdapterWithCapacity(50),
|
||||
memory.AdapterWithStorageCapacity(104_857_600),
|
||||
)
|
||||
hlsCacheAdapter = &ca
|
||||
if err != nil {
|
||||
log.Warn("unable to create web cache", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Since HLS segments cannot be changed once they're rendered, we can cache
|
||||
// individual segments for a long time.
|
||||
if hlsResponseCache == nil {
|
||||
rc, err := cache.NewClient(
|
||||
cache.ClientWithAdapter(*hlsCacheAdapter),
|
||||
cache.ClientWithTTL(30*time.Second),
|
||||
cache.ClientWithExpiresHeader(),
|
||||
)
|
||||
hlsResponseCache = rc
|
||||
if err != nil {
|
||||
log.Warn("unable to create web cache client", err)
|
||||
}
|
||||
}
|
||||
|
||||
requestedPath := r.URL.Path
|
||||
relativePath := strings.Replace(requestedPath, "/hls/", "", 1)
|
||||
fullPath := filepath.Join(config.HLSStoragePath, relativePath)
|
||||
@@ -91,10 +48,6 @@ func HandleHLSRequest(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
cacheTime := utils.GetCacheDurationSecondsForPath(relativePath)
|
||||
w.Header().Set("Cache-Control", "public, max-age="+strconv.Itoa(cacheTime))
|
||||
|
||||
fileServer := &FileServerHandler{HLSPath: fullPath}
|
||||
hlsResponseCache.Middleware(fileServer).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
middleware.EnableCors(w)
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/core"
|
||||
@@ -17,45 +16,12 @@ import (
|
||||
"github.com/owncast/owncast/static"
|
||||
"github.com/owncast/owncast/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
cache "github.com/victorspringer/http-cache"
|
||||
"github.com/victorspringer/http-cache/adapter/memory"
|
||||
)
|
||||
|
||||
var (
|
||||
indexCacheAdapter *cache.Adapter
|
||||
indexBotSearchCache *cache.Client
|
||||
)
|
||||
|
||||
// IndexHandler handles the default index route.
|
||||
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
middleware.EnableCors(w)
|
||||
|
||||
if indexCacheAdapter == nil {
|
||||
ca, err := memory.NewAdapter(
|
||||
memory.AdapterWithAlgorithm(memory.LFU),
|
||||
memory.AdapterWithCapacity(50),
|
||||
memory.AdapterWithStorageCapacity(104_857_600),
|
||||
)
|
||||
indexCacheAdapter = &ca
|
||||
if err != nil {
|
||||
log.Warn("unable to create web cache", err)
|
||||
}
|
||||
}
|
||||
|
||||
if indexBotSearchCache == nil {
|
||||
rc, err := cache.NewClient(
|
||||
cache.ClientWithAdapter(*indexCacheAdapter),
|
||||
cache.ClientWithTTL(30*time.Second),
|
||||
cache.ClientWithExpiresHeader(),
|
||||
cache.ClientWithRefreshKey("bot-search-page"),
|
||||
cache.ClientWithExpiresHeader(),
|
||||
)
|
||||
indexBotSearchCache = rc
|
||||
if err != nil {
|
||||
log.Warn("unable to create web cache client", err)
|
||||
}
|
||||
}
|
||||
|
||||
isIndexRequest := r.URL.Path == "/" || filepath.Base(r.URL.Path) == "index.html" || filepath.Base(r.URL.Path) == ""
|
||||
|
||||
if utils.IsUserAgentAPlayer(r.UserAgent()) && isIndexRequest {
|
||||
@@ -66,7 +32,7 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// For search engine bots and social scrapers return a special
|
||||
// server-rendered page.
|
||||
if utils.IsUserAgentABot(r.UserAgent()) && isIndexRequest {
|
||||
indexBotSearchCache.Middleware(http.HandlerFunc(handleScraperMetadataPage))
|
||||
handleScraperMetadataPage(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user