Cache: Refactor internal/ttl package

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2024-01-10 10:26:38 +01:00
parent 2897248f1e
commit 481c207897
8 changed files with 39 additions and 22 deletions

View file

@ -98,7 +98,7 @@ func AddCacheHeader(c *gin.Context, maxAge ttl.Duration, public bool) {
// AddCoverCacheHeader adds cover image cache control headers to the response.
func AddCoverCacheHeader(c *gin.Context) {
AddCacheHeader(c, ttl.Cover, thumb.CachePublic)
AddCacheHeader(c, ttl.CacheCover, thumb.CachePublic)
}
// AddImmutableCacheHeader adds cache control headers to the response for immutable content like thumbnails.
@ -106,9 +106,9 @@ func AddImmutableCacheHeader(c *gin.Context) {
if c == nil {
return
} else if thumb.CachePublic {
c.Header("Cache-Control", fmt.Sprintf("public, max-age=%s, immutable", ttl.Default.String()))
c.Header("Cache-Control", fmt.Sprintf("public, max-age=%s, immutable", ttl.CacheDefault.String()))
} else {
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%s, immutable", ttl.Default.String()))
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%s, immutable", ttl.CacheDefault.String()))
}
}
@ -117,8 +117,8 @@ func AddVideoCacheHeader(c *gin.Context, cdn bool) {
if c == nil {
return
} else if cdn || thumb.CachePublic {
c.Header("Cache-Control", fmt.Sprintf("public, max-age=%s, immutable", ttl.Video.String()))
c.Header("Cache-Control", fmt.Sprintf("public, max-age=%s, immutable", ttl.CacheVideo.String()))
} else {
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%s, immutable", ttl.Video.String()))
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%s, immutable", ttl.CacheVideo.String()))
}
}

View file

@ -184,8 +184,8 @@ func (c *Config) Propagate() {
thumb.CachePublic = c.HttpCachePublic()
// Set cache expiration defaults.
ttl.Default = c.HttpCacheMaxAge()
ttl.Video = c.HttpVideoMaxAge()
ttl.CacheDefault = c.HttpCacheMaxAge()
ttl.CacheVideo = c.HttpVideoMaxAge()
// Set geocoding parameters.
places.UserAgent = c.UserAgent()

View file

@ -87,7 +87,7 @@ func (c *Config) HttpCompression() string {
func (c *Config) HttpCacheMaxAge() ttl.Duration {
// Return default cache maxage?
if c.options.HttpCacheMaxAge < 1 {
return ttl.Default
return ttl.CacheDefault
} else if c.options.HttpCacheMaxAge > 31536000 {
return ttl.Duration(31536000)
}
@ -100,7 +100,7 @@ func (c *Config) HttpCacheMaxAge() ttl.Duration {
func (c *Config) HttpVideoMaxAge() ttl.Duration {
// Return default video maxage?
if c.options.HttpVideoMaxAge < 1 {
return ttl.Video
return ttl.CacheVideo
} else if c.options.HttpVideoMaxAge > 31536000 {
return ttl.Duration(31536000)
}

View file

@ -69,7 +69,7 @@ func TestConfig_HttpCacheMaxAge(t *testing.T) {
c.Options().HttpCacheMaxAge = 23
assert.Equal(t, ttl.Duration(23), c.HttpCacheMaxAge())
c.Options().HttpCacheMaxAge = 41536000
assert.Equal(t, ttl.Limit, c.HttpCacheMaxAge())
assert.Equal(t, ttl.CacheMaxAge, c.HttpCacheMaxAge())
c.Options().HttpCacheMaxAge = 0
assert.Equal(t, ttl.Duration(2592000), c.HttpCacheMaxAge())
}
@ -77,13 +77,13 @@ func TestConfig_HttpCacheMaxAge(t *testing.T) {
func TestConfig_HttpVideoMaxAge(t *testing.T) {
c := NewConfig(CliTestContext())
assert.Equal(t, ttl.Video, c.HttpVideoMaxAge())
assert.Equal(t, ttl.CacheVideo, c.HttpVideoMaxAge())
c.Options().HttpVideoMaxAge = 23
assert.Equal(t, ttl.Duration(23), c.HttpVideoMaxAge())
c.Options().HttpVideoMaxAge = 41536000
assert.Equal(t, ttl.Limit, c.HttpVideoMaxAge())
assert.Equal(t, ttl.CacheMaxAge, c.HttpVideoMaxAge())
c.Options().HttpVideoMaxAge = 0
assert.Equal(t, ttl.Video, c.HttpVideoMaxAge())
assert.Equal(t, ttl.CacheVideo, c.HttpVideoMaxAge())
}
func TestConfig_HttpCachePublic(t *testing.T) {

View file

@ -498,13 +498,13 @@ var Flags = CliFlags{
}}, {
Flag: cli.IntFlag{
Name: "http-cache-maxage",
Value: int(ttl.Default),
Value: int(ttl.CacheDefault),
Usage: "time in `SECONDS` until cached content expires",
EnvVar: EnvVar("HTTP_CACHE_MAXAGE"),
}}, {
Flag: cli.IntFlag{
Name: "http-video-maxage",
Value: int(ttl.Video),
Value: int(ttl.CacheVideo),
Usage: "time in `SECONDS` until cached videos expire",
EnvVar: EnvVar("HTTP_VIDEO_MAXAGE"),
}}, {

8
internal/ttl/cache.go Normal file
View file

@ -0,0 +1,8 @@
package ttl
var (
CacheMaxAge Duration = 31536000 // 365 days is the maximum cache time
CacheDefault Duration = 2592000 // 30 days is the default cache time
CacheVideo Duration = 21600 // 6 hours for video streams
CacheCover Duration = 3600 // 1 hour for album cover images
)

View file

@ -0,0 +1,16 @@
package ttl
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCache(t *testing.T) {
t.Run("Defaults", func(t *testing.T) {
assert.Equal(t, Duration(365*24*3600), CacheMaxAge)
assert.Greater(t, CacheMaxAge, CacheDefault)
assert.Greater(t, CacheDefault, CacheVideo)
assert.Greater(t, CacheVideo, CacheCover)
})
}

View file

@ -23,10 +23,3 @@ Additional information can be found in our Developer Guide:
<https://docs.photoprism.app/developer-guide/>
*/
package ttl
var (
Limit Duration = 31536000 // 365 days
Default Duration = 2592000 // 30 days
Video Duration = 21600 // 6 hours
Cover Duration = 3600 // 1 hour
)