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 <art27@cantab.net>
This commit is contained in:
parent
3a4801d195
commit
30f7ddb833
7 changed files with 29 additions and 10 deletions
6
modules/cache/cache.go
vendored
6
modules/cache/cache.go
vendored
|
@ -58,7 +58,7 @@ func GetString(key string, getFunc func() (string, error)) (string, error) {
|
||||||
if value, err = getFunc(); err != nil {
|
if value, err = getFunc(); err != nil {
|
||||||
return value, err
|
return value, err
|
||||||
}
|
}
|
||||||
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
|
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ func GetInt(key string, getFunc func() (int, error)) (int, error) {
|
||||||
if value, err = getFunc(); err != nil {
|
if value, err = getFunc(); err != nil {
|
||||||
return value, err
|
return value, err
|
||||||
}
|
}
|
||||||
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
|
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
@ -118,7 +118,7 @@ func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
|
||||||
if value, err = getFunc(); err != nil {
|
if value, err = getFunc(); err != nil {
|
||||||
return value, err
|
return value, err
|
||||||
}
|
}
|
||||||
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
|
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,5 +25,5 @@ func (c *LastCommitCache) getCacheKey(repoPath, ref, entryPath string) string {
|
||||||
// Put put the last commit id with commit and entry path
|
// Put put the last commit id with commit and entry path
|
||||||
func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
|
func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
|
||||||
log("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
|
log("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
|
||||||
return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl)
|
return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl())
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,14 +16,14 @@ import (
|
||||||
// LastCommitCache represents a cache to store last commit
|
// LastCommitCache represents a cache to store last commit
|
||||||
type LastCommitCache struct {
|
type LastCommitCache struct {
|
||||||
repoPath string
|
repoPath string
|
||||||
ttl int64
|
ttl func() int64
|
||||||
repo *Repository
|
repo *Repository
|
||||||
commitCache map[string]*object.Commit
|
commitCache map[string]*object.Commit
|
||||||
cache Cache
|
cache Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLastCommitCache creates a new last commit cache for repo
|
// NewLastCommitCache creates a new last commit cache for repo
|
||||||
func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl int64, cache Cache) *LastCommitCache {
|
func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64, cache Cache) *LastCommitCache {
|
||||||
if cache == nil {
|
if cache == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,14 +13,14 @@ import (
|
||||||
// LastCommitCache represents a cache to store last commit
|
// LastCommitCache represents a cache to store last commit
|
||||||
type LastCommitCache struct {
|
type LastCommitCache struct {
|
||||||
repoPath string
|
repoPath string
|
||||||
ttl int64
|
ttl func() int64
|
||||||
repo *Repository
|
repo *Repository
|
||||||
commitCache map[string]*Commit
|
commitCache map[string]*Commit
|
||||||
cache Cache
|
cache Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLastCommitCache creates a new last commit cache for repo
|
// NewLastCommitCache creates a new last commit cache for repo
|
||||||
func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl int64, cache Cache) *LastCommitCache {
|
func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64, cache Cache) *LastCommitCache {
|
||||||
if cache == nil {
|
if cache == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ func CacheRef(repo *models.Repository, gitRepo *git.Repository, fullRefName stri
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
commitCache := git.NewLastCommitCache(repo.FullName(), gitRepo, int64(setting.CacheService.LastCommit.TTL.Seconds()), cache.GetCache())
|
commitCache := git.NewLastCommitCache(repo.FullName(), gitRepo, setting.LastCommitCacheTTLSeconds, cache.GetCache())
|
||||||
|
|
||||||
return commitCache.CacheCommit(commit)
|
return commitCache.CacheCommit(commit)
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,9 @@ var (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MemcacheMaxTTL represents the maximum memcache TTL
|
||||||
|
const MemcacheMaxTTL = 30 * 24 * time.Hour
|
||||||
|
|
||||||
func newCacheService() {
|
func newCacheService() {
|
||||||
sec := Cfg.Section("cache")
|
sec := Cfg.Section("cache")
|
||||||
if err := sec.MapTo(&CacheService); err != nil {
|
if err := sec.MapTo(&CacheService); err != nil {
|
||||||
|
@ -85,3 +88,19 @@ func newCacheService() {
|
||||||
log.Info("Last Commit Cache Service Enabled")
|
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())
|
||||||
|
}
|
||||||
|
|
|
@ -140,7 +140,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
|
||||||
|
|
||||||
var c *git.LastCommitCache
|
var c *git.LastCommitCache
|
||||||
if setting.CacheService.LastCommit.Enabled && ctx.Repo.CommitsCount >= setting.CacheService.LastCommit.CommitsCount {
|
if setting.CacheService.LastCommit.Enabled && ctx.Repo.CommitsCount >= setting.CacheService.LastCommit.CommitsCount {
|
||||||
c = git.NewLastCommitCache(ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, int64(setting.CacheService.LastCommit.TTL.Seconds()), cache.GetCache())
|
c = git.NewLastCommitCache(ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, setting.LastCommitCacheTTLSeconds, cache.GetCache())
|
||||||
}
|
}
|
||||||
|
|
||||||
var latestCommit *git.Commit
|
var latestCommit *git.Commit
|
||||||
|
|
Loading…
Reference in a new issue