Skip to content

Commit e7cb8da

Browse files
authored
Always enable caches (#28527)
Nowadays, cache will be used on almost everywhere of Gitea and it cannot be disabled, otherwise some features will become unaviable. Then I think we can just remove the option for cache enable. That means cache cannot be disabled. But of course, we can still use cache configuration to set how should Gitea use the cache.
1 parent 4eb2a29 commit e7cb8da

File tree

14 files changed

+31
-82
lines changed

14 files changed

+31
-82
lines changed

custom/conf/app.example.ini

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,9 +1705,6 @@ LEVEL = Info
17051705
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17061706
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17071707
;;
1708-
;; if the cache enabled
1709-
;ENABLED = true
1710-
;;
17111708
;; Either "memory", "redis", "memcache", or "twoqueue". default is "memory"
17121709
;ADAPTER = memory
17131710
;;
@@ -1732,8 +1729,6 @@ LEVEL = Info
17321729
;[cache.last_commit]
17331730
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17341731
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1735-
;; if the cache enabled
1736-
;ENABLED = true
17371732
;;
17381733
;; Time to keep items in cache if not used, default is 8760 hours.
17391734
;; Setting it to -1 disables caching

docs/content/administration/config-cheat-sheet.en-us.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,6 @@ and
763763

764764
## Cache (`cache`)
765765

766-
- `ENABLED`: **true**: Enable the cache.
767766
- `ADAPTER`: **memory**: Cache engine adapter, either `memory`, `redis`, `redis-cluster`, `twoqueue` or `memcache`. (`twoqueue` represents a size limited LRU cache.)
768767
- `INTERVAL`: **60**: Garbage Collection interval (sec), for memory and twoqueue cache only.
769768
- `HOST`: **_empty_**: Connection string for `redis`, `redis-cluster` and `memcache`. For `twoqueue` sets configuration for the queue.
@@ -775,7 +774,6 @@ and
775774

776775
## Cache - LastCommitCache settings (`cache.last_commit`)
777776

778-
- `ENABLED`: **true**: Enable the cache.
779777
- `ITEM_TTL`: **8760h**: Time to keep items in cache if not used, Setting it to -1 disables caching.
780778
- `COMMITS_COUNT`: **1000**: Only enable the cache when repository's commits count great than.
781779

docs/content/administration/config-cheat-sheet.zh-cn.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,6 @@ Gitea 创建以下非唯一队列:
721721

722722
## 缓存 (`cache`)
723723

724-
- `ENABLED`: **true**: 是否启用缓存。
725724
- `ADAPTER`: **memory**: 缓存引擎,可以为 `memory`, `redis`, `redis-cluster`, `twoqueue``memcache`. (`twoqueue` 代表缓冲区固定的LRU缓存)
726725
- `INTERVAL`: **60**: 垃圾回收间隔(秒),只对`memory``towqueue`有效。
727726
- `HOST`: **_empty_**: 缓存配置。`redis`, `redis-cluster``memcache`配置连接字符串;`twoqueue` 设置队列参数
@@ -733,7 +732,6 @@ Gitea 创建以下非唯一队列:
733732

734733
### 缓存 - 最后提交缓存设置 (`cache.last_commit`)
735734

736-
- `ENABLED`: **true**:是否启用缓存。
737735
- `ITEM_TTL`: **8760h**:如果未使用,保持缓存中的项目的时间,将其设置为 -1 会禁用缓存。
738736
- `COMMITS_COUNT`: **1000**:仅在存储库的提交计数大于时启用缓存。
739737

modules/cache/cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
2424
})
2525
}
2626

