mirror of
https://github.com/owncast/owncast.git
synced 2024-12-18 15:23:55 +03:00
feat(api): cache thumb and preview in memory for fast retrieval. Help address #3462
This commit is contained in:
parent
aa0b74e236
commit
1d49801a27
1 changed files with 24 additions and 6 deletions
|
@ -3,7 +3,9 @@ package controllers
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jellydator/ttlcache/v3"
|
||||||
"github.com/owncast/owncast/config"
|
"github.com/owncast/owncast/config"
|
||||||
"github.com/owncast/owncast/utils"
|
"github.com/owncast/owncast/utils"
|
||||||
)
|
)
|
||||||
|
@ -13,16 +15,28 @@ const (
|
||||||
contentTypeGIF = "image/gif"
|
contentTypeGIF = "image/gif"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var previewThumbCache = ttlcache.New(
|
||||||
|
ttlcache.WithTTL[string, []byte](15),
|
||||||
|
ttlcache.WithCapacity[string, []byte](1),
|
||||||
|
ttlcache.WithDisableTouchOnHit[string, []byte](),
|
||||||
|
)
|
||||||
|
|
||||||
// GetThumbnail will return the thumbnail image as a response.
|
// GetThumbnail will return the thumbnail image as a response.
|
||||||
func GetThumbnail(w http.ResponseWriter, r *http.Request) {
|
func GetThumbnail(w http.ResponseWriter, r *http.Request) {
|
||||||
imageFilename := "thumbnail.jpg"
|
imageFilename := "thumbnail.jpg"
|
||||||
imagePath := filepath.Join(config.TempDir, imageFilename)
|
imagePath := filepath.Join(config.TempDir, imageFilename)
|
||||||
|
httpCacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
|
||||||
|
inMemoryCacheTime := time.Duration(15) * time.Second
|
||||||
|
|
||||||
var imageBytes []byte
|
var imageBytes []byte
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if utils.DoesFileExists(imagePath) {
|
if previewThumbCache.Get(imagePath) != nil {
|
||||||
|
ci := previewThumbCache.Get(imagePath)
|
||||||
|
imageBytes = ci.Value()
|
||||||
|
} else if utils.DoesFileExists(imagePath) {
|
||||||
imageBytes, err = getImage(imagePath)
|
imageBytes, err = getImage(imagePath)
|
||||||
|
previewThumbCache.Set(imagePath, imageBytes, inMemoryCacheTime)
|
||||||
} else {
|
} else {
|
||||||
GetLogo(w, r)
|
GetLogo(w, r)
|
||||||
return
|
return
|
||||||
|
@ -33,20 +47,25 @@ func GetThumbnail(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
|
writeBytesAsImage(imageBytes, contentTypeJPEG, w, httpCacheTime)
|
||||||
writeBytesAsImage(imageBytes, contentTypeJPEG, w, cacheTime)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPreview will return the preview gif as a response.
|
// GetPreview will return the preview gif as a response.
|
||||||
func GetPreview(w http.ResponseWriter, r *http.Request) {
|
func GetPreview(w http.ResponseWriter, r *http.Request) {
|
||||||
imageFilename := "preview.gif"
|
imageFilename := "preview.gif"
|
||||||
imagePath := filepath.Join(config.TempDir, imageFilename)
|
imagePath := filepath.Join(config.TempDir, imageFilename)
|
||||||
|
httpCacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
|
||||||
|
inMemoryCacheTime := time.Duration(15) * time.Second
|
||||||
|
|
||||||
var imageBytes []byte
|
var imageBytes []byte
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if utils.DoesFileExists(imagePath) {
|
if previewThumbCache.Get(imagePath) != nil {
|
||||||
|
ci := previewThumbCache.Get(imagePath)
|
||||||
|
imageBytes = ci.Value()
|
||||||
|
} else if utils.DoesFileExists(imagePath) {
|
||||||
imageBytes, err = getImage(imagePath)
|
imageBytes, err = getImage(imagePath)
|
||||||
|
previewThumbCache.Set(imagePath, imageBytes, inMemoryCacheTime)
|
||||||
} else {
|
} else {
|
||||||
GetLogo(w, r)
|
GetLogo(w, r)
|
||||||
return
|
return
|
||||||
|
@ -57,6 +76,5 @@ func GetPreview(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
|
writeBytesAsImage(imageBytes, contentTypeGIF, w, httpCacheTime)
|
||||||
writeBytesAsImage(imageBytes, contentTypeGIF, w, cacheTime)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue