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