gcache_adapter.go 6.8 KB

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