mirror of
https://github.com/owncast/owncast.git
synced 2024-11-22 04:40:37 +03:00
fix: split out the botscraper response cache from the web app
This commit is contained in:
parent
c8985093fb
commit
0827af6cc8
2 changed files with 39 additions and 5 deletions
|
@ -20,7 +20,7 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
cacheAdapter *cache.Adapter
|
||||
hlsCacheAdapter *cache.Adapter
|
||||
hlsResponseCache *cache.Client
|
||||
)
|
||||
|
||||
|
@ -40,13 +40,13 @@ func HandleHLSRequest(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if cacheAdapter == nil {
|
||||
if hlsCacheAdapter == nil {
|
||||
ca, err := memory.NewAdapter(
|
||||
memory.AdapterWithAlgorithm(memory.LFU),
|
||||
memory.AdapterWithCapacity(50),
|
||||
memory.AdapterWithStorageCapacity(104_857_600),
|
||||
)
|
||||
cacheAdapter = &ca
|
||||
hlsCacheAdapter = &ca
|
||||
if err != nil {
|
||||
log.Warn("unable to create web cache", err)
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ func HandleHLSRequest(w http.ResponseWriter, r *http.Request) {
|
|||
// individual segments for a long time.
|
||||
if hlsResponseCache == nil {
|
||||
rc, err := cache.NewClient(
|
||||
cache.ClientWithAdapter(*cacheAdapter),
|
||||
cache.ClientWithAdapter(*hlsCacheAdapter),
|
||||
cache.ClientWithTTL(30*time.Second),
|
||||
cache.ClientWithExpiresHeader(),
|
||||
)
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/core"
|
||||
|
@ -16,12 +17,45 @@ 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 {
|
||||
|
@ -32,7 +66,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 {
|
||||
handleScraperMetadataPage(w, r)
|
||||
indexBotSearchCache.Middleware(http.HandlerFunc(handleScraperMetadataPage))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue