gcache.go 4.9 KB

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