gcache.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // It provides a concurrent-safe in-memory cache adapter for process in default.
  8. package gcache
  9. import (
  10. "context"
  11. "github.com/gogf/gf/container/gvar"
  12. "time"
  13. )
  14. // Default cache object.
  15. var defaultCache = New()
  16. // Ctx is a chaining function, which shallowly clones current object and sets the context
  17. // for next operation.
  18. func Ctx(ctx context.Context) *Cache {
  19. return defaultCache.Ctx(ctx)
  20. }
  21. // Set sets cache with `key`-`value` pair, which is expired after `duration`.
  22. // It does not expire if `duration` == 0.
  23. func Set(key interface{}, value interface{}, duration time.Duration) error {
  24. return defaultCache.Set(key, value, duration)
  25. }
  26. // SetIfNotExist sets cache with `key`-`value` pair if `key` does not exist in the cache,
  27. // which is expired after `duration`. It does not expire if `duration` == 0.
  28. func SetIfNotExist(key interface{}, value interface{}, duration time.Duration) (bool, error) {
  29. return defaultCache.SetIfNotExist(key, value, duration)
  30. }
  31. // Sets batch sets cache with key-value pairs by `data`, which is expired after `duration`.
  32. //
  33. // It does not expire if `duration` == 0.
  34. func Sets(data map[interface{}]interface{}, duration time.Duration) error {
  35. return defaultCache.Sets(data, duration)
  36. }
  37. // Get returns the value of `key`.
  38. // It returns nil if it does not exist or its value is nil.
  39. func Get(key interface{}) (interface{}, error) {
  40. return defaultCache.Get(key)
  41. }
  42. // GetVar retrieves and returns the value of `key` as gvar.Var.
  43. func GetVar(key interface{}) (*gvar.Var, error) {
  44. return defaultCache.GetVar(key)
  45. }
  46. // GetOrSet returns the value of `key`,
  47. // or sets `key`-`value` pair and returns `value` if `key` does not exist in the cache.
  48. // The key-value pair expires after `duration`.
  49. //
  50. // It does not expire if `duration` == 0.
  51. func GetOrSet(key interface{}, value interface{}, duration time.Duration) (interface{}, error) {
  52. return defaultCache.GetOrSet(key, value, duration)
  53. }
  54. // GetOrSetFunc returns the value of `key`, or sets `key` with result of function `f`
  55. // and returns its result if `key` does not exist in the cache. The key-value pair expires
  56. // after `duration`. It does not expire if `duration` == 0.
  57. func GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
  58. return defaultCache.GetOrSetFunc(key, f, duration)
  59. }
  60. // GetOrSetFuncLock returns the value of `key`, or sets `key` with result of function `f`
  61. // and returns its result if `key` does not exist in the cache. The key-value pair expires
  62. // after `duration`. It does not expire if `duration` == 0.
  63. //
  64. // Note that the function `f` is executed within writing mutex lock.
  65. func GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
  66. return defaultCache.GetOrSetFuncLock(key, f, duration)
  67. }
  68. // Contains returns true if `key` exists in the cache, or else returns false.
  69. func Contains(key interface{}) (bool, error) {
  70. return defaultCache.Contains(key)
  71. }
  72. // Remove deletes the one or more keys from cache, and returns its value.
  73. // If multiple keys are given, it returns the value of the deleted last item.
  74. func Remove(keys ...interface{}) (value interface{}, err error) {
  75. return defaultCache.Remove(keys...)
  76. }
  77. // Removes deletes `keys` in the cache.
  78. // Deprecated, use Remove instead.
  79. func Removes(keys []interface{}) {
  80. defaultCache.Remove(keys...)
  81. }
  82. // Data returns a copy of all key-value pairs in the cache as map type.
  83. func Data() (map[interface{}]interface{}, error) {
  84. return defaultCache.Data()
  85. }
  86. // Keys returns all keys in the cache as slice.
  87. func Keys() ([]interface{}, error) {
  88. return defaultCache.Keys()
  89. }
  90. // KeyStrings returns all keys in the cache as string slice.
  91. func KeyStrings() ([]string, error) {
  92. return defaultCache.KeyStrings()
  93. }
  94. // Values returns all values in the cache as slice.
  95. func Values() ([]interface{}, error) {
  96. return defaultCache.Values()
  97. }
  98. // Size returns the size of the cache.
  99. func Size() (int, error) {
  100. return defaultCache.Size()
  101. }
  102. // GetExpire retrieves and returns the expiration of `key`.
  103. // It returns -1 if the `key` does not exist in the cache.
  104. func GetExpire(key interface{}) (time.Duration, error) {
  105. return defaultCache.GetExpire(key)
  106. }
  107. // Update updates the value of `key` without changing its expiration and returns the old value.
  108. // The returned `exist` value is false if the `key` does not exist in the cache.
  109. func Update(key interface{}, value interface{}) (oldValue interface{}, exist bool, err error) {
  110. return defaultCache.Update(key, value)
  111. }
  112. // UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
  113. // It returns -1 if the `key` does not exist in the cache.
  114. func UpdateExpire(key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
  115. return defaultCache.UpdateExpire(key, duration)
  116. }