From 30f7ddb833adfe276a93c1a79e243b8d33bdd41e Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 9 Feb 2021 22:29:03 +0000 Subject: Ensure memcache TTL cannot be over 30 days (#14592) Memcached TTL cannot be > 30 days and if it is attempted the TTL is interpreted as a unix timestamp. This PR ensures that the TTL is switched to a unix timestamp in those cases. Fix #14571 Signed-off-by: Andrew Thornton --- modules/setting/cache.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'modules/setting') diff --git a/modules/setting/cache.go b/modules/setting/cache.go index 618be2482a..7bfea91961 100644 --- a/modules/setting/cache.go +++ b/modules/setting/cache.go @@ -49,6 +49,9 @@ var ( } ) +// MemcacheMaxTTL represents the maximum memcache TTL +const MemcacheMaxTTL = 30 * 24 * time.Hour + func newCacheService() { sec := Cfg.Section("cache") if err := sec.MapTo(&CacheService); err != nil { @@ -85,3 +88,19 @@ func newCacheService() { log.Info("Last Commit Cache Service Enabled") } } + +// TTLSeconds returns the TTLSeconds or unix timestamp for memcache +func (c Cache) TTLSeconds() int64 { + if c.Adapter == "memcache" && c.TTL > MemcacheMaxTTL { + return time.Now().Add(c.TTL).Unix() + } + return int64(c.TTL.Seconds()) +} + +// LastCommitCacheTTLSeconds returns the TTLSeconds or unix timestamp for memcache +func LastCommitCacheTTLSeconds() int64 { + if CacheService.Adapter == "memcache" && CacheService.LastCommit.TTL > MemcacheMaxTTL { + return time.Now().Add(CacheService.LastCommit.TTL).Unix() + } + return int64(CacheService.LastCommit.TTL.Seconds()) +} -- cgit v1.2.3