Skip to content

Commit faaf672

Browse files
committed
Add configuratuion option to allow to change caching time or disable it
1 parent 89b8156 commit faaf672

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

conf/app.ini

+3
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ INTERVAL = 60
337337
; redis: network=tcp,addr=:6379,password=macaron,db=0,pool_size=100,idle_timeout=180
338338
; memcache: `127.0.0.1:11211`
339339
HOST =
340+
; Time to keep items in cache if not used, default is 16 hours.
341+
; Setting it to 0 disables caching
342+
ITEM_TTL = 16h
340343

341344
[session]
342345
; Either "memory", "file", or "redis", default is "memory"

modules/cache/cache.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package cache
66

77
import (
8-
"time"
9-
108
"code.gitea.io/gitea/modules/setting"
119

1210
mc "github.com/go-macaron/cache"
@@ -31,7 +29,7 @@ func NewContext() error {
3129

3230
// GetInt returns key value from cache with callback when no key exists in cache
3331
func GetInt(key string, getFunc func() (int, error)) (int, error) {
34-
if conn == nil {
32+
if conn == nil || setting.CacheService.TTL == 0 {
3533
return getFunc()
3634
}
3735
if !conn.IsExist(key) {
@@ -42,14 +40,14 @@ func GetInt(key string, getFunc func() (int, error)) (int, error) {
4240
if value, err = getFunc(); err != nil {
4341
return value, err
4442
}
45-
conn.Put(key, value, int64(time.Hour.Seconds()))
43+
conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
4644
}
4745
return conn.Get(key).(int), nil
4846
}
4947

5048
// GetInt64 returns key value from cache with callback when no key exists in cache
5149
func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
52-
if conn == nil {
50+
if conn == nil || setting.CacheService.TTL == 0 {
5351
return getFunc()
5452
}
5553
if !conn.IsExist(key) {
@@ -60,7 +58,7 @@ func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
6058
if value, err = getFunc(); err != nil {
6159
return value, err
6260
}
63-
conn.Put(key, value, int64(time.Hour.Seconds()))
61+
conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
6462
}
6563
return conn.Get(key).(int64), nil
6664
}

modules/setting/setting.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ type Cache struct {
12771277
Adapter string
12781278
Interval int
12791279
Conn string
1280+
TTL time.Duration
12801281
}
12811282

12821283
var (
@@ -1285,7 +1286,7 @@ var (
12851286
)
12861287

12871288
func newCacheService() {
1288-
sec := Cfg.Section("mailer")
1289+
sec := Cfg.Section("cache")
12891290
CacheService = &Cache{
12901291
Adapter: sec.Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"}),
12911292
}
@@ -1297,6 +1298,7 @@ func newCacheService() {
12971298
default:
12981299
log.Fatal(4, "Unknown cache adapter: %s", CacheService.Adapter)
12991300
}
1301+
CacheService.TTL = sec.Key("ITEM_TTL").MustDuration(16 * time.Hour)
13001302

13011303
log.Info("Cache Service Enabled")
13021304
}

0 commit comments

Comments
 (0)