gcache.go 10.0 KB

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