Skip to content

Ensure memcache TTL cannot be over 30 days #14592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions modules/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func GetString(key string, getFunc func() (string, error)) (string, error) {
if value, err = getFunc(); err != nil {
return value, err
}
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
if err != nil {
return "", err
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func GetInt(key string, getFunc func() (int, error)) (int, error) {
if value, err = getFunc(); err != nil {
return value, err
}
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
if value, err = getFunc(); err != nil {
return value, err
}
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion modules/git/last_commit_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ func (c *LastCommitCache) getCacheKey(repoPath, ref, entryPath string) string {
// Put put the last commit id with commit and entry path
func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
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())
}
4 changes: 2 additions & 2 deletions modules/git/last_commit_cache_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
// LastCommitCache represents a cache to store last commit
type LastCommitCache struct {
repoPath string
ttl int64
ttl func() int64
repo *Repository
commitCache map[string]*object.Commit
cache Cache
}

// 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 {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions modules/git/last_commit_cache_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
// LastCommitCache represents a cache to store last commit
type LastCommitCache struct {
repoPath string
ttl int64
ttl func() int64
repo *Repository
commitCache map[string]*Commit
cache Cache
}

// 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 {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion modules/repository/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func CacheRef(repo *models.Repository, gitRepo *git.Repository, fullRefName stri
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)
}
19 changes: 19 additions & 0 deletions modules/setting/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion routers/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {

var c *git.LastCommitCache
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
Expand Down