Skip to content

Commit 343cded

Browse files
authored
Merge branch 'master' into unique-queues
2 parents 47d94a1 + 72f9cfc commit 343cded

File tree

20 files changed

+428
-115
lines changed

20 files changed

+428
-115
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,6 @@ prime/
8383
*.snap-build
8484
*_source.tar.bz2
8585
.DS_Store
86+
87+
# Make evidence files
88+
/.make_evidence

Makefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ GOFLAGS := -v
2929
EXTRA_GOFLAGS ?=
3030

3131
MAKE_VERSION := $(shell $(MAKE) -v | head -n 1)
32+
MAKE_EVIDENCE_DIR := .make_evidence
3233

3334
ifneq ($(RACE_ENABLED),)
3435
GOTESTFLAGS ?= -race
@@ -58,7 +59,10 @@ BINDATA_DEST := modules/public/bindata.go modules/options/bindata.go modules/tem
5859
BINDATA_HASH := $(addsuffix .hash,$(BINDATA_DEST))
5960

6061
WEBPACK_DEST_DIRS := public/js public/css
62+
63+
FOMANTIC_SOURCES ?= $(shell find web_src/fomantic -type f)
6164
FOMANTIC_DEST_DIR := public/fomantic
65+
FOMANTIC_EVIDENCE := $(MAKE_EVIDENCE_DIR)/fomantic
6266

6367
TAGS ?=
6468

@@ -139,7 +143,7 @@ node-check:
139143

140144
.PHONY: clean-all
141145
clean-all: clean
142-
rm -rf $(WEBPACK_DEST_DIRS) $(FOMANTIC_DEST_DIR)
146+
rm -rf $(WEBPACK_DEST_DIRS) $(FOMANTIC_DEST_DIR) $(FOMANTIC_EVIDENCE)
143147

144148
.PHONY: clean
145149
clean:
@@ -498,18 +502,18 @@ css:
498502
$(MAKE) webpack
499503

500504
.PHONY: fomantic
501-
fomantic: node-check $(FOMANTIC_DEST_DIR)
505+
fomantic: node-check $(FOMANTIC_EVIDENCE)
502506

