Skip to content

Commit c21b16a

Browse files
lafrikslunny
authored andcommitted
Simplify last commit cache
1 parent ce3d707 commit c21b16a

File tree

7 files changed

+18
-76
lines changed

7 files changed

+18
-76
lines changed

custom/conf/app.ini.sample

+1-15
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ DEFAULT_CLOSE_ISSUES_VIA_COMMITS_IN_ANY_BRANCH = false
4343
ENABLE_PUSH_CREATE_USER = false
4444
ENABLE_PUSH_CREATE_ORG = false
4545
; Comma separated list of globally disabled repo units. Allowed values: repo.issues, repo.ext_issues, repo.pulls, repo.wiki, repo.ext_wiki
46-
DISABLED_REPO_UNITS =
46+
DISABLED_REPO_UNITS =
4747
; Comma separated list of default repo units. Allowed values: repo.code, repo.releases, repo.issues, repo.pulls, repo.wiki.
4848
; Note: Code and Releases can currently not be deactivated. If you specify default repo units you should still list them for future compatibility.
4949
; External wiki and issue tracker can't be enabled by default as it requires additional settings.
@@ -632,8 +632,6 @@ SENDMAIL_PATH = sendmail
632632
SENDMAIL_ARGS =
633633

634634
[cache]
635-
; if the cache enabled
636-
ENABLED = true
637635
; Either "memory", "redis", or "memcache", default is "memory"
638636
ADAPTER = memory
639637
; For "memory" only, GC interval in seconds, default is 60
@@ -648,18 +646,6 @@ ITEM_TTL = 16h
648646

649647
; Last commit cache
650648
[cache.last_commit]
651-
; if the cache enabled
652-
ENABLED = true
653-
; if use the default cache, by pass all the next options
654-
USE_SEPERATE_CACHE = false
655-
; Either "memory", "redis", or "memcache", default is "memory"
656-
ADAPTER = memory
657-
; For "memory" only, GC interval in seconds, default is 60
658-
INTERVAL = 60
659-
; For "redis" and "memcache", connection host address
660-
; redis: network=tcp,addr=:6379,password=macaron,db=0,pool_size=100,idle_timeout=180
661-
; memcache: `127.0.0.1:11211`
662-
HOST =
663649
; Time to keep items in cache if not used, default is 16 hours.
664650
; Setting it to 0 disables caching
665651
ITEM_TTL = 16h

docs/content/doc/advanced/config-cheat-sheet.en-us.md

-8
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ relation to port exhaustion.
383383

384384
## Cache (`cache`)
385385

386-
- `ENABLED`: **true**: Enable the cache.
387386
- `ADAPTER`: **memory**: Cache engine adapter, either `memory`, `redis`, or `memcache`.
388387
- `INTERVAL`: **60**: Garbage Collection interval (sec), for memory cache only.
389388
- `HOST`: **\<empty\>**: Connection string for `redis` and `memcache`.
@@ -393,13 +392,6 @@ relation to port exhaustion.
393392

394393
## Cache - LastCommitCache settings (`cache.last_commit`)
395394

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

docs/content/doc/advanced/config-cheat-sheet.zh-cn.md

-8
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ menu:
148148

149149
## Cache (`cache`)
150150

151-
- `ENABLED`: **true**: 是否启用。
152151
- `ADAPTER`: **memory**: 缓存引擎,可以为 `memory`, `redis``memcache`
153152
- `INTERVAL`: **60**: 只对内存缓存有效,GC间隔,单位秒。
154153
- `HOST`: **\<empty\>**: 针对redis和memcache有效,主机地址和端口。
@@ -158,13 +157,6 @@ menu:
158157

159158
## Cache - LastCommitCache settings (`cache.last_commit`)
160159

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

modules/cache/cache.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import (
1717
)
1818

1919
var (
20-
conn mc.Cache
21-
lastCommitCache mc.Cache
20+
conn mc.Cache
2221
)
2322

2423
func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
@@ -33,18 +32,12 @@ func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
3332
func NewContext() error {
3433
var err error
3534

36-
if conn == nil && setting.CacheService.Enabled {
35+
if conn == nil && setting.CacheService.TTL > 0 {
3736
if conn, err = newCache(setting.CacheService.Cache); err != nil {
3837
return err
3938
}
4039
}
4140

42-
if lastCommitCache == nil && setting.CacheService.LastCommit.Enabled {
43-
if lastCommitCache, err = newCache(setting.CacheService.LastCommit.Cache); err != nil {
44-
return err
45-
}
46-
}
47-
4841
return err
4942
}
5043

modules/cache/last_commit.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ func NewLastCommitCache(repoPath string, gitRepo *git.Repository, ttl int64) *La
3030
repo: gitRepo,
3131
commitCache: make(map[string]*object.Commit),
3232
ttl: ttl,
33-
Cache: lastCommitCache,
33+
Cache: conn,
3434
}
3535
}
3636

