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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2017 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 cache
  5. import (
  6. "fmt"
  7. "strconv"
  8. "code.gitea.io/gitea/modules/setting"
  9. mc "gitea.com/go-chi/cache"
  10. _ "gitea.com/go-chi/cache/memcache" // memcache plugin for cache
  11. )
  12. var conn mc.Cache
  13. func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
  14. return mc.NewCacher(mc.Options{
  15. Adapter: cacheConfig.Adapter,
  16. AdapterConfig: cacheConfig.Conn,
  17. Interval: cacheConfig.Interval,
  18. })
  19. }
  20. // NewContext start cache service
  21. func NewContext() error {
  22. var err error
  23. if conn == nil && setting.CacheService.Enabled {
  24. if conn, err = newCache(setting.CacheService.Cache); err != nil {
  25. return err
  26. }
  27. if err = conn.Ping(); err != nil {
  28. return err
  29. }
  30. }
  31. return err
  32. }
  33. // GetCache returns the currently configured cache
  34. func GetCache() mc.Cache {
  35. return conn
  36. }
  37. // GetString returns the key value from cache with callback when no key exists in cache
  38. func GetString(key string, getFunc func() (string, error)) (string, error) {
  39. if conn == nil || setting.CacheService.TTL == 0 {
  40. return getFunc()
  41. }
  42. if !conn.IsExist(key) {
  43. var (
  44. value string
  45. err error
  46. )
  47. if value, err = getFunc(); err != nil {
  48. return value, err
  49. }
  50. err = conn.Put(key, value, setting.CacheService.TTLSeconds())
  51. if err != nil {
  52. return "", err
  53. }
  54. }
  55. value := conn.Get(key)
  56. if v, ok := value.(string); ok {
  57. return v, nil
  58. }
  59. if v, ok := value.(fmt.Stringer); ok {
  60. return v.String(), nil
  61. }
  62. return fmt.Sprintf("%s", conn.Get(key)), nil
  63. }
  64. // GetInt returns key value from cache with callback when no key exists in cache
  65. func GetInt(key string, getFunc func() (int, error)) (int, error) {
  66. if conn == nil || setting.CacheService.TTL == 0 {
  67. return getFunc()
  68. }
  69. if !conn.IsExist(key) {
  70. var (
  71. value int
  72. err error
  73. )
  74. if value, err = getFunc(); err != nil {
  75. return value, err
  76. }
  77. err = conn.Put(key, value, setting.CacheService.TTLSeconds())
  78. if err != nil {
  79. return 0, err
  80. }
  81. }
  82. switch value := conn.Get(key).(type) {
  83. case int:
  84. return value, nil
  85. case string:
  86. v, err := strconv.Atoi(value)
  87. if err != nil {
  88. return 0, err
  89. }
  90. return v, nil
  91. default:
  92. return 0, fmt.Errorf("Unsupported cached value type: %v", value)
  93. }
  94. }
  95. // GetInt64 returns key value from cache with callback when no key exists in cache
  96. func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
  97. if conn == nil || setting.CacheService.TTL == 0 {
  98. return getFunc()
  99. }
  100. if !conn.IsExist(key) {
  101. var (
  102. value int64
  103. err error
  104. )
  105. if value, err = getFunc(); err != nil {
  106. return value, err
  107. }
  108. err = conn.Put(key, value, setting.CacheService.TTLSeconds())
  109. if err != nil {
  110. return 0, err
  111. }
  112. }
  113. switch value := conn.Get(key).(type) {
  114. case int64:
  115. return value, nil
  116. case string:
  117. v, err := strconv.ParseInt(value, 10, 64)
  118. if err != nil {
  119. return 0, err
  120. }
  121. return v, nil
  122. default:
  123. return 0, fmt.Errorf("Unsupported cached value type: %v", value)
  124. }
  125. }
  126. // Remove key from cache
  127. func Remove(key string) {
  128. if conn == nil {
  129. return
  130. }
  131. _ = conn.Delete(key)
  132. }