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

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