3737
// Get get the last commit information by commit id and entry path
3838
func (c LastCommitCache) Get(ref, entryPath string) (*object.Commit, error) {
39-
v := c.Cache.Get(fmt.Sprintf("%s:%s:%s", c.repoPath, ref, entryPath))
39+
v := c.Cache.Get(fmt.Sprintf("lastcommit:%s:%s:%s", c.repoPath, ref, entryPath))
4040
if vs, ok := v.(string); ok {
4141
log.Trace("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
4242
if commit, ok := c.commitCache[vs]; ok {
@@ -60,5 +60,5 @@ func (c LastCommitCache) Get(ref, entryPath string) (*object.Commit, error) {
6060
// Put put the last commit id with commit and entry path
6161
func (c LastCommitCache) Put(ref, entryPath, commitID string) error {
6262
log.Trace("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
63-
return c.Cache.Put(fmt.Sprintf("%s:%s:%s", c.repoPath, ref, entryPath), commitID, c.ttl)
63+
return c.Cache.Put(fmt.Sprintf("lastcommit:%s:%s:%s", c.repoPath, ref, entryPath), commitID, c.ttl)
6464
}

modules/setting/cache.go

+11-32
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
// Cache represents cache settings
1515
type Cache struct {
16-
Enabled bool
1716
Adapter string
1817
Interval int
1918
Conn string
@@ -26,30 +25,21 @@ var (
2625
Cache
2726

2827
LastCommit struct {
29-
Cache
30-
UseSeperateCache bool
31-
CommitsCount int64
28+
TTL time.Duration `ini:"ITEM_TTL"`
29+
CommitsCount int64
3230
} `ini:"cache.last_commit"`
3331
}{
3432
Cache: Cache{
35-
Enabled: true,
3633
Adapter: "memory",
3734
Interval: 60,
3835
TTL: 16 * time.Hour,
3936
},
4037
LastCommit: struct {
41-
Cache
42-
UseSeperateCache bool
43-
CommitsCount int64
38+
TTL time.Duration `ini:"ITEM_TTL"`
39+
CommitsCount int64
4440
}{
45-
Cache: Cache{
46-
Enabled: true,
47-
Adapter: "memory",
48-
Interval: 60,
49-
TTL: 86400 * time.Hour,
50-
},
51-
UseSeperateCache: false,
52-
CommitsCount: 1000,
41+
TTL: 16 * time.Hour,
42+
CommitsCount: 1000,
5343
},
5444
}
5545
)
@@ -66,34 +56,23 @@ func newCacheService() {
6656
case "redis", "memcache":
6757
CacheService.Conn = strings.Trim(sec.Key("HOST").String(), "\" ")
6858
case "": // disable cache
69-
CacheService.Enabled = false
59+
CacheService.TTL = 0
7060
default:
7161
log.Fatal("Unknown cache adapter: %s", CacheService.Adapter)
7262
}
7363

74-
if CacheService.Enabled {
64+
if CacheService.TTL > 0 {
7565
log.Info("Cache Service Enabled")
7666
}
7767

7868
sec = Cfg.Section("cache.last_commit")
79-
if !CacheService.LastCommit.UseSeperateCache {
80-
CacheService.LastCommit.Cache = CacheService.Cache
81-
} else {
82-
CacheService.LastCommit.Adapter = sec.Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"})
83-
switch CacheService.LastCommit.Adapter {
84-
case "memory":
85-
case "redis", "memcache":
86-
CacheService.LastCommit.Conn = strings.Trim(sec.Key("HOST").String(), "\" ")
87-
case "": // disable cache
88-
CacheService.LastCommit.Enabled = false
89-
default:
90-
log.Fatal("Unknown cache.last_commit adapter: %s", CacheService.LastCommit.Adapter)
91-
}
69+
if CacheService.TTL == 0 {
70+
CacheService.LastCommit.TTL = 0
9271
}
9372

9473
CacheService.LastCommit.CommitsCount = sec.Key("COMMITS_COUNT").MustInt64(1000)
9574

96-
if CacheService.LastCommit.Enabled {
75+
if CacheService.LastCommit.TTL > 0 {
9776
log.Info("Last Commit Cache Service Enabled")
9877
}
9978
}

routers/repo/view.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
5151
entries.CustomSort(base.NaturalSortLess)
5252

5353
var c git.LastCommitCache
54-
if setting.CacheService.LastCommit.Enabled && ctx.Repo.CommitsCount >= setting.CacheService.LastCommit.CommitsCount {
54+
if setting.CacheService.LastCommit.TTL > 0 && ctx.Repo.CommitsCount >= setting.CacheService.LastCommit.CommitsCount {
5555
c = cache.NewLastCommitCache(ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, int64(setting.CacheService.LastCommit.TTL.Seconds()))
5656
}
5757

0 commit comments

Comments
 (0)