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

cache.go 3.8 kB

Use native git variants by default with go-git variants as build tag (#13673) * Move last commit cache back into modules/git Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from the interface for last commit cache Signed-off-by: Andrew Thornton <art27@cantab.net> * move cacheref to last_commit_cache Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from routers/private/hook Signed-off-by: Andrew Thornton <art27@cantab.net> * Move FindLFSFiles to pipeline Signed-off-by: Andrew Thornton <art27@cantab.net> * Make no-go-git variants Signed-off-by: Andrew Thornton <art27@cantab.net> * Submodule RefID Signed-off-by: Andrew Thornton <art27@cantab.net> * fix issue with GetCommitsInfo Signed-off-by: Andrew Thornton <art27@cantab.net> * fix GetLastCommitForPaths Signed-off-by: Andrew Thornton <art27@cantab.net> * Improve efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * More efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * even faster Signed-off-by: Andrew Thornton <art27@cantab.net> * Reduce duplication * As per @lunny Signed-off-by: Andrew Thornton <art27@cantab.net> * attempt to fix drone Signed-off-by: Andrew Thornton <art27@cantab.net> * fix test-tags Signed-off-by: Andrew Thornton <art27@cantab.net> * default to use no-go-git variants and add gogit build tag Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @6543 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
4 years ago
Use native git variants by default with go-git variants as build tag (#13673) * Move last commit cache back into modules/git Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from the interface for last commit cache Signed-off-by: Andrew Thornton <art27@cantab.net> * move cacheref to last_commit_cache Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from routers/private/hook Signed-off-by: Andrew Thornton <art27@cantab.net> * Move FindLFSFiles to pipeline Signed-off-by: Andrew Thornton <art27@cantab.net> * Make no-go-git variants Signed-off-by: Andrew Thornton <art27@cantab.net> * Submodule RefID Signed-off-by: Andrew Thornton <art27@cantab.net> * fix issue with GetCommitsInfo Signed-off-by: Andrew Thornton <art27@cantab.net> * fix GetLastCommitForPaths Signed-off-by: Andrew Thornton <art27@cantab.net> * Improve efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * More efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * even faster Signed-off-by: Andrew Thornton <art27@cantab.net> * Reduce duplication * As per @lunny Signed-off-by: Andrew Thornton <art27@cantab.net> * attempt to fix drone Signed-off-by: Andrew Thornton <art27@cantab.net> * fix test-tags Signed-off-by: Andrew Thornton <art27@cantab.net> * default to use no-go-git variants and add gogit build tag Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @6543 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. )
  12. var (
  13. conn mc.Cache
  14. )
  15. func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
  16. return mc.NewCacher(cacheConfig.Adapter, mc.Options{
  17. Adapter: cacheConfig.Adapter,
  18. AdapterConfig: cacheConfig.Conn,
  19. Interval: cacheConfig.Interval,
  20. })
  21. }
  22. // Cache is the interface that operates the cache data.
  23. type Cache interface {
  24. // Put puts value into cache with key and expire time.
  25. Put(key string, val interface{}, timeout int64) error
  26. // Get gets cached value by given key.
  27. Get(key string) interface{}
  28. // Delete deletes cached value by given key.
  29. Delete(key string) error
  30. // Incr increases cached int-type value by given key as a counter.
  31. Incr(key string) error
  32. // Decr decreases cached int-type value by given key as a counter.
  33. Decr(key string) error
  34. // IsExist returns true if cached value exists.
  35. IsExist(key string) bool
  36. // Flush deletes all cached data.
  37. Flush() error
  38. }
  39. // NewContext start cache service
  40. func NewContext() error {
  41. var err error
  42. if conn == nil && setting.CacheService.Enabled {
  43. if conn, err = newCache(setting.CacheService.Cache); err != nil {
  44. return err
  45. }
  46. }
  47. return err
  48. }
  49. // GetCache returns the currently configured cache
  50. func GetCache() Cache {
  51. return conn
  52. }
  53. // GetString returns the key value from cache with callback when no key exists in cache
  54. func GetString(key string, getFunc func() (string, error)) (string, error) {
  55. if conn == nil || setting.CacheService.TTL == 0 {
  56. return getFunc()
  57. }
  58. if !conn.IsExist(key) {
  59. var (
  60. value string
  61. err error
  62. )
  63. if value, err = getFunc(); err != nil {
  64. return value, err
  65. }
  66. err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
  67. if err != nil {
  68. return "", err
  69. }
  70. }
  71. value := conn.Get(key)
  72. if v, ok := value.(string); ok {
  73. return v, nil
  74. }
  75. if v, ok := value.(fmt.Stringer); ok {
  76. return v.String(), nil
  77. }
  78. return fmt.Sprintf("%s", conn.Get(key)), nil
  79. }
  80. // GetInt returns key value from cache with callback when no key exists in cache
  81. func GetInt(key string, getFunc func() (int, error)) (int, error) {
  82. if conn == nil || setting.CacheService.TTL == 0 {
  83. return getFunc()
  84. }
  85. if !conn.IsExist(key) {
  86. var (
  87. value int
  88. err error
  89. )
  90. if value, err = getFunc(); err != nil {
  91. return value, err
  92. }
  93. err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
  94. if err != nil {
  95. return 0, err
  96. }
  97. }
  98. switch value := conn.Get(key).(type) {
  99. case int:
  100. return value, nil
  101. case string:
  102. v, err := strconv.Atoi(value)
  103. if err != nil {
  104. return 0, err
  105. }
  106. return v, nil
  107. default:
  108. return 0, fmt.Errorf("Unsupported cached value type: %v", value)
  109. }
  110. }
  111. // GetInt64 returns key value from cache with callback when no key exists in cache
  112. func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
  113. if conn == nil || setting.CacheService.TTL == 0 {
  114. return getFunc()
  115. }
  116. if !conn.IsExist(key) {
  117. var (
  118. value int64
  119. err error
  120. )
  121. if value, err = getFunc(); err != nil {
  122. return value, err
  123. }
  124. err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
  125. if err != nil {
  126. return 0, err
  127. }
  128. }
  129. switch value := conn.Get(key).(type) {
  130. case int64:
  131. return value, nil
  132. case string:
  133. v, err := strconv.ParseInt(value, 10, 64)
  134. if err != nil {
  135. return 0, err
  136. }
  137. return v, nil
  138. default:
  139. return 0, fmt.Errorf("Unsupported cached value type: %v", value)
  140. }
  141. }
  142. // Remove key from cache
  143. func Remove(key string) {
  144. if conn == nil {
  145. return
  146. }
  147. _ = conn.Delete(key)
  148. }