Config: Rename HttpCacheTTL to HttpCacheMaxAge #3297

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2023-03-20 20:37:07 +01:00
parent 3dfa6dc4f4
commit f63ac04956
9 changed files with 27 additions and 27 deletions

View file

@ -10,8 +10,8 @@ import (
"github.com/photoprism/photoprism/internal/thumb" "github.com/photoprism/photoprism/internal/thumb"
) )
// CoverCacheTTL specifies the number of seconds to cache album covers. // CoverMaxAge specifies the number of seconds to cache album covers.
var CoverCacheTTL thumb.MaxAge = 3600 // 1 hour var CoverMaxAge thumb.MaxAge = 3600 // 1 hour
type ThumbCache struct { type ThumbCache struct {
FileName string FileName string
@ -81,14 +81,14 @@ func AddCacheHeader(c *gin.Context, maxAge thumb.MaxAge, 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, CoverCacheTTL, thumb.CachePublic) AddCacheHeader(c, CoverMaxAge, thumb.CachePublic)
} }
// AddThumbCacheHeader adds thumbnail cache control headers to the response. // AddThumbCacheHeader adds thumbnail cache control headers to the response.
func AddThumbCacheHeader(c *gin.Context) { func AddThumbCacheHeader(c *gin.Context) {
if thumb.CachePublic { if thumb.CachePublic {
c.Header("Cache-Control", fmt.Sprintf("public, max-age=%s, no-transform, immutable", thumb.CacheTTL.String())) c.Header("Cache-Control", fmt.Sprintf("public, max-age=%s, no-transform, immutable", thumb.CacheMaxAge.String()))
} else { } else {
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%s, no-transform, immutable", thumb.CacheTTL.String())) c.Header("Cache-Control", fmt.Sprintf("private, max-age=%s, no-transform, immutable", thumb.CacheMaxAge.String()))
} }
} }

View file

