diff --git a/controllers/hls.go b/controllers/hls.go index d9b84c440..cfd3985d6 100644 --- a/controllers/hls.go +++ b/controllers/hls.go @@ -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 }