Skip to content
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

Simplify last commit cache #4

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
12 changes: 1 addition & 11 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ DEFAULT_CLOSE_ISSUES_VIA_COMMITS_IN_ANY_BRANCH = false
ENABLE_PUSH_CREATE_USER = false
ENABLE_PUSH_CREATE_ORG = false
; Comma separated list of globally disabled repo units. Allowed values: repo.issues, repo.ext_issues, repo.pulls, repo.wiki, repo.ext_wiki
DISABLED_REPO_UNITS =
DISABLED_REPO_UNITS =
; Comma separated list of default repo units. Allowed values: repo.code, repo.releases, repo.issues, repo.pulls, repo.wiki.
; Note: Code and Releases can currently not be deactivated. If you specify default repo units you should still list them for future compatibility.
; External wiki and issue tracker can't be enabled by default as it requires additional settings.
Expand Down Expand Up @@ -650,16 +650,6 @@ ITEM_TTL = 16h
[cache.last_commit]
; if the cache enabled
lafriks marked this conversation as resolved.
Show resolved Hide resolved
ENABLED = true
; if use the default cache, by pass all the next options
USE_SEPERATE_CACHE = false
; Either "memory", "redis", or "memcache", default is "memory"
ADAPTER = memory
; For "memory" only, GC interval in seconds, default is 60
INTERVAL = 60
; For "redis" and "memcache", connection host address
; redis: network=tcp,addr=:6379,password=macaron,db=0,pool_size=100,idle_timeout=180
; memcache: `127.0.0.1:11211`
HOST =
; Time to keep items in cache if not used, default is 16 hours.
; Setting it to 0 disables caching
ITEM_TTL = 16h
Expand Down
6 changes: 0 additions & 6 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,6 @@ relation to port exhaustion.
## Cache - LastCommitCache settings (`cache.last_commit`)

- `ENABLED`: **true**: Enable the cache.
- `USE_SEPERATE_CACHE`: **false** If use a seperate cache setting, `false` will bypass all the next options.
- `ADAPTER`: **memory**: Cache engine adapter, either `memory`, `redis`, or `memcache`.
- `INTERVAL`: **60**: Garbage Collection interval (sec), for memory cache only.
- `HOST`: **\<empty\>**: Connection string for `redis` and `memcache`.
- Redis: `network=tcp,addr=127.0.0.1:6379,password=macaron,db=0,pool_size=100,idle_timeout=180`
- Memcache: `127.0.0.1:9090;127.0.0.1:9091`
- `ITEM_TTL`: **16h**: Time to keep items in cache if not used, Setting it to 0 disables caching.
- `COMMITS_COUNT`: **1000**: Only enable the cache when repository's commits count great than.

Expand Down
6 changes: 0 additions & 6 deletions docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,6 @@ menu:
## Cache - LastCommitCache settings (`cache.last_commit`)

- `ENABLED`: **true**: 是否启用。
- `USE_SEPERATE_CACHE`: **false** 是否启用独立的Cache设置,如果为否,则以下的Cache设置无效。
- `ADAPTER`: **memory**: 缓存引擎,可以为 `memory`, `redis` 或 `memcache`。
- `INTERVAL`: **60**: 只对内存缓存有效,GC间隔,单位秒。
- `HOST`: **\<empty\>**: 针对redis和memcache有效,主机地址和端口。
- Redis: `network=tcp,addr=127.0.0.1:6379,password=macaron,db=0,pool_size=100,idle_timeout=180`
- Memache: `127.0.0.1:9090;127.0.0.1:9091`
- `ITEM_TTL`: **86400h**: 缓存项目失效时间,设置为 0 则禁用缓存。
- `COMMITS_COUNT`: **1000**: 仅当仓库的提交数大于时才启用缓存。

Expand Down
9 changes: 1 addition & 8 deletions modules/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
)

var (
conn mc.Cache
lastCommitCache mc.Cache
conn mc.Cache
)

func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
Expand All @@ -39,12 +38,6 @@ func NewContext() error {
}
}

if lastCommitCache == nil && setting.CacheService.LastCommit.Enabled {
if lastCommitCache, err = newCache(setting.CacheService.LastCommit.Cache); err != nil {
return err
}
}

return err
}

Expand Down
6 changes: 3 additions & 3 deletions modules/cache/last_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func NewLastCommitCache(repoPath string, gitRepo *git.Repository, ttl int64) *La
repo: gitRepo,
commitCache: make(map[string]*object.Commit),
ttl: ttl,
Cache: lastCommitCache,
Cache: conn,
}
}

// Get get the last commit information by commit id and entry path
func (c LastCommitCache) Get(ref, entryPath string) (*object.Commit, error) {
v := c.Cache.Get(fmt.Sprintf("%s:%s:%s", c.repoPath, ref, entryPath))
v := c.Cache.Get(fmt.Sprintf("last_commit:%s:%s:%s", c.repoPath, ref, entryPath))
if vs, ok := v.(string); ok {
log.Trace("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
if commit, ok := c.commitCache[vs]; ok {
Expand All @@ -60,5 +60,5 @@ func (c LastCommitCache) Get(ref, entryPath string) (*object.Commit, error) {
// Put put the last commit id with commit and entry path
func (c LastCommitCache) Put(ref, entryPath, commitID string) error {
log.Trace("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
return c.Cache.Put(fmt.Sprintf("%s:%s:%s", c.repoPath, ref, entryPath), commitID, c.ttl)
return c.Cache.Put(fmt.Sprintf("last_commit:%s:%s:%s", c.repoPath, ref, entryPath), commitID, c.ttl)
}
38 changes: 11 additions & 27 deletions modules/setting/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ var (
Cache

LastCommit struct {
Cache
UseSeperateCache bool
CommitsCount int64
Enabled bool
TTL time.Duration `ini:"ITEM_TTL"`
CommitsCount int64
} `ini:"cache.last_commit"`
}{
Cache: Cache{
Expand All @@ -38,18 +38,13 @@ var (
TTL: 16 * time.Hour,
},
LastCommit: struct {
Cache
UseSeperateCache bool
CommitsCount int64
Enabled bool
TTL time.Duration `ini:"ITEM_TTL"`
CommitsCount int64
}{
Cache: Cache{
Enabled: true,
Adapter: "memory",
Interval: 60,
TTL: 86400 * time.Hour,
},
UseSeperateCache: false,
CommitsCount: 1000,
Enabled: true,
TTL: 16 * time.Hour,
CommitsCount: 1000,
},
}
)
Expand All @@ -76,19 +71,8 @@ func newCacheService() {
}

sec = Cfg.Section("cache.last_commit")
if !CacheService.LastCommit.UseSeperateCache {
CacheService.LastCommit.Cache = CacheService.Cache
} else {
CacheService.LastCommit.Adapter = sec.Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"})
switch CacheService.LastCommit.Adapter {
case "memory":
case "redis", "memcache":
CacheService.LastCommit.Conn = strings.Trim(sec.Key("HOST").String(), "\" ")
case "": // disable cache
CacheService.LastCommit.Enabled = false
default:
log.Fatal("Unknown cache.last_commit adapter: %s", CacheService.LastCommit.Adapter)
}
if !CacheService.Enabled {
CacheService.LastCommit.Enabled = false
}

CacheService.LastCommit.CommitsCount = sec.Key("COMMITS_COUNT").MustInt64(1000)
Expand Down