0

fix(video): fix hls in-memory cache being recreated on every request

This commit is contained in:
Gabe Kangas 2023-12-10 12:28:11 -08:00
parent 42249fbc58
commit c8985093fb
No known key found for this signature in database
GPG Key ID: 4345B2060657F330

View File

@ -19,6 +19,11 @@ import (
"github.com/victorspringer/http-cache/adapter/memory"
)
var (
cacheAdapter *cache.Adapter
hlsResponseCache *cache.Client
)
type FileServerHandler struct {
HLSPath string
}
@ -35,24 +40,30 @@ func HandleHLSRequest(w http.ResponseWriter, r *http.Request) {
return
}
responseCache, err := memory.NewAdapter(
memory.AdapterWithAlgorithm(memory.LRU),
memory.AdapterWithCapacity(20),
memory.AdapterWithStorageCapacity(209_715_200),
)
if err != nil {
log.Warn("unable to create web cache", err)
if cacheAdapter == nil {
ca, err := memory.NewAdapter(
memory.AdapterWithAlgorithm(memory.LFU),
memory.AdapterWithCapacity(50),
memory.AdapterWithStorageCapacity(104_857_600),
)
cacheAdapter = &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.
longTermHLSSegmentCache, err := cache.NewClient(
cache.ClientWithAdapter(responseCache),
cache.ClientWithTTL(30*time.Second),
cache.ClientWithExpiresHeader(),
)
if err != nil {
log.Warn("unable to create web cache client", err)
if hlsResponseCache == nil {
rc, err := cache.NewClient(
cache.ClientWithAdapter(*cacheAdapter),
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
@ -82,7 +93,7 @@ func HandleHLSRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "public, max-age="+strconv.Itoa(cacheTime))
fileServer := &FileServerHandler{HLSPath: fullPath}
longTermHLSSegmentCache.Middleware(fileServer).ServeHTTP(w, r)
hlsResponseCache.Middleware(fileServer).ServeHTTP(w, r)
return
}