gcache模块采用了适配器设计模式,提供了Adapter适配器接口,任何实现了Adapter接口的对象均可注册到缓存管理对象中,使得开发者可以对缓存管理对象进行灵活的扩展。

    gcache.Cache对象结构定义如下:

    1. // Cache struct.
    2. type Cache struct {
    3. Adapter // Adapter for cache features.
    4. }

    Adapter接口定义如下:

    https://godoc.org/github.com/gogf/gf/os/gcache#Adapter

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

    适配器的注册方法:

    1. // SetAdapter changes the adapter for this cache.
    2. // Be very note that, this setting function is not concurrent-safe, which means you should not call
    3. // this setting function concurrently in multiple goroutines.
    4. func (c *Cache) SetAdapter(adapter Adapter)

    具体示例请参考 缓存管理-Redis缓存 章节。