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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/macaron/cache"
  10. _ "gitea.com/macaron/cache/memcache" // memcache plugin for cache
  11. _ "gitea.com/macaron/cache/redis"
  12. )
  13. var (
  14. conn mc.Cache
  15. )
  16. func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
  17. return mc.NewCacher(cacheConfig.Adapter, mc.Options{
  18. Adapter: cacheConfig.Adapter,
  19. AdapterConfig: cacheConfig.Conn,
  20. Interval: cacheConfig.Interval,
  21. })
  22. }
  23. // NewContext start cache service
  24. func NewContext() error {
  25. var err error
  26. if conn == nil && setting.CacheService.Enabled {
  27. if conn, err = newCache(setting.CacheService.Cache); err != nil {
  28. return err
  29. }
  30. }
  31. return err
  32. }
  33. // GetInt returns key value from cache with callback when no key exists in cache
  34. func GetInt(key string, getFunc func() (int, error)) (int, error) {
  35. if conn == nil || setting.CacheService.TTL == 0 {
  36. return getFunc()
  37. }
  38. if !conn.IsExist(key) {
  39. var (
  40. value int
  41. err error
  42. )
  43. if value, err = getFunc(); err != nil {
  44. return value, err
  45. }
  46. err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
  47. if err != nil {
  48. return 0, err
  49. }
  50. }
  51. switch value := conn.Get(key).(type) {
  52. case int:
  53. return value, nil
  54. case string:
  55. v, err := strconv.Atoi(value)
  56. if err != nil {
  57. return 0, err
  58. }
  59. return v, nil
  60. default:
  61. return 0, fmt.Errorf("Unsupported cached value type: %v", value)
  62. }
  63. }
  64. // GetInt64 returns key value from cache with callback when no key exists in cache
  65. func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
  66. if conn == nil || setting.CacheService.TTL == 0 {
  67. return getFunc()
  68. }
  69. if !conn.IsExist(key) {
  70. var (
  71. value int64
  72. err error
  73. )
  74. if value, err = getFunc(); err != nil {
  75. return value, err
  76. }
  77. err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
  78. if err != nil {
  79. return 0, err
  80. }
  81. }
  82. switch value := conn.Get(key).(type) {
  83. case int64:
  84. return value, nil
  85. case string:
  86. v, err := strconv.ParseInt(value, 10, 64)
  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. // Remove key from cache
  96. func Remove(key string) {
  97. if conn == nil {
  98. return
  99. }
  100. _ = conn.Delete(key)
  101. }