@ -60,7 +60,7 @@ func startAction(ctx *cli.Context) error {
{"detach-server", fmt.Sprintf("%t", conf.DetachServer())}, {"detach-server", fmt.Sprintf("%t", conf.DetachServer())},
{"http-mode", conf.HttpMode()}, {"http-mode", conf.HttpMode()},
{"http-compression", conf.HttpCompression()}, {"http-compression", conf.HttpCompression()},
{"http-cache-ttl", fmt.Sprintf("%d", conf.HttpCacheTTL())}, {"http-cache-maxage", fmt.Sprintf("%d", conf.HttpCacheMaxAge())},
{"http-cache-public", fmt.Sprintf("%t", conf.HttpCachePublic())}, {"http-cache-public", fmt.Sprintf("%t", conf.HttpCachePublic())},
{"http-host", conf.HttpHost()}, {"http-host", conf.HttpHost()},
{"http-port", fmt.Sprintf("%d", conf.HttpPort())}, {"http-port", fmt.Sprintf("%d", conf.HttpPort())},

View file

@ -165,7 +165,7 @@ func (c *Config) Propagate() {
thumb.SizeUncached = c.ThumbSizeUncached() thumb.SizeUncached = c.ThumbSizeUncached()
thumb.Filter = c.ThumbFilter() thumb.Filter = c.ThumbFilter()
thumb.JpegQuality = c.JpegQuality() thumb.JpegQuality = c.JpegQuality()
thumb.CacheTTL = c.HttpCacheTTL() thumb.CacheMaxAge = c.HttpCacheMaxAge()
thumb.CachePublic = c.HttpCachePublic() thumb.CachePublic = c.HttpCachePublic()
// Set geocoding parameters. // Set geocoding parameters.

View file

@ -83,17 +83,17 @@ func (c *Config) HttpCompression() string {
return strings.ToLower(strings.TrimSpace(c.options.HttpCompression)) return strings.ToLower(strings.TrimSpace(c.options.HttpCompression))
} }
// HttpCacheTTL returns the HTTP response cache time in seconds. // HttpCacheMaxAge returns the time in seconds until cached content expires.
func (c *Config) HttpCacheTTL() thumb.MaxAge { func (c *Config) HttpCacheMaxAge() thumb.MaxAge {
if c.options.HttpCacheTTL < 1 || c.options.HttpCacheTTL > 31536000 { if c.options.HttpCacheMaxAge < 1 || c.options.HttpCacheMaxAge > 31536000 {
// Default to one month. // Default to one month.
return thumb.CacheTTL return thumb.CacheMaxAge
} }
return thumb.MaxAge(c.options.HttpCacheTTL) return thumb.MaxAge(c.options.HttpCacheMaxAge)
} }
// HttpCachePublic checks whether HTTP responses may be cached publicly, e.g. by a CDN. // HttpCachePublic checks whether static content may be cached by a CDN or caching proxy.
func (c *Config) HttpCachePublic() bool { func (c *Config) HttpCachePublic() bool {
if c.options.HttpCachePublic { if c.options.HttpCachePublic {
return true return true

View file

@ -52,14 +52,14 @@ func TestConfig_HttpCompression(t *testing.T) {
assert.Equal(t, "", c.HttpCompression()) assert.Equal(t, "", c.HttpCompression())
} }
func TestConfig_HttpCacheTTL(t *testing.T) { func TestConfig_HttpCacheMaxAge(t *testing.T) {
c := NewConfig(CliTestContext()) c := NewConfig(CliTestContext())
assert.Equal(t, thumb.MaxAge(2592000), c.HttpCacheTTL()) assert.Equal(t, thumb.MaxAge(2592000), c.HttpCacheMaxAge())
c.Options().HttpCacheTTL = 23 c.Options().HttpCacheMaxAge = 23
assert.Equal(t, thumb.MaxAge(23), c.HttpCacheTTL()) assert.Equal(t, thumb.MaxAge(23), c.HttpCacheMaxAge())
c.Options().HttpCacheTTL = 0 c.Options().HttpCacheMaxAge = 0
assert.Equal(t, thumb.MaxAge(2592000), c.HttpCacheTTL()) assert.Equal(t, thumb.MaxAge(2592000), c.HttpCacheMaxAge())
} }
func TestConfig_HttpCachePublic(t *testing.T) { func TestConfig_HttpCachePublic(t *testing.T) {

View file

@ -484,14 +484,14 @@ var Flags = CliFlags{
EnvVar: EnvVar("HTTP_COMPRESSION"), EnvVar: EnvVar("HTTP_COMPRESSION"),
}}, { }}, {
Flag: cli.IntFlag{ Flag: cli.IntFlag{
Name: "http-cache-ttl", Name: "http-cache-maxage",
Value: int(thumb.CacheTTL), Value: int(thumb.CacheMaxAge),
Usage: "number of `SECONDS` that a browser or CDN is allowed to cache HTTP responses", Usage: "time in `SECONDS` until cached content expires",
EnvVar: EnvVar("HTTP_CACHE_TTL"), EnvVar: EnvVar("HTTP_CACHE_MAXAGE"),
}}, { }}, {
Flag: cli.BoolFlag{ Flag: cli.BoolFlag{
Name: "http-cache-public", Name: "http-cache-public",
Usage: "allow HTTP responses to be stored in a public cache, e.g. a CDN or caching proxy", Usage: "allow static content to be cached by a CDN or caching proxy",
EnvVar: EnvVar("HTTP_CACHE_PUBLIC"), EnvVar: EnvVar("HTTP_CACHE_PUBLIC"),
}}, { }}, {
Flag: cli.StringFlag{ Flag: cli.StringFlag{

View file

@ -110,7 +110,7 @@ type Options struct {
TLSKey string `yaml:"TLSKey" json:"TLSKey" flag:"tls-key"` TLSKey string `yaml:"TLSKey" json:"TLSKey" flag:"tls-key"`
HttpMode string `yaml:"HttpMode" json:"-" flag:"http-mode"` HttpMode string `yaml:"HttpMode" json:"-" flag:"http-mode"`
HttpCompression string `yaml:"HttpCompression" json:"-" flag:"http-compression"` HttpCompression string `yaml:"HttpCompression" json:"-" flag:"http-compression"`
HttpCacheTTL int `yaml:"HttpCacheTTL" json:"HttpCacheTTL" flag:"http-cache-ttl"` HttpCacheMaxAge int `yaml:"HttpCacheMaxAge" json:"HttpCacheMaxAge" flag:"http-cache-maxage"`
HttpCachePublic bool `yaml:"HttpCachePublic" json:"HttpCachePublic" flag:"http-cache-public"` HttpCachePublic bool `yaml:"HttpCachePublic" json:"HttpCachePublic" flag:"http-cache-public"`
HttpHost string `yaml:"HttpHost" json:"-" flag:"http-host"` HttpHost string `yaml:"HttpHost" json:"-" flag:"http-host"`
HttpPort int `yaml:"HttpPort" json:"-" flag:"http-port"` HttpPort int `yaml:"HttpPort" json:"-" flag:"http-port"`

View file

@ -158,7 +158,7 @@ func (c *Config) Report() (rows [][]string, cols []string) {
{"tls-key", c.TLSKey()}, {"tls-key", c.TLSKey()},
{"http-mode", c.HttpMode()}, {"http-mode", c.HttpMode()},
{"http-compression", c.HttpCompression()}, {"http-compression", c.HttpCompression()},
{"http-cache-ttl", fmt.Sprintf("%d", c.HttpCacheTTL())}, {"http-cache-maxage", fmt.Sprintf("%d", c.HttpCacheMaxAge())},
{"http-cache-public", fmt.Sprintf("%t", c.HttpCachePublic())}, {"http-cache-public", fmt.Sprintf("%t", c.HttpCachePublic())},
{"http-host", c.HttpHost()}, {"http-host", c.HttpHost()},
{"http-port", fmt.Sprintf("%d", c.HttpPort())}, {"http-port", fmt.Sprintf("%d", c.HttpPort())},

View file

@ -11,6 +11,6 @@ func (a MaxAge) String() string {
} }
var ( var (
CacheTTL MaxAge = 2592000 CacheMaxAge MaxAge = 2592000
CachePublic = false CachePublic = false
) )