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