github.com/wangyougui/gf/v2@v2.6.5/os/gcache/gcache.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/wangyougui/gf. 6 7 // Package gcache provides kinds of cache management for process. 8 // 9 // It provides a concurrent-safe in-memory cache adapter for process in default. 10 package gcache 11 12 import ( 13 "context" 14 "time" 15 16 "github.com/wangyougui/gf/v2/container/gvar" 17 ) 18 19 // Func is the cache function that calculates and returns the value. 20 type Func func(ctx context.Context) (value interface{}, err error) 21 22 const ( 23 DurationNoExpire = time.Duration(0) // Expire duration that never expires. 24 ) 25 26 // Default cache object. 27 var defaultCache = New() 28 29 // Set sets cache with `key`-`value` pair, which is expired after `duration`. 30 // 31 // It does not expire if `duration` == 0. 32 // It deletes the keys of `data` if `duration` < 0 or given `value` is nil. 33 func Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error { 34 return defaultCache.Set(ctx, key, value, duration) 35 } 36 37 // SetMap batch sets cache with key-value pairs by `data` map, which is expired after `duration`. 38 // 39 // It does not expire if `duration` == 0. 40 // It deletes the keys of `data` if `duration` < 0 or given `value` is nil. 41 func SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error { 42 return defaultCache.SetMap(ctx, data, duration) 43 } 44 45 // SetIfNotExist sets cache with `key`-`value` pair which is expired after `duration` 46 // if `key` does not exist in the cache. It returns true the `key` does not exist in the 47 // cache, and it sets `value` successfully to the cache, or else it returns false. 48 // 49 // It does not expire if `duration` == 0. 50 // It deletes the `key` if `duration` < 0 or given `value` is nil. 51 func SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (bool, error) { 52 return defaultCache.SetIfNotExist(ctx, key, value, duration) 53 } 54 55 // SetIfNotExistFunc sets `key` with result of function `f` and returns true 56 // if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists. 57 // 58 // The parameter `value` can be type of `func() interface{}`, but it does nothing if its 59 // result is nil. 60 // 61 // It does not expire if `duration` == 0. 62 // It deletes the `key` if `duration` < 0 or given `value` is nil. 63 func SetIfNotExistFunc(ctx context.Context, key interface{}, f Func, duration time.Duration) (bool, error) { 64 return defaultCache.SetIfNotExistFunc(ctx, key, f, duration) 65 } 66 67 // SetIfNotExistFuncLock sets `key` with result of function `f` and returns true 68 // if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists. 69 // 70 // It does not expire if `duration` == 0. 71 // It deletes the `key` if `duration` < 0 or given `value` is nil. 72 // 73 // Note that it differs from function `SetIfNotExistFunc` is that the function `f` is executed within 74 // writing mutex lock for concurrent safety purpose. 75 func SetIfNotExistFuncLock(ctx context.Context, key interface{}, f Func, duration time.Duration) (bool, error) { 76 return defaultCache.SetIfNotExistFuncLock(ctx, key, f, duration) 77 } 78 79 // Get retrieves and returns the associated value of given `key`. 80 // It returns nil if it does not exist, or its value is nil, or it's expired. 81 // If you would like to check if the `key` exists in the cache, it's better using function Contains. 82 func Get(ctx context.Context, key interface{}) (*gvar.Var, error) { 83 return defaultCache.Get(ctx, key) 84 } 85 86 // GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and 87 // returns `value` if `key` does not exist in the cache. The key-value pair expires 88 // after `duration`. 89 // 90 // It does not expire if `duration` == 0. 91 // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing 92 // if `value` is a function and the function result is nil. 93 func GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (*gvar.Var, error) { 94 return defaultCache.GetOrSet(ctx, key, value, duration) 95 } 96 97 // GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of 98 // function `f` and returns its result if `key` does not exist in the cache. The key-value 99 // pair expires after `duration`. 100 // 101 // It does not expire if `duration` == 0. 102 // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing 103 // if `value` is a function and the function result is nil. 104 func GetOrSetFunc(ctx context.Context, key interface{}, f Func, duration time.Duration) (*gvar.Var, error) { 105 return defaultCache.GetOrSetFunc(ctx, key, f, duration) 106 } 107 108 // GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of 109 // function `f` and returns its result if `key` does not exist in the cache. The key-value 110 // pair expires after `duration`. 111 // 112 // It does not expire if `duration` == 0. 113 // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing 114 // if `value` is a function and the function result is nil. 115 // 116 // Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within 117 // writing mutex lock for concurrent safety purpose. 118 func GetOrSetFuncLock(ctx context.Context, key interface{}, f Func, duration time.Duration) (*gvar.Var, error) { 119 return defaultCache.GetOrSetFuncLock(ctx, key, f, duration) 120 } 121 122 // Contains checks and returns true if `key` exists in the cache, or else returns false. 123 func Contains(ctx context.Context, key interface{}) (bool, error) { 124 return defaultCache.Contains(ctx, key) 125 } 126 127 // GetExpire retrieves and returns the expiration of `key` in the cache. 128 // 129 // Note that, 130 // It returns 0 if the `key` does not expire. 131 // It returns -1 if the `key` does not exist in the cache. 132 func GetExpire(ctx context.Context, key interface{}) (time.Duration, error) { 133 return defaultCache.GetExpire(ctx, key) 134 } 135 136 // Remove deletes one or more keys from cache, and returns its value. 137 // If multiple keys are given, it returns the value of the last deleted item. 138 func Remove(ctx context.Context, keys ...interface{}) (value *gvar.Var, err error) { 139 return defaultCache.Remove(ctx, keys...) 140 } 141 142 // Removes deletes `keys` in the cache. 143 func Removes(ctx context.Context, keys []interface{}) error { 144 return defaultCache.Removes(ctx, keys) 145 } 146 147 // Update updates the value of `key` without changing its expiration and returns the old value. 148 // The returned value `exist` is false if the `key` does not exist in the cache. 149 // 150 // It deletes the `key` if given `value` is nil. 151 // It does nothing if `key` does not exist in the cache. 152 func Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) { 153 return defaultCache.Update(ctx, key, value) 154 } 155 156 // UpdateExpire updates the expiration of `key` and returns the old expiration duration value. 157 // 158 // It returns -1 and does nothing if the `key` does not exist in the cache. 159 // It deletes the `key` if `duration` < 0. 160 func UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) { 161 return defaultCache.UpdateExpire(ctx, key, duration) 162 } 163 164 // Size returns the number of items in the cache. 165 func Size(ctx context.Context) (int, error) { 166 return defaultCache.Size(ctx) 167 } 168 169 // Data returns a copy of all key-value pairs in the cache as map type. 170 // Note that this function may lead lots of memory usage, you can implement this function 171 // if necessary. 172 func Data(ctx context.Context) (map[interface{}]interface{}, error) { 173 return defaultCache.Data(ctx) 174 } 175 176 // Keys returns all keys in the cache as slice. 177 func Keys(ctx context.Context, regexp ...string) ([]interface{}, error) { 178 return defaultCache.Keys(ctx, regexp...) 179 } 180 181 // KeyStrings returns all keys in the cache as string slice. 182 func KeyStrings(ctx context.Context, pattern ...string) ([]string, error) { 183 return defaultCache.KeyStrings(ctx, pattern...) 184 } 185 186 // Values returns all values in the cache as slice. 187 func Values(ctx context.Context) ([]interface{}, error) { 188 return defaultCache.Values(ctx) 189 } 190 191 // MustGet acts like Get, but it panics if any error occurs. 192 func MustGet(ctx context.Context, key interface{}) *gvar.Var { 193 return defaultCache.MustGet(ctx, key) 194 } 195 196 // MustGetOrSet acts like GetOrSet, but it panics if any error occurs. 197 func MustGetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) *gvar.Var { 198 return defaultCache.MustGetOrSet(ctx, key, value, duration) 199 } 200 201 // MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs. 202 func MustGetOrSetFunc(ctx context.Context, key interface{}, f Func, duration time.Duration) *gvar.Var { 203 return defaultCache.MustGetOrSetFunc(ctx, key, f, duration) 204 } 205 206 // MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs. 207 func MustGetOrSetFuncLock(ctx context.Context, key interface{}, f Func, duration time.Duration) *gvar.Var { 208 return defaultCache.MustGetOrSetFuncLock(ctx, key, f, duration) 209 } 210 211 // MustContains acts like Contains, but it panics if any error occurs. 212 func MustContains(ctx context.Context, key interface{}) bool { 213 return defaultCache.MustContains(ctx, key) 214 } 215 216 // MustGetExpire acts like GetExpire, but it panics if any error occurs. 217 func MustGetExpire(ctx context.Context, key interface{}) time.Duration { 218 return defaultCache.MustGetExpire(ctx, key) 219 } 220 221 // MustSize acts like Size, but it panics if any error occurs. 222 func MustSize(ctx context.Context) int { 223 return defaultCache.MustSize(ctx) 224 } 225 226 // MustData acts like Data, but it panics if any error occurs. 227 func MustData(ctx context.Context) map[interface{}]interface{} { 228 return defaultCache.MustData(ctx) 229 } 230 231 // MustKeys acts like Keys, but it panics if any error occurs. 232 func MustKeys(ctx context.Context) []interface{} { 233 return defaultCache.MustKeys(ctx) 234 } 235 236 // MustKeyStrings acts like KeyStrings, but it panics if any error occurs. 237 func MustKeyStrings(ctx context.Context) []string { 238 return defaultCache.MustKeyStrings(ctx) 239 } 240 241 // MustValues acts like Values, but it panics if any error occurs. 242 func MustValues(ctx context.Context) []interface{} { 243 return defaultCache.MustValues(ctx) 244 }