503-
$(FOMANTIC_DEST_DIR): semantic.json web_src/fomantic/theme.config.less | node_modules
507+
$(FOMANTIC_EVIDENCE): semantic.json $(FOMANTIC_SOURCES) | node_modules
504508
cp web_src/fomantic/theme.config.less node_modules/fomantic-ui/src/theme.config
505509
cp web_src/fomantic/_site/globals/* node_modules/fomantic-ui/src/_site/globals/
506510
npx gulp -f node_modules/fomantic-ui/gulpfile.js build
507-
@touch $(FOMANTIC_DEST_DIR)
511+
@mkdir -p $(MAKE_EVIDENCE_DIR) && touch $(FOMANTIC_EVIDENCE)
508512

509513
.PHONY: webpack
510514
webpack: node-check $(WEBPACK_DEST)
511515

512-
$(WEBPACK_DEST): $(WEBPACK_SOURCES) | node_modules
516+
$(WEBPACK_DEST): $(WEBPACK_SOURCES) webpack.config.js | node_modules
513517
npx eslint web_src/js webpack.config.js
514518
npx stylelint web_src/less
515519
npx webpack --hide-modules --display-entrypoints=false

custom/conf/app.ini.sample

Lines changed: 13 additions & 1 deletion
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,6 +632,8 @@ SENDMAIL_PATH = sendmail
632632
SENDMAIL_ARGS =
633633

634634
[cache]
635+
; if the cache enabled
636+
ENABLED = true
635637
; Either "memory", "redis", or "memcache", default is "memory"
636638
ADAPTER = memory
637639
; For "memory" only, GC interval in seconds, default is 60
@@ -644,6 +646,16 @@ HOST =
644646
; Setting it to 0 disables caching
645647
ITEM_TTL = 16h
646648

649+
; Last commit cache
650+
[cache.last_commit]
651+
; if the cache enabled
652+
ENABLED = true
653+
; Time to keep items in cache if not used, default is 8760 hours.
654+
; Setting it to 0 disables caching
655+
ITEM_TTL = 8760h
656+
; Only enable the cache when repository's commits count great than
657+
COMMITS_COUNT = 1000
658+
647659
[session]
648660
; Either "memory", "file", or "redis", default is "memory"
649661
PROVIDER = memory

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,20 @@ set name for unique queues. Individual queues will default to
387387

388388
## Cache (`cache`)
389389

390+
- `ENABLED`: **true**: Enable the cache.
390391
- `ADAPTER`: **memory**: Cache engine adapter, either `memory`, `redis`, or `memcache`.
391392
- `INTERVAL`: **60**: Garbage Collection interval (sec), for memory cache only.
392393
- `HOST`: **\<empty\>**: Connection string for `redis` and `memcache`.
393394
- Redis: `network=tcp,addr=127.0.0.1:6379,password=macaron,db=0,pool_size=100,idle_timeout=180`
394395
- Memcache: `127.0.0.1:9090;127.0.0.1:9091`
395396
- `ITEM_TTL`: **16h**: Time to keep items in cache if not used, Setting it to 0 disables caching.
396397

398+
## Cache - LastCommitCache settings (`cache.last_commit`)
399+
400+
- `ENABLED`: **true**: Enable the cache.
401+
- `ITEM_TTL`: **8760h**: Time to keep items in cache if not used, Setting it to 0 disables caching.
402+
- `COMMITS_COUNT`: **1000**: Only enable the cache when repository's commits count great than.
403+
397404
## Session (`session`)
398405

399406
- `PROVIDER`: **memory**: Session engine provider \[memory, file, redis, mysql, couchbase, memcache, nodb, postgres\].

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,20 @@ menu:
148148

149149
## Cache (`cache`)
150150

151+
- `ENABLED`: **true**: 是否启用。
151152
- `ADAPTER`: **memory**: 缓存引擎,可以为 `memory`, `redis``memcache`
152153
- `INTERVAL`: **60**: 只对内存缓存有效,GC间隔,单位秒。
153154
- `HOST`: **\<empty\>**: 针对redis和memcache有效,主机地址和端口。
154155
- Redis: `network=tcp,addr=127.0.0.1:6379,password=macaron,db=0,pool_size=100,idle_timeout=180`
155156
- Memache: `127.0.0.1:9090;127.0.0.1:9091`
156157
- `ITEM_TTL`: **16h**: 缓存项目失效时间,设置为 0 则禁用缓存。
157158

159+
## Cache - LastCommitCache settings (`cache.last_commit`)
160+
161+
- `ENABLED`: **true**: 是否启用。
162+
- `ITEM_TTL`: **8760h**: 缓存项目失效时间,设置为 0 则禁用缓存。
163+
- `COMMITS_COUNT`: **1000**: 仅当仓库的提交数大于时才启用缓存。
164+
158165
## Session (`session`)
159166

160167
- `PROVIDER`: Session 内容存储方式,可选 `memory`, `file`, `redis``mysql`

integrations/repo_test.go

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ package integrations
77
import (
88
"fmt"
99
"net/http"
10+
"path"
1011
"strings"
1112
"testing"
13+
"time"
1214

1315
"code.gitea.io/gitea/modules/setting"
1416

@@ -29,12 +31,71 @@ func TestViewRepo(t *testing.T) {
2931
session.MakeRequest(t, req, http.StatusNotFound)
3032
}
3133

32-
func TestViewRepo2(t *testing.T) {
34+
func testViewRepo(t *testing.T) {
3335
defer prepareTestEnv(t)()
3436

3537
req := NewRequest(t, "GET", "/user3/repo3")
3638
session := loginUser(t, "user2")
37-
session.MakeRequest(t, req, http.StatusOK)
39+
resp := session.MakeRequest(t, req, http.StatusOK)
40+
41+
htmlDoc := NewHTMLParser(t, resp.Body)
42+
files := htmlDoc.doc.Find("#repo-files-table > TBODY > TR")
43+
44+
type file struct {
45+
fileName string
46+
commitID string
47+
commitMsg string
48+
commitTime string
49+
}
50+
51+
var items []file
52+
53+
files.Each(func(i int, s *goquery.Selection) {
54+
tds := s.Find("td")
55+
var f file
56+
tds.Each(func(i int, s *goquery.Selection) {
57+
if i == 0 {
58+
f.fileName = strings.TrimSpace(s.Text())
59+
} else if i == 1 {
60+
a := s.Find("a")
61+
f.commitMsg = strings.TrimSpace(a.Text())
62+
l, _ := a.Attr("href")
63+
f.commitID = path.Base(l)
64+
}
65+
})
66+
67+
f.commitTime, _ = s.Find("span.time-since").Attr("title")
68+
items = append(items, f)
69+
})
70+
71+
assert.EqualValues(t, []file{
72+
{
73+
fileName: "doc",
74+
commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
75+
commitMsg: "init project",
76+
commitTime: time.Date(2017, time.June, 14, 13, 54, 21, 0, time.UTC).Format(time.RFC1123),
77+
},
78+
{
79+
fileName: "README.md",
80+
commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
81+
commitMsg: "init project",
82+
commitTime: time.Date(2017, time.June, 14, 13, 54, 21, 0, time.UTC).Format(time.RFC1123),
83+
},
84+
}, items)
85+
}
86+
87+
func TestViewRepo2(t *testing.T) {
88+
// no last commit cache
89+
testViewRepo(t)
90+
91+
// enable last commit cache for all repositories
92+
oldCommitsCount := setting.CacheService.LastCommit.CommitsCount
93+
setting.CacheService.LastCommit.CommitsCount = 0
94+
// first view will not hit the cache
95+
testViewRepo(t)
96+
// second view will hit the cache
97+
testViewRepo(t)
98+
setting.CacheService.LastCommit.CommitsCount = oldCommitsCount
3899
}
39100

40101
func TestViewRepo3(t *testing.T) {

modules/cache/cache.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,28 @@ import (
1616
_ "gitea.com/macaron/cache/redis"
1717
)
1818

19-
var conn mc.Cache
19+
var (
20+
conn mc.Cache
21+
)
22+
23+
func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
24+
return mc.NewCacher(cacheConfig.Adapter, mc.Options{
25+
Adapter: cacheConfig.Adapter,
26+
AdapterConfig: cacheConfig.Conn,
27+
Interval: cacheConfig.Interval,
28+
})
29+
}
2030

2131
// NewContext start cache service
2232
func NewContext() error {
23-
if setting.CacheService == nil || conn != nil {
24-
return nil
33+
var err error
34+
35+
if conn == nil && setting.CacheService.Enabled {
36+
if conn, err = newCache(setting.CacheService.Cache); err != nil {
37+
return err
38+
}
2539
}
2640

27-
var err error
28-
conn, err = mc.NewCacher(setting.CacheService.Adapter, mc.Options{
29-
Adapter: setting.CacheService.Adapter,
30-
AdapterConfig: setting.CacheService.Conn,
31-
Interval: setting.CacheService.Interval,
32-
})
3341
return err
3442
}
3543

modules/cache/last_commit.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package cache
6+
7+
import (
8+
"fmt"
9+
10+
"code.gitea.io/gitea/modules/git"
11+
"code.gitea.io/gitea/modules/log"
12+
13+
mc "gitea.com/macaron/cache"
14+
"gopkg.in/src-d/go-git.v4/plumbing/object"
15+
)
16+
17+
// LastCommitCache represents a cache to store last commit
18+
type LastCommitCache struct {
19+
repoPath string
20+
ttl int64
21+
repo *git.Repository
22+
commitCache map[string]*object.Commit
23+
mc.Cache
24+
}
25+
26+
// NewLastCommitCache creates a new last commit cache for repo
27+
func NewLastCommitCache(repoPath string, gitRepo *git.Repository, ttl int64) *LastCommitCache {
28+
return &LastCommitCache{
29+
repoPath: repoPath,
30+
repo: gitRepo,
31+
commitCache: make(map[string]*object.Commit),
32+
ttl: ttl,
33+
Cache: conn,
34+
}
35+
}
36+
37+
// Get get the last commit information by commit id and entry path
38+
func (c LastCommitCache) Get(ref, entryPath string) (*object.Commit, error) {
39+
v := c.Cache.Get(fmt.Sprintf("last_commit:%s:%s:%s", c.repoPath, ref, entryPath))
40+
if vs, ok := v.(string); ok {
41+
log.Trace("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
42+
if commit, ok := c.commitCache[vs]; ok {
43+
log.Trace("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
44+
return commit, nil
45+
}
46+
id, err := c.repo.ConvertToSHA1(vs)
47+
if err != nil {
48+
return nil, err
49+
}
50+
commit, err := c.repo.GoGitRepo().CommitObject(id)
51+
if err != nil {
52+
return nil, err
53+
}
54+
c.commitCache[vs] = commit
55+
return commit, nil
56+
}
57+
return nil, nil
58+
}
59+
60+
// Put put the last commit id with commit and entry path
61+
func (c LastCommitCache) Put(ref, entryPath, commitID string) error {
62+
log.Trace("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
63+
return c.Cache.Put(fmt.Sprintf("last_commit:%s:%s:%s", c.repoPath, ref, entryPath), commitID, c.ttl)
64+
}

modules/git/cache.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
package git
66

7+
import "gopkg.in/src-d/go-git.v4/plumbing/object"
8+
79
// LastCommitCache cache
810
type LastCommitCache interface {
9-
Get(repoPath, ref, entryPath string) (*Commit, error)
10-
Put(repoPath, ref, entryPath string, commit *Commit) error
11+
Get(ref, entryPath string) (*object.Commit, error)
12+
Put(ref, entryPath, commitID string) error
1113
}

0 commit comments

Comments
 (0)