gcache_adapter.go 5.4 KB

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