您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2019 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. package setting
  5. import (
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. // Cache represents cache settings
  11. type Cache struct {
  12. Enabled bool
  13. Adapter string
  14. Interval int
  15. Conn string
  16. TTL time.Duration `ini:"ITEM_TTL"`
  17. }
  18. var (
  19. // CacheService the global cache
  20. CacheService = struct {
  21. Cache `ini:"cache"`
  22. LastCommit struct {
  23. Enabled bool
  24. TTL time.Duration `ini:"ITEM_TTL"`
  25. CommitsCount int64
  26. } `ini:"cache.last_commit"`
  27. }{
  28. Cache: Cache{
  29. Enabled: true,
  30. Adapter: "memory",
  31. Interval: 60,
  32. TTL: 16 * time.Hour,
  33. },
  34. LastCommit: struct {
  35. Enabled bool
  36. TTL time.Duration `ini:"ITEM_TTL"`
  37. CommitsCount int64
  38. }{
  39. Enabled: true,
  40. TTL: 8760 * time.Hour,
  41. CommitsCount: 1000,
  42. },
  43. }
  44. )
  45. // MemcacheMaxTTL represents the maximum memcache TTL
  46. const MemcacheMaxTTL = 30 * 24 * time.Hour
  47. func newCacheService() {
  48. sec := Cfg.Section("cache")
  49. if err := sec.MapTo(&CacheService); err != nil {
  50. log.Fatal("Failed to map Cache settings: %v", err)
  51. }
  52. CacheService.Adapter = sec.Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"})
  53. switch CacheService.Adapter {
  54. case "memory":
  55. case "redis", "memcache":
  56. CacheService.Conn = strings.Trim(sec.Key("HOST").String(), "\" ")
  57. case "": // disable cache
  58. CacheService.Enabled = false
  59. default:
  60. log.Fatal("Unknown cache adapter: %s", CacheService.Adapter)
  61. }
  62. if CacheService.Enabled {
  63. log.Info("Cache Service Enabled")
  64. } else {
  65. log.Warn("Cache Service Disabled so that captcha disabled too")
  66. // captcha depends on cache service
  67. Service.EnableCaptcha = false
  68. }
  69. sec = Cfg.Section("cache.last_commit")
  70. if !CacheService.Enabled {
  71. CacheService.LastCommit.Enabled = false
  72. }
  73. CacheService.LastCommit.CommitsCount = sec.Key("COMMITS_COUNT").MustInt64(1000)
  74. if CacheService.LastCommit.Enabled {
  75. log.Info("Last Commit Cache Service Enabled")
  76. }
  77. }
  78. // TTLSeconds returns the TTLSeconds or unix timestamp for memcache
  79. func (c Cache) TTLSeconds() int64 {
  80. if c.Adapter == "memcache" && c.TTL > MemcacheMaxTTL {
  81. return time.Now().Add(c.TTL).Unix()
  82. }
  83. return int64(c.TTL.Seconds())
  84. }
  85. // LastCommitCacheTTLSeconds returns the TTLSeconds or unix timestamp for memcache
  86. func LastCommitCacheTTLSeconds() int64 {
  87. if CacheService.Adapter == "memcache" && CacheService.LastCommit.TTL > MemcacheMaxTTL {
  88. return time.Now().Add(CacheService.LastCommit.TTL).Unix()
  89. }
  90. return int64(CacheService.LastCommit.TTL.Seconds())
  91. }