fix(video): fix hls in-memory cache being recreated on every request
This commit is contained in:
parent
42249fbc58
commit
c8985093fb
@ -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,25 +40,31 @@ 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 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),
|
||||
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
|
||||
relativePath := strings.Replace(requestedPath, "/hls/", "", 1)
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user