27-
// NewContext start cache service
28-
func NewContext() error {
27+
// Init start cache service
28+
func Init() error {
2929
var err error
3030

31-
if conn == nil && setting.CacheService.Enabled {
31+
if conn == nil {
3232
if conn, err = newCache(setting.CacheService.Cache); err != nil {
3333
return err
3434
}

modules/cache/cache_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ func createTestCache() {
2222
}
2323

2424
func TestNewContext(t *testing.T) {
25-
assert.NoError(t, NewContext())
25+
assert.NoError(t, Init())
2626

27-
setting.CacheService.Cache = setting.Cache{Enabled: true, Adapter: "redis", Conn: "some random string"}
27+
setting.CacheService.Cache = setting.Cache{Adapter: "redis", Conn: "some random string"}
2828
con, err := newCache(setting.Cache{
2929
Adapter: "rand",
3030
Conn: "false conf",

modules/git/last_commit_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewLastCommitCache(count int64, repoPath string, gitRepo *Repository, cache
3939
if cache == nil {
4040
return nil
4141
}
42-
if !setting.CacheService.LastCommit.Enabled || count < setting.CacheService.LastCommit.CommitsCount {
42+
if count < setting.CacheService.LastCommit.CommitsCount {
4343
return nil
4444
}
4545

modules/setting/cache.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
// Cache represents cache settings
1414
type Cache struct {
15-
Enabled bool
1615
Adapter string
1716
Interval int
1817
Conn string
@@ -24,23 +23,19 @@ var CacheService = struct {
2423
Cache `ini:"cache"`
2524

2625
LastCommit struct {
27-
Enabled bool
2826
TTL time.Duration `ini:"ITEM_TTL"`
2927
CommitsCount int64
3028
} `ini:"cache.last_commit"`
3129
}{
3230
Cache: Cache{
33-
Enabled: true,
3431
Adapter: "memory",
3532
Interval: 60,
3633
TTL: 16 * time.Hour,
3734
},
3835
LastCommit: struct {
39-
Enabled bool
4036
TTL time.Duration `ini:"ITEM_TTL"`
4137
CommitsCount int64
4238
}{
43-
Enabled: true,
4439
TTL: 8760 * time.Hour,
4540
CommitsCount: 1000,
4641
},
@@ -65,30 +60,12 @@ func loadCacheFrom(rootCfg ConfigProvider) {
6560
if CacheService.Conn == "" {
6661
CacheService.Conn = "50000"
6762
}
68-
case "": // disable cache
69-
CacheService.Enabled = false
7063
default:
7164
log.Fatal("Unknown cache adapter: %s", CacheService.Adapter)
7265
}
7366

74-
if CacheService.Enabled {
75-
log.Info("Cache Service Enabled")
76-
} else {
77-
log.Warn("Cache Service Disabled so that captcha disabled too")
78-
// captcha depends on cache service
79-
Service.EnableCaptcha = false
80-
}
81-
8267
sec = rootCfg.Section("cache.last_commit")
83-
if !CacheService.Enabled {
84-
CacheService.LastCommit.Enabled = false
85-
}
86-
8768
CacheService.LastCommit.CommitsCount = sec.Key("COMMITS_COUNT").MustInt64(1000)
88-
89-
if CacheService.LastCommit.Enabled {
90-
log.Info("Last Commit Cache Service Enabled")
91-
}
9269
}
9370

9471
// TTLSeconds returns the TTLSeconds or unix timestamp for memcache

routers/api/v1/misc/nodeinfo.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ func NodeInfo(ctx *context.APIContext) {
2929

3030
nodeInfoUsage := structs.NodeInfoUsage{}
3131
if setting.Federation.ShareUserStatistics {
32-
cached := false
33-
if setting.CacheService.Enabled {
34-
nodeInfoUsage, cached = ctx.Cache.Get(cacheKeyNodeInfoUsage).(structs.NodeInfoUsage)
35-
}
32+
var cached bool
33+
nodeInfoUsage, cached = ctx.Cache.Get(cacheKeyNodeInfoUsage).(structs.NodeInfoUsage)
34+
3635
if !cached {
3736
usersTotal := int(user_model.CountUsers(ctx, nil))
3837
now := time.Now()
@@ -53,11 +52,10 @@ func NodeInfo(ctx *context.APIContext) {
5352
LocalPosts: int(allIssues),
5453
LocalComments: int(allComments),
5554
}
56-
if setting.CacheService.Enabled {
57-
if err := ctx.Cache.Put(cacheKeyNodeInfoUsage, nodeInfoUsage, 180); err != nil {
58-
ctx.InternalServerError(err)
59-
return
60-
}
55+
56+
if err := ctx.Cache.Put(cacheKeyNodeInfoUsage, nodeInfoUsage, 180); err != nil {
57+
ctx.InternalServerError(err)
58+
return
6159
}
6260
}
6361
}

routers/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func InitWebInstalled(ctx context.Context) {
118118
mustInit(storage.Init)
119119

120120
mailer.NewContext(ctx)
121-
mustInit(cache.NewContext)
121+
mustInit(cache.Init)
122122
mustInit(feed_service.Init)
123123
mustInit(uinotification.Init)
124124
mustInitCtx(ctx, archiver.Init)

routers/web/auth/auth.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,8 @@ func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.
622622
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale)
623623
ctx.HTML(http.StatusOK, TplActivate)
624624

625-
if setting.CacheService.Enabled {
626-
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
627-
log.Error("Set cache(MailResendLimit) fail: %v", err)
628-
}
625+
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
626+
log.Error("Set cache(MailResendLimit) fail: %v", err)
629627
}
630628
return false
631629
}
@@ -645,16 +643,14 @@ func Activate(ctx *context.Context) {
645643
}
646644
// Resend confirmation email.
647645
if setting.Service.RegisterEmailConfirm {
648-
if setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+ctx.Doer.LowerName) {
646+
if ctx.Cache.IsExist("MailResendLimit_" + ctx.Doer.LowerName) {
649647
ctx.Data["ResendLimited"] = true
650648
} else {
651649
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale)
652650
mailer.SendActivateAccountMail(ctx.Locale, ctx.Doer)
653651

654-
if setting.CacheService.Enabled {
655-
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
656-
log.Error("Set cache(MailResendLimit) fail: %v", err)
657-
}
652+
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
653+
log.Error("Set cache(MailResendLimit) fail: %v", err)
658654
}
659655
}
660656
} else {
@@ -789,7 +785,7 @@ func ActivateEmail(ctx *context.Context) {
789785

790786
if u, err := user_model.GetUserByID(ctx, email.UID); err != nil {
791787
log.Warn("GetUserByID: %d", email.UID)
792-
} else if setting.CacheService.Enabled {
788+
} else {
793789
// Allow user to validate more emails
794790
_ = ctx.Cache.Delete("MailResendLimit_" + u.LowerName)
795791
}

routers/web/auth/password.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,16 @@ func ForgotPasswdPost(ctx *context.Context) {
7979
return
8080
}
8181

82-
if setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+u.LowerName) {
82+
if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
8383
ctx.Data["ResendLimited"] = true
8484
ctx.HTML(http.StatusOK, tplForgotPassword)
8585
return
8686
}
8787

8888
mailer.SendResetPasswordMail(u)
8989

90-
if setting.CacheService.Enabled {
91-
if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
92-
log.Error("Set cache(MailResendLimit) fail: %v", err)
93-
}
90+
if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
91+
log.Error("Set cache(MailResendLimit) fail: %v", err)
9492
}
9593

9694
ctx.Data["ResetPwdCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale)

routers/web/healthcheck/check.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ func checkDatabase(ctx context.Context, checks checks) status {
121121

122122
// cache checks gitea cache status
123123
func checkCache(checks checks) status {
124-
if !setting.CacheService.Enabled {
125-
return pass
126-
}
127-
128124
st := componentStatus{}
129125
if err := cache.GetCache().Ping(); err != nil {
130126
st.Status = fail

routers/web/user/setting/account.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func EmailPost(ctx *context.Context) {
105105
// Send activation Email
106106
if ctx.FormString("_method") == "SENDACTIVATION" {
107107
var address string
108-
if setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+ctx.Doer.LowerName) {
108+
if ctx.Cache.IsExist("MailResendLimit_" + ctx.Doer.LowerName) {
109109
log.Error("Send activation: activation still pending")
110110
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
111111
return
@@ -141,11 +141,10 @@ func EmailPost(ctx *context.Context) {
141141
}
142142
address = email.Email
143143

144-
if setting.CacheService.Enabled {
145-
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
146-
log.Error("Set cache(MailResendLimit) fail: %v", err)
147-
}
144+
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
145+
log.Error("Set cache(MailResendLimit) fail: %v", err)
148146
}
147+
149148
ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", address, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale)))
150149
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
151150
return
@@ -204,11 +203,10 @@ func EmailPost(ctx *context.Context) {
204203
// Send confirmation email
205204
if setting.Service.RegisterEmailConfirm {
206205
mailer.SendActivateEmailMail(ctx.Doer, email)
207-
if setting.CacheService.Enabled {
208-
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
209-
log.Error("Set cache(MailResendLimit) fail: %v", err)
210-
}
206+
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
207+
log.Error("Set cache(MailResendLimit) fail: %v", err)
211208
}
209+
212210
ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale)))
213211
} else {
214212
ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
@@ -276,7 +274,7 @@ func loadAccountData(ctx *context.Context) {
276274
user_model.EmailAddress
277275
CanBePrimary bool
278276
}
279-
pendingActivation := setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+ctx.Doer.LowerName)
277+
pendingActivation := ctx.Cache.IsExist("MailResendLimit_" + ctx.Doer.LowerName)
280278
emails := make([]*UserEmail, len(emlist))
281279
for i, em := range emlist {
282280
var email UserEmail

services/repository/cache.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,10 @@ import (
99
repo_model "code.gitea.io/gitea/models/repo"
1010
"code.gitea.io/gitea/modules/cache"
1111
"code.gitea.io/gitea/modules/git"
12-
"code.gitea.io/gitea/modules/setting"
1312
)
1413

1514
// CacheRef cachhe last commit information of the branch or the tag
1615
func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, fullRefName git.RefName) error {
17-
if !setting.CacheService.LastCommit.Enabled {
18-
return nil
19-
}
20-
2116
commit, err := gitRepo.GetCommit(fullRefName.String())
2217
if err != nil {
2318
return err

0 commit comments

Comments
 (0)