gcache_cache_adapter.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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
  7. import (
  8. "time"
  9. )
  10. // Set sets cache with `key`-`value` pair, which is expired after `duration`.
  11. //
  12. // It does not expire if `duration` == 0.
  13. // It deletes the `key` if `duration` < 0.
  14. func (c *Cache) Set(key interface{}, value interface{}, duration time.Duration) error {
  15. return c.adapter.Set(c.getCtx(), key, value, duration)
  16. }
  17. // Sets batch sets cache with key-value pairs by `data`, which is expired after `duration`.
  18. //
  19. // It does not expire if `duration` == 0.
  20. // It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
  21. func (c *Cache) Sets(data map[interface{}]interface{}, duration time.Duration) error {
  22. return c.adapter.Sets(c.getCtx(), data, duration)
  23. }
  24. // SetIfNotExist sets cache with `key`-`value` pair which is expired after `duration`
  25. // if `key` does not exist in the cache. It returns true the `key` does not exist in the
  26. // cache, and it sets `value` successfully to the cache, or else it returns false.
  27. //
  28. // The parameter `value` can be type of <func() interface{}>, but it does nothing if its
  29. // result is nil.
  30. //
  31. // It does not expire if `duration` == 0.
  32. // It deletes the `key` if `duration` < 0 or given `value` is nil.
  33. func (c *Cache) SetIfNotExist(key interface{}, value interface{}, duration time.Duration) (bool, error) {
  34. return c.adapter.SetIfNotExist(c.getCtx(), key, value, duration)
  35. }
  36. // Get retrieves and returns the associated value of given `key`.
  37. // It returns nil if it does not exist, its value is nil or it's expired.
  38. func (c *Cache) Get(key interface{}) (interface{}, error) {
  39. return c.adapter.Get(c.getCtx(), key)
  40. }
  41. // GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and
  42. // returns `value` if `key` does not exist in the cache. The key-value pair expires
  43. // after `duration`.
  44. //
  45. // It does not expire if `duration` == 0.
  46. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
  47. // if `value` is a function and the function result is nil.
  48. func (c *Cache) GetOrSet(key interface{}, value interface{}, duration time.Duration) (interface{}, error) {
  49. return c.adapter.GetOrSet(c.getCtx(), key, value, duration)
  50. }
  51. // GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of
  52. // function `f` and returns its result if `key` does not exist in the cache. The key-value
  53. // pair expires after `duration`.
  54. //
  55. // It does not expire if `duration` == 0.
  56. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
  57. // if `value` is a function and the function result is nil.
  58. func (c *Cache) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
  59. return c.adapter.GetOrSetFunc(c.getCtx(), key, f, duration)
  60. }
  61. // GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of
  62. // function `f` and returns its result if `key` does not exist in the cache. The key-value
  63. // pair expires after `duration`.
  64. //
  65. // It does not expire if `duration` == 0.
  66. // It does nothing if function `f` returns nil.
  67. //
  68. // Note that the function `f` should be executed within writing mutex lock for concurrent
  69. // safety purpose.
  70. func (c *Cache) GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
  71. return c.adapter.GetOrSetFuncLock(c.getCtx(), key, f, duration)
  72. }
  73. // Contains returns true if `key` exists in the cache, or else returns false.
  74. func (c *Cache) Contains(key interface{}) (bool, error) {
  75. return c.adapter.Contains(c.getCtx(), key)
  76. }
  77. // GetExpire retrieves and returns the expiration of `key` in the cache.
  78. //
  79. // It returns 0 if the `key` does not expire.
  80. // It returns -1 if the `key` does not exist in the cache.
  81. func (c *Cache) GetExpire(key interface{}) (time.Duration, error) {
  82. return c.adapter.GetExpire(c.getCtx(), key)
  83. }
  84. // Remove deletes one or more keys from cache, and returns its value.
  85. // If multiple keys are given, it returns the value of the last deleted item.
  86. func (c *Cache) Remove(keys ...interface{}) (value interface{}, err error) {
  87. return c.adapter.Remove(c.getCtx(), keys...)
  88. }
  89. // Update updates the value of `key` without changing its expiration and returns the old value.
  90. // The returned value `exist` is false if the `key` does not exist in the cache.
  91. //
  92. // It deletes the `key` if given `value` is nil.
  93. // It does nothing if `key` does not exist in the cache.
  94. func (c *Cache) Update(key interface{}, value interface{}) (oldValue interface{}, exist bool, err error) {
  95. return c.adapter.Update(c.getCtx(), key, value)
  96. }
  97. // UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
  98. //
  99. // It returns -1 and does nothing if the `key` does not exist in the cache.
  100. // It deletes the `key` if `duration` < 0.
  101. func (c *Cache) UpdateExpire(key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
  102. return c.adapter.UpdateExpire(c.getCtx(), key, duration)
  103. }
  104. // Size returns the number of items in the cache.
  105. func (c *Cache) Size() (size int, err error) {
  106. return c.adapter.Size(c.getCtx())
  107. }
  108. // Data returns a copy of all key-value pairs in the cache as map type.
  109. // Note that this function may lead lots of memory usage, you can implement this function
  110. // if necessary.
  111. func (c *Cache) Data() (map[interface{}]interface{}, error) {
  112. return c.adapter.Data(c.getCtx())
  113. }
  114. // Keys returns all keys in the cache as slice.
  115. func (c *Cache) Keys() ([]interface{}, error) {
  116. return c.adapter.Keys(c.getCtx())
  117. }
  118. // Values returns all values in the cache as slice.
  119. func (c *Cache) Values() ([]interface{}, error) {
  120. return c.adapter.Values(c.getCtx())
  121. }
  122. // Clear clears all data of the cache.
  123. // Note that this function is sensitive and should be carefully used.
  124. func (c *Cache) Clear() error {
  125. return c.adapter.Clear(c.getCtx())
  126. }
  127. // Close closes the cache if necessary.
  128. func (c *Cache) Close() error {
  129. return c.adapter.Close(c.getCtx())
  130. }