diff --git a/internal/api/cache.go b/internal/api/cache.go index 6b598932f..1cd9ff057 100644 --- a/internal/api/cache.go +++ b/internal/api/cache.go @@ -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())) } } diff --git a/internal/config/config.go b/internal/config/config.go index 036b0e4fd..f32c08c20 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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() diff --git a/internal/config/config_server.go b/internal/config/config_server.go index dd227147b..4d1e30408 100644 --- a/internal/config/config_server.go +++ b/internal/config/config_server.go @@ -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) } diff --git a/internal/config/config_server_test.go b/internal/config/config_server_test.go index 4a8d21885..fc8363142 100644 --- a/internal/config/config_server_test.go +++ b/internal/config/config_server_test.go @@ -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) { diff --git a/internal/config/flags.go b/internal/config/flags.go index 84f4daff9..44589bf2e 100644 --- a/internal/config/flags.go +++ b/internal/config/flags.go @@ -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"), }}, { diff --git a/internal/ttl/cache.go b/internal/ttl/cache.go new file mode 100644 index 000000000..f22036819 --- /dev/null +++ b/internal/ttl/cache.go @@ -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 +) diff --git a/internal/ttl/cache_test.go b/internal/ttl/cache_test.go new file mode 100644 index 000000000..afcee882f --- /dev/null +++ b/internal/ttl/cache_test.go @@ -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) + }) +} diff --git a/internal/ttl/maxage.go b/internal/ttl/ttl.go similarity index 85% rename from internal/ttl/maxage.go rename to internal/ttl/ttl.go index bcdee65f3..782a9b7cf 100644 --- a/internal/ttl/maxage.go +++ b/internal/ttl/ttl.go @@ -23,10 +23,3 @@ Additional information can be found in our 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 -)