You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cache.go 2.7KB

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