gcache.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gcache provides kinds of cache management for process.
  7. //
  8. // It provides a concurrent-safe in-memory cache adapter for process in default.
  9. package gcache
  10. import (
  11. "context"
  12. "time"
  13. "github.com/gogf/gf/v2/container/gvar"
  14. )
  15. // Func is the cache function that calculates and returns the value.
  16. type Func func(ctx context.Context) (value interface{}, err error)
  17. // Default cache object.
  18. var defaultCache = New()
  19. // Set sets cache with `key`-`value` pair, which is expired after `duration`.
  20. //
  21. // It does not expire if `duration` == 0.
  22. // It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
  23. func Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error {
  24. return defaultCache.Set(ctx, key, value, duration)
  25. }
  26. // SetMap batch sets cache with key-value pairs by `data` map, which is expired after `duration`.
  27. //
  28. // It does not expire if `duration` == 0.
  29. // It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
  30. func SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error {
  31. return defaultCache.SetMap(ctx, data, duration)
  32. }
  33. // SetIfNotExist sets cache with `key`-`value` pair which is expired after `duration`
  34. // if `key` does not exist in the cache. It returns true the `key` does not exist in the
  35. // cache, and it sets `value` successfully to the cache, or else it returns false.
  36. //
  37. // It does not expire if `duration` == 0.
  38. // It deletes the `key` if `duration` < 0 or given `value` is nil.
  39. func SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (bool, error) {
  40. return defaultCache.SetIfNotExist(ctx, key, value, duration)
  41. }
  42. // SetIfNotExistFunc sets `key` with result of function `f` and returns true
  43. // if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists.
  44. //
  45. // The parameter `value` can be type of `func() interface{}`, but it does nothing if its
  46. // result is nil.
  47. //
  48. // It does not expire if `duration` == 0.
  49. // It deletes the `key` if `duration` < 0 or given `value` is nil.
  50. func SetIfNotExistFunc(ctx context.Context, key interface{}, f Func, duration time.Duration) (bool, error) {
  51. return defaultCache.SetIfNotExistFunc(ctx, key, f, duration)
  52. }
  53. // SetIfNotExistFuncLock sets `key` with result of function `f` and returns true
  54. // if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists.
  55. //
  56. // It does not expire if `duration` == 0.
  57. // It deletes the `key` if `duration` < 0 or given `value` is nil.
  58. //
  59. // Note that it differs from function `SetIfNotExistFunc` is that the function `f` is executed within
  60. // writing mutex lock for concurrent safety purpose.
  61. func SetIfNotExistFuncLock(ctx context.Context, key interface{}, f Func, duration time.Duration) (bool, error) {
  62. return defaultCache.SetIfNotExistFuncLock(ctx, key, f, duration)
  63. }
  64. // Get retrieves and returns the associated value of given `key`.
  65. // It returns nil if it does not exist, or its value is nil, or it's expired.
  66. // If you would like to check if the `key` exists in the cache, it's better using function Contains.
  67. func Get(ctx context.Context, key interface{}) (*gvar.Var, error) {
  68. return defaultCache.Get(ctx, key)
  69. }
  70. // GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and
  71. // returns `value` if `key` does not exist in the cache. The key-value pair expires
  72. // after `duration`.
  73. //
  74. // It does not expire if `duration` == 0.
  75. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
  76. // if `value` is a function and the function result is nil.
  77. func GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (*gvar.Var, error) {
  78. return defaultCache.GetOrSet(ctx, key, value, duration)
  79. }
  80. // GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of
  81. // function `f` and returns its result if `key` does not exist in the cache. The key-value
  82. // pair expires after `duration`.
  83. //
  84. // It does not expire if `duration` == 0.
  85. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
  86. // if `value` is a function and the function result is nil.
  87. func GetOrSetFunc(ctx context.Context, key interface{}, f Func, duration time.Duration) (*gvar.Var, error) {
  88. return defaultCache.GetOrSetFunc(ctx, key, f, duration)
  89. }
  90. // GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of
  91. // function `f` and returns its result if `key` does not exist in the cache. The key-value
  92. // pair expires after `duration`.
  93. //
  94. // It does not expire if `duration` == 0.
  95. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
  96. // if `value` is a function and the function result is nil.
  97. //
  98. // Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within
  99. // writing mutex lock for concurrent safety purpose.
  100. func GetOrSetFuncLock(ctx context.Context, key interface{}, f Func, duration time.Duration) (*gvar.Var, error) {
  101. return defaultCache.GetOrSetFuncLock(ctx, key, f, duration)
  102. }
  103. // Contains checks and returns true if `key` exists in the cache, or else returns false.
  104. func Contains(ctx context.Context, key interface{}) (bool, error) {
  105. return defaultCache.Contains(ctx, key)
  106. }
  107. // GetExpire retrieves and returns the expiration of `key` in the cache.
  108. //
  109. // Note that,
  110. // It returns 0 if the `key` does not expire.
  111. // It returns -1 if the `key` does not exist in the cache.
  112. func GetExpire(ctx context.Context, key interface{}) (time.Duration, error) {
  113. return defaultCache.GetExpire(ctx, key)
  114. }
  115. // Remove deletes one or more keys from cache, and returns its value.
  116. // If multiple keys are given, it returns the value of the last deleted item.
  117. func Remove(ctx context.Context, keys ...interface{}) (value *gvar.Var, err error) {
  118. return defaultCache.Remove(ctx, keys...)
  119. }
  120. // Removes deletes `keys` in the cache.
  121. func Removes(ctx context.Context, keys []interface{}) error {
  122. return defaultCache.Removes(ctx, keys)
  123. }
  124. // Update updates the value of `key` without changing its expiration and returns the old value.
  125. // The returned value `exist` is false if the `key` does not exist in the cache.
  126. //
  127. // It deletes the `key` if given `value` is nil.
  128. // It does nothing if `key` does not exist in the cache.
  129. func Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
  130. return defaultCache.Update(ctx, key, value)
  131. }
  132. // UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
  133. //
  134. // It returns -1 and does nothing if the `key` does not exist in the cache.
  135. // It deletes the `key` if `duration` < 0.
  136. func UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
  137. return defaultCache.UpdateExpire(ctx, key, duration)
  138. }
  139. // Size returns the number of items in the cache.
  140. func Size(ctx context.Context) (int, error) {
  141. return defaultCache.Size(ctx)
  142. }
  143. // Data returns a copy of all key-value pairs in the cache as map type.
  144. // Note that this function may lead lots of memory usage, you can implement this function
  145. // if necessary.
  146. func Data(ctx context.Context) (map[interface{}]interface{}, error) {
  147. return defaultCache.Data(ctx)
  148. }
  149. // Keys returns all keys in the cache as slice.
  150. func Keys(ctx context.Context) ([]interface{}, error) {
  151. return defaultCache.Keys(ctx)
  152. }
  153. // KeyStrings returns all keys in the cache as string slice.
  154. func KeyStrings(ctx context.Context) ([]string, error) {
  155. return defaultCache.KeyStrings(ctx)
  156. }
  157. // Values returns all values in the cache as slice.
  158. func Values(ctx context.Context) ([]interface{}, error) {
  159. return defaultCache.Values(ctx)
  160. }
  161. // MustGet acts like Get, but it panics if any error occurs.
  162. func MustGet(ctx context.Context, key interface{}) *gvar.Var {
  163. return defaultCache.MustGet(ctx, key)
  164. }
  165. // MustGetOrSet acts like GetOrSet, but it panics if any error occurs.
  166. func MustGetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) *gvar.Var {
  167. return defaultCache.MustGetOrSet(ctx, key, value, duration)
  168. }
  169. // MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs.
  170. func MustGetOrSetFunc(ctx context.Context, key interface{}, f Func, duration time.Duration) *gvar.Var {
  171. return defaultCache.MustGetOrSet(ctx, key, f, duration)
  172. }
  173. // MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs.
  174. func MustGetOrSetFuncLock(ctx context.Context, key interface{}, f Func, duration time.Duration) *gvar.Var {
  175. return defaultCache.MustGetOrSetFuncLock(ctx, key, f, duration)
  176. }
  177. // MustContains acts like Contains, but it panics if any error occurs.
  178. func MustContains(ctx context.Context, key interface{}) bool {
  179. return defaultCache.MustContains(ctx, key)
  180. }
  181. // MustGetExpire acts like GetExpire, but it panics if any error occurs.
  182. func MustGetExpire(ctx context.Context, key interface{}) time.Duration {
  183. return defaultCache.MustGetExpire(ctx, key)
  184. }
  185. // MustSize acts like Size, but it panics if any error occurs.
  186. func MustSize(ctx context.Context) int {
  187. return defaultCache.MustSize(ctx)
  188. }
  189. // MustData acts like Data, but it panics if any error occurs.
  190. func MustData(ctx context.Context) map[interface{}]interface{} {
  191. return defaultCache.MustData(ctx)
  192. }
  193. // MustKeys acts like Keys, but it panics if any error occurs.
  194. func MustKeys(ctx context.Context) []interface{} {
  195. return defaultCache.MustKeys(ctx)
  196. }
  197. // MustKeyStrings acts like KeyStrings, but it panics if any error occurs.
  198. func MustKeyStrings(ctx context.Context) []string {
  199. return defaultCache.MustKeyStrings(ctx)
  200. }
  201. // MustValues acts like Values, but it panics if any error occurs.
  202. func MustValues(ctx context.Context) []interface{} {
  203. return defaultCache.MustValues(ctx)
  204. }