code.gitea.io/gitea@v1.22.3/modules/setting/cache.go (about) 1 // Copyright 2019 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package setting 5 6 import ( 7 "strings" 8 "time" 9 10 "code.gitea.io/gitea/modules/log" 11 ) 12 13 // Cache represents cache settings 14 type Cache struct { 15 Adapter string 16 Interval int 17 Conn string 18 TTL time.Duration `ini:"ITEM_TTL"` 19 } 20 21 // CacheService the global cache 22 var CacheService = struct { 23 Cache `ini:"cache"` 24 25 LastCommit struct { 26 TTL time.Duration `ini:"ITEM_TTL"` 27 CommitsCount int64 28 } `ini:"cache.last_commit"` 29 }{ 30 Cache: Cache{ 31 Adapter: "memory", 32 Interval: 60, 33 TTL: 16 * time.Hour, 34 }, 35 LastCommit: struct { 36 TTL time.Duration `ini:"ITEM_TTL"` 37 CommitsCount int64 38 }{ 39 TTL: 8760 * time.Hour, 40 CommitsCount: 1000, 41 }, 42 } 43 44 // MemcacheMaxTTL represents the maximum memcache TTL 45 const MemcacheMaxTTL = 30 * 24 * time.Hour 46 47 func loadCacheFrom(rootCfg ConfigProvider) { 48 sec := rootCfg.Section("cache") 49 if err := sec.MapTo(&CacheService); err != nil { 50 log.Fatal("Failed to map Cache settings: %v", err) 51 } 52 53 CacheService.Adapter = sec.Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache", "twoqueue"}) 54 switch CacheService.Adapter { 55 case "memory": 56 case "redis", "memcache": 57 CacheService.Conn = strings.Trim(sec.Key("HOST").String(), "\" ") 58 case "twoqueue": 59 CacheService.Conn = strings.TrimSpace(sec.Key("HOST").String()) 60 if CacheService.Conn == "" { 61 CacheService.Conn = "50000" 62 } 63 default: 64 log.Fatal("Unknown cache adapter: %s", CacheService.Adapter) 65 } 66 67 sec = rootCfg.Section("cache.last_commit") 68 CacheService.LastCommit.CommitsCount = sec.Key("COMMITS_COUNT").MustInt64(1000) 69 } 70 71 // TTLSeconds returns the TTLSeconds or unix timestamp for memcache 72 func (c Cache) TTLSeconds() int64 { 73 if c.Adapter == "memcache" && c.TTL > MemcacheMaxTTL { 74 return time.Now().Add(c.TTL).Unix() 75 } 76 return int64(c.TTL.Seconds()) 77 } 78 79 // LastCommitCacheTTLSeconds returns the TTLSeconds or unix timestamp for memcache 80 func LastCommitCacheTTLSeconds() int64 { 81 if CacheService.Adapter == "memcache" && CacheService.LastCommit.TTL > MemcacheMaxTTL { 82 return time.Now().Add(CacheService.LastCommit.TTL).Unix() 83 } 84 return int64(CacheService.LastCommit.TTL.Seconds()) 85 }