Skip to content

Commit 30f7ddb

Browse files
authored
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 <[email protected]>
1 parent 3a4801d commit 30f7ddb

File tree

7 files changed

+29
-10
lines changed

7 files changed

+29
-10
lines changed

modules/cache/cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func GetString(key string, getFunc func() (string, error)) (string, error) {
5858
if value, err = getFunc(); err != nil {
5959
return value, err
6060
}
61-
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
61+
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
6262
if err != nil {
6363
return "", err
6464
}
@@ -86,7 +86,7 @@ func GetInt(key string, getFunc func() (int, error)) (int, error) {
8686
if value, err = getFunc(); err != nil {
8787
return value, err
8888
}
89-
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
89+
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
9090
if err != nil {
9191
return 0, err
9292
}
@@ -118,7 +118,7 @@ func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
118118
if value, err = getFunc(); err != nil {
119119
return value, err
120120
}
121-
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
121+
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
122122
if err != nil {
123123
return 0, err
124124
}

modules/git/last_commit_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ func (c *LastCommitCache) getCacheKey(repoPath, ref, entryPath string) string {
2525
// Put put the last commit id with commit and entry path
2626
func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
2727
log("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
28-
return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl)
28+
return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl())
2929
}

modules/git/last_commit_cache_gogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import (
1616
// LastCommitCache represents a cache to store last commit
1717
type LastCommitCache struct {
1818
repoPath string
19-
ttl int64
19+
ttl func() int64
2020
repo *Repository
2121
commitCache map[string]*object.Commit
2222
cache Cache
2323
}
2424

2525
// NewLastCommitCache creates a new last commit cache for repo
26-
func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl int64, cache Cache) *LastCommitCache {
26+
func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64, cache Cache) *LastCommitCache {
2727
if cache == nil {
2828
return nil
2929
}

modules/git/last_commit_cache_nogogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313
// LastCommitCache represents a cache to store last commit
1414
type LastCommitCache struct {
1515
repoPath string
16-
ttl int64
16+
ttl func() int64
1717
repo *Repository
1818
commitCache map[string]*Commit
1919
cache Cache
2020
}
2121

2222
// NewLastCommitCache creates a new last commit cache for repo
23-
func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl int64, cache Cache) *LastCommitCache {
23+
func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64, cache Cache) *LastCommitCache {
2424
if cache == nil {
2525
return nil
2626
}

modules/repository/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func CacheRef(repo *models.Repository, gitRepo *git.Repository, fullRefName stri
4141
return nil
4242
}
4343

44-
commitCache := git.NewLastCommitCache(repo.FullName(), gitRepo, int64(setting.CacheService.LastCommit.TTL.Seconds()), cache.GetCache())
44+
commitCache := git.NewLastCommitCache(repo.FullName(), gitRepo, setting.LastCommitCacheTTLSeconds, cache.GetCache())
4545

4646
return commitCache.CacheCommit(commit)
4747
}

modules/setting/cache.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ var (
4949
}
5050
)
5151

52+
// MemcacheMaxTTL represents the maximum memcache TTL
53+
const MemcacheMaxTTL = 30 * 24 * time.Hour
54+
5255
func newCacheService() {
5356
sec := Cfg.Section("cache")
5457
if err := sec.MapTo(&CacheService); err != nil {
@@ -85,3 +88,19 @@ func newCacheService() {
8588
log.Info("Last Commit Cache Service Enabled")
8689
}
8790
}
91+
92+
// TTLSeconds returns the TTLSeconds or unix timestamp for memcache
93+
func (c Cache) TTLSeconds() int64 {
94+
if c.Adapter == "memcache" && c.TTL > MemcacheMaxTTL {
95+
return time.Now().Add(c.TTL).Unix()
96+
}
97+
return int64(c.TTL.Seconds())
98+
}
99+
100+
// LastCommitCacheTTLSeconds returns the TTLSeconds or unix timestamp for memcache
101+
func LastCommitCacheTTLSeconds() int64 {
102+
if CacheService.Adapter == "memcache" && CacheService.LastCommit.TTL > MemcacheMaxTTL {
103+
return time.Now().Add(CacheService.LastCommit.TTL).Unix()
104+
}
105+
return int64(CacheService.LastCommit.TTL.Seconds())
106+
}

routers/repo/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
140140

141141
var c *git.LastCommitCache
142142
if setting.CacheService.LastCommit.Enabled && ctx.Repo.CommitsCount >= setting.CacheService.LastCommit.CommitsCount {
143-
c = git.NewLastCommitCache(ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, int64(setting.CacheService.LastCommit.TTL.Seconds()), cache.GetCache())
143+
c = git.NewLastCommitCache(ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, setting.LastCommitCacheTTLSeconds, cache.GetCache())
144144
}
145145

146146
var latestCommit *git.Commit

0 commit comments

Comments
 (0)