gcache_adapter.go 5.0 KB

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