mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 08:44:04 +01:00
Cache: Refactor internal/ttl package
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
@@ -98,7 +98,7 @@ func AddCacheHeader(c *gin.Context, maxAge ttl.Duration, public bool) {
|
|||||||
|
|
||||||
// AddCoverCacheHeader adds cover image cache control headers to the response.
|
// AddCoverCacheHeader adds cover image cache control headers to the response.
|
||||||
func AddCoverCacheHeader(c *gin.Context) {
|
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.
|
// 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 {
|
if c == nil {
|
||||||
return
|
return
|
||||||
} else if thumb.CachePublic {
|
} 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 {
|
} 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 {
|
if c == nil {
|
||||||
return
|
return
|
||||||
} else if cdn || thumb.CachePublic {
|
} 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 {
|
} 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()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -184,8 +184,8 @@ func (c *Config) Propagate() {
|
|||||||
thumb.CachePublic = c.HttpCachePublic()
|
thumb.CachePublic = c.HttpCachePublic()
|
||||||
|
|
||||||
// Set cache expiration defaults.
|
// Set cache expiration defaults.
|
||||||
ttl.Default = c.HttpCacheMaxAge()
|
ttl.CacheDefault = c.HttpCacheMaxAge()
|
||||||
ttl.Video = c.HttpVideoMaxAge()
|
ttl.CacheVideo = c.HttpVideoMaxAge()
|
||||||
|
|
||||||
// Set geocoding parameters.
|
// Set geocoding parameters.
|
||||||
places.UserAgent = c.UserAgent()
|
places.UserAgent = c.UserAgent()
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ func (c *Config) HttpCompression() string {
|
|||||||
func (c *Config) HttpCacheMaxAge() ttl.Duration {
|
func (c *Config) HttpCacheMaxAge() ttl.Duration {
|
||||||
// Return default cache maxage?
|
// Return default cache maxage?
|
||||||
if c.options.HttpCacheMaxAge < 1 {
|
if c.options.HttpCacheMaxAge < 1 {
|
||||||
return ttl.Default
|
return ttl.CacheDefault
|
||||||
} else if c.options.HttpCacheMaxAge > 31536000 {
|
} else if c.options.HttpCacheMaxAge > 31536000 {
|
||||||
return ttl.Duration(31536000)
|
return ttl.Duration(31536000)
|
||||||
}
|
}
|
||||||
@@ -100,7 +100,7 @@ func (c *Config) HttpCacheMaxAge() ttl.Duration {
|
|||||||
func (c *Config) HttpVideoMaxAge() ttl.Duration {
|
func (c *Config) HttpVideoMaxAge() ttl.Duration {
|
||||||
// Return default video maxage?
|
// Return default video maxage?
|
||||||
if c.options.HttpVideoMaxAge < 1 {
|
if c.options.HttpVideoMaxAge < 1 {
|
||||||
return ttl.Video
|
return ttl.CacheVideo
|
||||||
} else if c.options.HttpVideoMaxAge > 31536000 {
|
} else if c.options.HttpVideoMaxAge > 31536000 {
|
||||||
return ttl.Duration(31536000)
|
return ttl.Duration(31536000)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ func TestConfig_HttpCacheMaxAge(t *testing.T) {
|
|||||||
c.Options().HttpCacheMaxAge = 23
|
c.Options().HttpCacheMaxAge = 23
|
||||||
assert.Equal(t, ttl.Duration(23), c.HttpCacheMaxAge())
|
assert.Equal(t, ttl.Duration(23), c.HttpCacheMaxAge())
|
||||||
c.Options().HttpCacheMaxAge = 41536000
|
c.Options().HttpCacheMaxAge = 41536000
|
||||||
assert.Equal(t, ttl.Limit, c.HttpCacheMaxAge())
|
assert.Equal(t, ttl.CacheMaxAge, c.HttpCacheMaxAge())
|
||||||
c.Options().HttpCacheMaxAge = 0
|
c.Options().HttpCacheMaxAge = 0
|
||||||
assert.Equal(t, ttl.Duration(2592000), c.HttpCacheMaxAge())
|
assert.Equal(t, ttl.Duration(2592000), c.HttpCacheMaxAge())
|
||||||
}
|
}
|
||||||
@@ -77,13 +77,13 @@ func TestConfig_HttpCacheMaxAge(t *testing.T) {
|
|||||||
func TestConfig_HttpVideoMaxAge(t *testing.T) {
|
func TestConfig_HttpVideoMaxAge(t *testing.T) {
|
||||||
c := NewConfig(CliTestContext())
|
c := NewConfig(CliTestContext())
|
||||||
|
|
||||||
assert.Equal(t, ttl.Video, c.HttpVideoMaxAge())
|
assert.Equal(t, ttl.CacheVideo, c.HttpVideoMaxAge())
|
||||||
c.Options().HttpVideoMaxAge = 23
|
c.Options().HttpVideoMaxAge = 23
|
||||||
assert.Equal(t, ttl.Duration(23), c.HttpVideoMaxAge())
|
assert.Equal(t, ttl.Duration(23), c.HttpVideoMaxAge())
|
||||||
c.Options().HttpVideoMaxAge = 41536000
|
c.Options().HttpVideoMaxAge = 41536000
|
||||||
assert.Equal(t, ttl.Limit, c.HttpVideoMaxAge())
|
assert.Equal(t, ttl.CacheMaxAge, c.HttpVideoMaxAge())
|
||||||
c.Options().HttpVideoMaxAge = 0
|
c.Options().HttpVideoMaxAge = 0
|
||||||
assert.Equal(t, ttl.Video, c.HttpVideoMaxAge())
|
assert.Equal(t, ttl.CacheVideo, c.HttpVideoMaxAge())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfig_HttpCachePublic(t *testing.T) {
|
func TestConfig_HttpCachePublic(t *testing.T) {
|
||||||
|
|||||||
@@ -498,13 +498,13 @@ var Flags = CliFlags{
|
|||||||
}}, {
|
}}, {
|
||||||
Flag: cli.IntFlag{
|
Flag: cli.IntFlag{
|
||||||
Name: "http-cache-maxage",
|
Name: "http-cache-maxage",
|
||||||
Value: int(ttl.Default),
|
Value: int(ttl.CacheDefault),
|
||||||
Usage: "time in `SECONDS` until cached content expires",
|
Usage: "time in `SECONDS` until cached content expires",
|
||||||
EnvVar: EnvVar("HTTP_CACHE_MAXAGE"),
|
EnvVar: EnvVar("HTTP_CACHE_MAXAGE"),
|
||||||
}}, {
|
}}, {
|
||||||
Flag: cli.IntFlag{
|
Flag: cli.IntFlag{
|
||||||
Name: "http-video-maxage",
|
Name: "http-video-maxage",
|
||||||
Value: int(ttl.Video),
|
Value: int(ttl.CacheVideo),
|
||||||
Usage: "time in `SECONDS` until cached videos expire",
|
Usage: "time in `SECONDS` until cached videos expire",
|
||||||
EnvVar: EnvVar("HTTP_VIDEO_MAXAGE"),
|
EnvVar: EnvVar("HTTP_VIDEO_MAXAGE"),
|
||||||
}}, {
|
}}, {
|
||||||
|
|||||||
8
internal/ttl/cache.go
Normal file
8
internal/ttl/cache.go
Normal 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
|
||||||
|
)
|
||||||
16
internal/ttl/cache_test.go
Normal file
16
internal/ttl/cache_test.go
Normal 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)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -23,10 +23,3 @@ Additional information can be found in our Developer Guide:
|
|||||||
<https://docs.photoprism.app/developer-guide/>
|
<https://docs.photoprism.app/developer-guide/>
|
||||||
*/
|
*/
|
||||||
package ttl
|
package ttl
|
||||||
|
|
||||||
var (
|
|
||||||
Limit Duration = 31536000 // 365 days
|
|
||||||
Default Duration = 2592000 // 30 days
|
|
||||||
Video Duration = 21600 // 6 hours
|
|
||||||
Cover Duration = 3600 // 1 hour
|
|
||||||
)
|
|
||||||
Reference in New Issue
Block a user