Documentation
¶
Overview ¶
Package gcache provides kinds of cache management for process.
It provides a concurrent-safe in-memory cache adapter for process in default.
Index ¶
- func Contains(ctx context.Context, key any) (bool, error)
- func Data(ctx context.Context) (map[any]any, error)
- func Get(ctx context.Context, key any) (any, bool, error)
- func GetExpire(ctx context.Context, key any) (time.Duration, error)
- func GetOrSet(ctx context.Context, key any, value any, duration time.Duration) (any, error)
- func GetOrSetFunc(ctx context.Context, key any, f Func[any], duration time.Duration) (any, error)
- func GetOrSetFuncLock(ctx context.Context, key any, f Func[any], duration time.Duration) (any, error)
- func KeyStrings(ctx context.Context) ([]string, error)
- func Keys(ctx context.Context) ([]any, error)
- func MustContains(ctx context.Context, key any) bool
- func MustData(ctx context.Context) map[any]any
- func MustGet(ctx context.Context, key any) any
- func MustGetExpire(ctx context.Context, key any) time.Duration
- func MustGetOrSet(ctx context.Context, key any, value any, duration time.Duration) any
- func MustGetOrSetFunc(ctx context.Context, key any, f Func[any], duration time.Duration) any
- func MustGetOrSetFuncLock(ctx context.Context, key any, f Func[any], duration time.Duration) any
- func MustKeyStrings(ctx context.Context) []string
- func MustKeys(ctx context.Context) []any
- func MustSize(ctx context.Context) int
- func MustValues(ctx context.Context) []any
- func Remove(ctx context.Context, keys ...any) (value any, err error)
- func Removes(ctx context.Context, keys []any) error
- func Set(ctx context.Context, key any, value any, duration time.Duration) error
- func SetIfNotExist(ctx context.Context, key any, value any, duration time.Duration) (bool, error)
- func SetIfNotExistFunc(ctx context.Context, key any, f Func[any], duration time.Duration) (bool, error)
- func SetIfNotExistFuncLock(ctx context.Context, key any, f Func[any], duration time.Duration) (bool, error)
- func SetMap(ctx context.Context, data map[any]any, duration time.Duration) error
- func Size(ctx context.Context) (int, error)
- func Update(ctx context.Context, key any, value any) (oldValue any, exist bool, err error)
- func UpdateExpire(ctx context.Context, key any, duration time.Duration) (oldDuration time.Duration, err error)
- func Values(ctx context.Context) ([]any, error)
- type Adapter
- type AdapterMemory
- func (c *AdapterMemory[K, V]) Clear(ctx context.Context) error
- func (c *AdapterMemory[K, V]) Close(ctx context.Context) error
- func (c *AdapterMemory[K, V]) Contains(ctx context.Context, key K) (ok bool, err error)
- func (c *AdapterMemory[K, V]) Data(ctx context.Context) (map[K]V, error)
- func (c *AdapterMemory[K, V]) Get(ctx context.Context, key K) (v V, ok bool, err error)
- func (c *AdapterMemory[K, V]) GetExpire(ctx context.Context, key K) (time.Duration, error)
- func (c *AdapterMemory[K, V]) GetOrSet(ctx context.Context, key K, value V, duration time.Duration) (V, error)
- func (c *AdapterMemory[K, V]) GetOrSetFunc(ctx context.Context, key K, f Func[V], duration time.Duration) (v V, err error)
- func (c *AdapterMemory[K, V]) GetOrSetFuncLock(ctx context.Context, key K, f Func[V], duration time.Duration) (V, error)
- func (c *AdapterMemory[K, V]) Keys(ctx context.Context) ([]K, error)
- func (c *AdapterMemory[K, V]) Remove(ctx context.Context, keys ...K) (v V, err error)
- func (c *AdapterMemory[K, V]) Set(ctx context.Context, key K, value V, duration time.Duration) error
- func (c *AdapterMemory[K, V]) SetIfNotExist(ctx context.Context, key K, value V, duration time.Duration) (bool, error)
- func (c *AdapterMemory[K, V]) SetIfNotExistFunc(ctx context.Context, key K, f Func[V], duration time.Duration) (bool, error)
- func (c *AdapterMemory[K, V]) SetIfNotExistFuncLock(ctx context.Context, key K, f Func[V], duration time.Duration) (bool, error)
- func (c *AdapterMemory[K, V]) SetMap(ctx context.Context, data map[K]V, duration time.Duration) error
- func (c *AdapterMemory[K, V]) Size(ctx context.Context) (size int, err error)
- func (c *AdapterMemory[K, V]) Update(ctx context.Context, key K, value V) (oldValue V, exist bool, err error)
- func (c *AdapterMemory[K, V]) UpdateExpire(ctx context.Context, key K, duration time.Duration) (oldDuration time.Duration, err error)
- func (c *AdapterMemory[K, V]) Values(ctx context.Context) ([]V, error)
- type Cache
- func (c *Cache[K, V]) GetAdapter() Adapter[K, V]
- func (c *Cache[K, V]) KeyStrings(ctx context.Context) ([]string, error)
- func (c *Cache[K, V]) MustContains(ctx context.Context, key K) bool
- func (c *Cache[K, V]) MustData(ctx context.Context) map[K]V
- func (c *Cache[K, V]) MustGet(ctx context.Context, key K) V
- func (c *Cache[K, V]) MustGetExpire(ctx context.Context, key K) time.Duration
- func (c *Cache[K, V]) MustGetOrSet(ctx context.Context, key K, value V, duration time.Duration) V
- func (c *Cache[K, V]) MustGetOrSetFunc(ctx context.Context, key K, f Func[V], duration time.Duration) V
- func (c *Cache[K, V]) MustGetOrSetFuncLock(ctx context.Context, key K, f Func[V], duration time.Duration) V
- func (c *Cache[K, V]) MustKeyStrings(ctx context.Context) []string
- func (c *Cache[K, V]) MustKeys(ctx context.Context) []K
- func (c *Cache[K, V]) MustSize(ctx context.Context) int
- func (c *Cache[K, V]) MustValues(ctx context.Context) []V
- func (c *Cache[K, V]) Removes(ctx context.Context, keys []K) error
- func (c *Cache[K, V]) SetAdapter(adapter Adapter[K, V])
- type Func
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
Contains checks and returns true if `key` exists in the cache, or else returns false.
func Data ¶
Data returns a copy of all key-value pairs in the cache as map type. Note that this function may lead lots of memory usage, you can implement this function if necessary.
func Get ¶
Get retrieves and returns the associated value of given `key`. It returns nil if it does not exist, or its value is nil, or it's expired. If you would like to check if the `key` exists in the cache, it's better using function Contains.
func GetExpire ¶
GetExpire retrieves and returns the expiration of `key` in the cache.
Note that, It returns 0 if the `key` does not expire. It returns -1 if the `key` does not exist in the cache.
func GetOrSet ¶
GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and returns `value` if `key` does not exist in the cache. The key-value pair expires after `duration`.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing if `value` is a function and the function result is nil.
func GetOrSetFunc ¶
GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of function `f` and returns its result if `key` does not exist in the cache. The key-value pair expires after `duration`.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing if `value` is a function and the function result is nil.
func GetOrSetFuncLock ¶
func GetOrSetFuncLock(ctx context.Context, key any, f Func[any], duration time.Duration) (any, error)
GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of function `f` and returns its result if `key` does not exist in the cache. The key-value pair expires after `duration`.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing if `value` is a function and the function result is nil.
Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within writing mutex lock for concurrent safety purpose.
func KeyStrings ¶
KeyStrings returns all keys in the cache as string slice.
func MustContains ¶
MustContains acts like Contains, but it panics if any error occurs.
func MustGetExpire ¶
MustGetExpire acts like GetExpire, but it panics if any error occurs.
func MustGetOrSet ¶
MustGetOrSet acts like GetOrSet, but it panics if any error occurs.
func MustGetOrSetFunc ¶
MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs.
func MustGetOrSetFuncLock ¶
MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs.
func MustKeyStrings ¶
MustKeyStrings acts like KeyStrings, but it panics if any error occurs.
func MustValues ¶
MustValues acts like Values, but it panics if any error occurs.
func Remove ¶
Remove deletes one or more keys from cache, and returns its value. If multiple keys are given, it returns the value of the last deleted item.
func Set ¶
Set sets cache with `key`-`value` pair, which is expired after `duration`.
It does not expire if `duration` == 0. It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
func SetIfNotExist ¶
SetIfNotExist sets cache with `key`-`value` pair which is expired after `duration` if `key` does not exist in the cache. It returns true the `key` does not exist in the cache, and it sets `value` successfully to the cache, or else it returns false.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil.
func SetIfNotExistFunc ¶
func SetIfNotExistFunc(ctx context.Context, key any, f Func[any], duration time.Duration) (bool, error)
SetIfNotExistFunc sets `key` with result of function `f` and returns true if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists.
The parameter `value` can be type of `func() interface{}`, but it does nothing if its result is nil.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil.
func SetIfNotExistFuncLock ¶
func SetIfNotExistFuncLock(ctx context.Context, key any, f Func[any], duration time.Duration) (bool, error)
SetIfNotExistFuncLock sets `key` with result of function `f` and returns true if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil.
Note that it differs from function `SetIfNotExistFunc` is that the function `f` is executed within writing mutex lock for concurrent safety purpose.
func SetMap ¶
SetMap batch sets cache with key-value pairs by `data` map, which is expired after `duration`.
It does not expire if `duration` == 0. It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
func Update ¶
Update updates the value of `key` without changing its expiration and returns the old value. The returned value `exist` is false if the `key` does not exist in the cache.
It deletes the `key` if given `value` is nil. It does nothing if `key` does not exist in the cache.
func UpdateExpire ¶
func UpdateExpire(ctx context.Context, key any, duration time.Duration) (oldDuration time.Duration, err error)
UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
It returns -1 and does nothing if the `key` does not exist in the cache. It deletes the `key` if `duration` < 0.
Types ¶
type Adapter ¶
type Adapter[K comparable, V any] interface { // Set sets cache with `key`-`value` pair, which is expired after `duration`. // // It does not expire if `duration` == 0. // It deletes the keys of `data` if `duration` < 0 or given `value` is nil. Set(ctx context.Context, key K, value V, duration time.Duration) error // SetMap batch sets cache with key-value pairs by `data` map, which is expired after `duration`. // // It does not expire if `duration` == 0. // It deletes the keys of `data` if `duration` < 0 or given `value` is nil. SetMap(ctx context.Context, data map[K]V, duration time.Duration) error // SetIfNotExist sets cache with `key`-`value` pair which is expired after `duration` // if `key` does not exist in the cache. It returns true the `key` does not exist in the // cache, and it sets `value` successfully to the cache, or else it returns false. // // It does not expire if `duration` == 0. // It deletes the `key` if `duration` < 0 or given `value` is nil. SetIfNotExist(ctx context.Context, key K, value V, duration time.Duration) (ok bool, err error) // SetIfNotExistFunc sets `key` with result of function `f` and returns true // if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists. // // The parameter `value` can be type of `func() interface{}`, but it does nothing if its // result is nil. // // It does not expire if `duration` == 0. // It deletes the `key` if `duration` < 0 or given `value` is nil. SetIfNotExistFunc(ctx context.Context, key K, f Func[V], duration time.Duration) (ok bool, err error) // SetIfNotExistFuncLock sets `key` with result of function `f` and returns true // if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists. // // It does not expire if `duration` == 0. // It deletes the `key` if `duration` < 0 or given `value` is nil. // // Note that it differs from function `SetIfNotExistFunc` is that the function `f` is executed within // writing mutex lock for concurrent safety purpose. SetIfNotExistFuncLock(ctx context.Context, key K, f Func[V], duration time.Duration) (ok bool, err error) // Get retrieves and returns the associated value of given `key`. // It returns nil if it does not exist, or its value is nil, or it's expired. // If you would like to check if the `key` exists in the cache, it's better using function Contains. Get(ctx context.Context, key K) (V, bool, error) // GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and // returns `value` if `key` does not exist in the cache. The key-value pair expires // after `duration`. // // It does not expire if `duration` == 0. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing // if `value` is a function and the function result is nil. GetOrSet(ctx context.Context, key K, value V, duration time.Duration) (result V, err error) // GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of // function `f` and returns its result if `key` does not exist in the cache. The key-value // pair expires after `duration`. // // It does not expire if `duration` == 0. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing // if `value` is a function and the function result is nil. GetOrSetFunc(ctx context.Context, key K, f Func[V], duration time.Duration) (result V, err error) // GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of // function `f` and returns its result if `key` does not exist in the cache. The key-value // pair expires after `duration`. // // It does not expire if `duration` == 0. // It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing // if `value` is a function and the function result is nil. // // Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within // writing mutex lock for concurrent safety purpose. GetOrSetFuncLock(ctx context.Context, key K, f Func[V], duration time.Duration) (result V, err error) // Contains checks and returns true if `key` exists in the cache, or else returns false. Contains(ctx context.Context, key K) (bool, error) // Size returns the number of items in the cache. Size(ctx context.Context) (size int, err error) // Data returns a copy of all key-value pairs in the cache as map type. // Note that this function may lead lots of memory usage, you can implement this function // if necessary. Data(ctx context.Context) (data map[K]V, err error) // Keys returns all keys in the cache as slice. Keys(ctx context.Context) (keys []K, err error) // Values returns all values in the cache as slice. Values(ctx context.Context) (values []V, err error) // Update updates the value of `key` without changing its expiration and returns the old value. // The returned value `exist` is false if the `key` does not exist in the cache. // // It deletes the `key` if given `value` is nil. // It does nothing if `key` does not exist in the cache. Update(ctx context.Context, key K, value V) (oldValue V, exist bool, err error) // UpdateExpire updates the expiration of `key` and returns the old expiration duration value. // // It returns -1 and does nothing if the `key` does not exist in the cache. // It deletes the `key` if `duration` < 0. UpdateExpire(ctx context.Context, key K, duration time.Duration) (oldDuration time.Duration, err error) // GetExpire retrieves and returns the expiration of `key` in the cache. // // Note that, // It returns 0 if the `key` does not expire. // It returns -1 if the `key` does not exist in the cache. GetExpire(ctx context.Context, key K) (time.Duration, error) // Remove deletes one or more keys from cache, and returns its value. // If multiple keys are given, it returns the value of the last deleted item. Remove(ctx context.Context, keys ...K) (lastValue V, err error) // Clear clears all data of the cache. // Note that this function is sensitive and should be carefully used. Clear(ctx context.Context) error // Close closes the cache if necessary. Close(ctx context.Context) error }
Adapter is the core adapter for cache features implements.
Note that the implementer itself should guarantee the concurrent safety of these functions.
type AdapterMemory ¶
type AdapterMemory[K comparable, V any] struct { // contains filtered or unexported fields }
AdapterMemory is an adapter implements using memory.
func NewAdapterMemory ¶
func NewAdapterMemory[K comparable, V any](lruCap ...int) *AdapterMemory[K, V]
NewAdapterMemory creates and returns a new memory cache object.
func (*AdapterMemory[K, V]) Clear ¶
func (c *AdapterMemory[K, V]) Clear(ctx context.Context) error
Clear clears all data of the cache. Note that this function is sensitive and should be carefully used.
func (*AdapterMemory[K, V]) Close ¶
func (c *AdapterMemory[K, V]) Close(ctx context.Context) error
Close closes the cache.
func (*AdapterMemory[K, V]) Contains ¶
func (c *AdapterMemory[K, V]) Contains(ctx context.Context, key K) (ok bool, err error)
Contains checks and returns true if `key` exists in the cache, or else returns false.
func (*AdapterMemory[K, V]) Data ¶
func (c *AdapterMemory[K, V]) Data(ctx context.Context) (map[K]V, error)
Data returns a copy of all key-value pairs in the cache as map type.
func (*AdapterMemory[K, V]) Get ¶
func (c *AdapterMemory[K, V]) Get(ctx context.Context, key K) (v V, ok bool, err error)
Get retrieves and returns the associated value of given `key`. It returns nil if it does not exist, or its value is nil, or it's expired. If you would like to check if the `key` exists in the cache, it's better using function Contains.
func (*AdapterMemory[K, V]) GetExpire ¶
GetExpire retrieves and returns the expiration of `key` in the cache.
Note that, It returns 0 if the `key` does not expire. It returns -1 if the `key` does not exist in the cache.
func (*AdapterMemory[K, V]) GetOrSet ¶
func (c *AdapterMemory[K, V]) GetOrSet(ctx context.Context, key K, value V, duration time.Duration) (V, error)
GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and returns `value` if `key` does not exist in the cache. The key-value pair expires after `duration`.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing if `value` is a function and the function result is nil.
func (*AdapterMemory[K, V]) GetOrSetFunc ¶
func (c *AdapterMemory[K, V]) GetOrSetFunc(ctx context.Context, key K, f Func[V], duration time.Duration) (v V, err error)
GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of function `f` and returns its result if `key` does not exist in the cache. The key-value pair expires after `duration`.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing if `value` is a function and the function result is nil.
func (*AdapterMemory[K, V]) GetOrSetFuncLock ¶
func (c *AdapterMemory[K, V]) GetOrSetFuncLock(ctx context.Context, key K, f Func[V], duration time.Duration) (V, error)
GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of function `f` and returns its result if `key` does not exist in the cache. The key-value pair expires after `duration`.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing if `value` is a function and the function result is nil.
Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within writing mutex lock for concurrent safety purpose.
func (*AdapterMemory[K, V]) Keys ¶
func (c *AdapterMemory[K, V]) Keys(ctx context.Context) ([]K, error)
Keys returns all keys in the cache as slice.
func (*AdapterMemory[K, V]) Remove ¶
func (c *AdapterMemory[K, V]) Remove(ctx context.Context, keys ...K) (v V, err error)
Remove deletes one or more keys from cache, and returns its value. If multiple keys are given, it returns the value of the last deleted item.
func (*AdapterMemory[K, V]) Set ¶
func (c *AdapterMemory[K, V]) Set(ctx context.Context, key K, value V, duration time.Duration) error
Set sets cache with `key`-`value` pair, which is expired after `duration`.
It does not expire if `duration` == 0. It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
func (*AdapterMemory[K, V]) SetIfNotExist ¶
func (c *AdapterMemory[K, V]) SetIfNotExist(ctx context.Context, key K, value V, duration time.Duration) (bool, error)
SetIfNotExist sets cache with `key`-`value` pair which is expired after `duration` if `key` does not exist in the cache. It returns true the `key` does not exist in the cache, and it sets `value` successfully to the cache, or else it returns false.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil.
func (*AdapterMemory[K, V]) SetIfNotExistFunc ¶
func (c *AdapterMemory[K, V]) SetIfNotExistFunc(ctx context.Context, key K, f Func[V], duration time.Duration) (bool, error)
SetIfNotExistFunc sets `key` with result of function `f` and returns true if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists.
The parameter `value` can be type of `func() interface{}`, but it does nothing if its result is nil.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil.
func (*AdapterMemory[K, V]) SetIfNotExistFuncLock ¶
func (c *AdapterMemory[K, V]) SetIfNotExistFuncLock(ctx context.Context, key K, f Func[V], duration time.Duration) (bool, error)
SetIfNotExistFuncLock sets `key` with result of function `f` and returns true if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists.
It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil.
Note that it differs from function `SetIfNotExistFunc` is that the function `f` is executed within writing mutex lock for concurrent safety purpose.
func (*AdapterMemory[K, V]) SetMap ¶
func (c *AdapterMemory[K, V]) SetMap(ctx context.Context, data map[K]V, duration time.Duration) error
SetMap batch sets cache with key-value pairs by `data` map, which is expired after `duration`.
It does not expire if `duration` == 0. It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
func (*AdapterMemory[K, V]) Size ¶
func (c *AdapterMemory[K, V]) Size(ctx context.Context) (size int, err error)
Size returns the size of the cache.
func (*AdapterMemory[K, V]) Update ¶
func (c *AdapterMemory[K, V]) Update(ctx context.Context, key K, value V) (oldValue V, exist bool, err error)
Update updates the value of `key` without changing its expiration and returns the old value. The returned value `exist` is false if the `key` does not exist in the cache.
It deletes the `key` if given `value` is nil. It does nothing if `key` does not exist in the cache.
func (*AdapterMemory[K, V]) UpdateExpire ¶
func (c *AdapterMemory[K, V]) UpdateExpire(ctx context.Context, key K, duration time.Duration) (oldDuration time.Duration, err error)
UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
It returns -1 and does nothing if the `key` does not exist in the cache. It deletes the `key` if `duration` < 0.
type Cache ¶
type Cache[K comparable, V any] struct { Adapter[K, V] }
Cache struct.
func New ¶
func New[K comparable, V any](lruCap ...int) *Cache[K, V]
New creates and returns a new cache object using default memory adapter. Note that the LRU feature is only available using memory adapter.
Example ¶
// Create a cache object, // Of course, you can also easily use the gcache package method directly. c := gcache.New[string, string]() // Set cache without expiration c.Set(ctx, "k1", "v1", 0) // Get cache v, _, _ := c.Get(ctx, "k1") fmt.Println(v) // Get cache size n, _ := c.Size(ctx) fmt.Println(n) // Does the specified key name exist in the cache b, _ := c.Contains(ctx, "k1") fmt.Println(b) // Delete and return the deleted key value fmt.Println(c.Remove(ctx, "k1")) // Close the cache object and let the GC reclaim resources c.Close(ctx)
Output: v1 1 true v1 <nil>
func NewWithAdapter ¶
func NewWithAdapter[K comparable, V any](adapter Adapter[K, V]) *Cache[K, V]
NewWithAdapter creates and returns a Cache object with given Adapter implements.
func (*Cache[K, V]) GetAdapter ¶
GetAdapter returns the adapter that is set in current Cache.
func (*Cache[K, V]) KeyStrings ¶
KeyStrings returns all keys in the cache as string slice.
Example ¶
c := gcache.New[string, string]()
c.SetMap(ctx, map[string]string{"k1": "v1", "k2": "v2"}, 0)
// KeyStrings returns all keys in the cache as string slice.
keys, _ := c.KeyStrings(ctx)
fmt.Println(keys)
// May Output:
// [k1 k2]
func (*Cache[K, V]) MustContains ¶
MustContains acts like Contains, but it panics if any error occurs.
Example ¶
// Create a cache object, // Of course, you can also easily use the gcache package method directly c := gcache.New[string, string]() // Set Cache c.Set(ctx, "k", "v", 0) // MustContains returns true if `key` exists in the cache, or else returns false. // return true data := c.MustContains(ctx, "k") fmt.Println(data) // return false data1 := c.MustContains(ctx, "k1") fmt.Println(data1)
Output: true false
func (*Cache[K, V]) MustData ¶
MustData acts like Data, but it panics if any error occurs.
Example ¶
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New[string, string]()
c.SetMap(ctx, map[string]string{"k1": "v1", "k2": "v2"}, 0)
data := c.MustData(ctx)
fmt.Println(data)
// May Output:
// map[k1:v1 k2:v2]
func (*Cache[K, V]) MustGet ¶
MustGet acts like Get, but it panics if any error occurs.
Example ¶
// Intercepting panic exception information
// err is empty, so panic is not performed
defer func() {
if r := recover(); r != nil {
fmt.Println("recover...:", r)
}
}()
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New[string, string]()
// Set Cache Object
c.Set(ctx, "k1", "v1", 0)
// MustGet acts like Get, but it panics if any error occurs.
k2 := c.MustGet(ctx, "k2")
fmt.Println(k2)
k1 := c.MustGet(ctx, "k1")
fmt.Println(k1)
Output: v1
func (*Cache[K, V]) MustGetExpire ¶
MustGetExpire acts like GetExpire, but it panics if any error occurs.
Example ¶
// Create a cache object, // Of course, you can also easily use the gcache package method directly c := gcache.New[string, string]() // Set cache without expiration c.Set(ctx, "k", "v", 10000*time.Millisecond) // MustGetExpire acts like GetExpire, but it panics if any error occurs. expire := c.MustGetExpire(ctx, "k") fmt.Println(expire) // May Output: // 10s
func (*Cache[K, V]) MustGetOrSet ¶
MustGetOrSet acts like GetOrSet, but it panics if any error occurs.
Example ¶
// Create a cache object, // Of course, you can also easily use the gcache package method directly c := gcache.New[string, string]() // MustGetOrSet acts like GetOrSet, but it panics if any error occurs. k1 := c.MustGetOrSet(ctx, "k1", "v1", 0) fmt.Println(k1) k2 := c.MustGetOrSet(ctx, "k1", "v2", 0) fmt.Println(k2)
Output: v1 v1
func (*Cache[K, V]) MustGetOrSetFunc ¶
func (c *Cache[K, V]) MustGetOrSetFunc(ctx context.Context, key K, f Func[V], duration time.Duration) V
MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs.
Example ¶
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New[string, string]()
// MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs.
c.MustGetOrSetFunc(ctx, "k1", func() (value string, err error) {
return "v1", nil
}, 10000*time.Millisecond)
v := c.MustGet(ctx, "k1")
fmt.Println(v)
c.MustGetOrSetFunc(ctx, "k2", func() (value string, err error) {
return "", nil
}, 10000*time.Millisecond)
v1 := c.MustGet(ctx, "k2")
fmt.Println(v1)
Output: v1
func (*Cache[K, V]) MustGetOrSetFuncLock ¶
func (c *Cache[K, V]) MustGetOrSetFuncLock(ctx context.Context, key K, f Func[V], duration time.Duration) V
MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs.
Example ¶
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New[string, string]()
// MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs.
c.MustGetOrSetFuncLock(ctx, "k1", func() (value string, err error) {
return "v1", nil
}, 0)
v := c.MustGet(ctx, "k1")
fmt.Println(v)
// Modification failed
c.MustGetOrSetFuncLock(ctx, "k1", func() (value string, err error) {
return "update v1", nil
}, 0)
v = c.MustGet(ctx, "k1")
fmt.Println(v)
Output: v1 v1
func (*Cache[K, V]) MustKeyStrings ¶
MustKeyStrings acts like KeyStrings, but it panics if any error occurs.
Example ¶
c := gcache.New[string, string]()
c.SetMap(ctx, map[string]string{"k1": "v1", "k2": "v2"}, 0)
// MustKeyStrings returns all keys in the cache as string slice.
// MustKeyStrings acts like KeyStrings, but it panics if any error occurs.
keys := c.MustKeyStrings(ctx)
fmt.Println(keys)
// May Output:
// [k1 k2]
func (*Cache[K, V]) MustKeys ¶
MustKeys acts like Keys, but it panics if any error occurs.
Example ¶
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New[string, string]()
c.SetMap(ctx, map[string]string{"k1": "v1", "k2": "v2"}, 0)
// MustKeys acts like Keys, but it panics if any error occurs.
keys1 := c.MustKeys(ctx)
fmt.Println(keys1)
// May Output:
// [k1 k2]
func (*Cache[K, V]) MustSize ¶
MustSize acts like Size, but it panics if any error occurs.
Example ¶
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New[int, int]()
// Add 10 elements without expiration
for i := 0; i < 10; i++ {
c.Set(ctx, i, i, 0)
}
// Size returns the number of items in the cache.
n := c.MustSize(ctx)
fmt.Println(n)
Output: 10
func (*Cache[K, V]) MustValues ¶
MustValues acts like Values, but it panics if any error occurs.
Example ¶
// Create a cache object, // Of course, you can also easily use the gcache package method directly c := gcache.New[string, string]() // Write value c.Set(ctx, "k1", "v1", 0) // MustValues returns all values in the cache as slice. data := c.MustValues(ctx) fmt.Println(data)
Output: [v1]
func (*Cache[K, V]) Removes ¶
Removes deletes `keys` in the cache.
Example ¶
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New[string, string]()
c.SetMap(ctx, map[string]string{"k1": "v1", "k2": "v2", "k3": "v3", "k4": "v4"}, 0)
// Remove deletes one or more keys from cache, and returns its value.
// If multiple keys are given, it returns the value of the last deleted item.
c.Removes(ctx, []string{"k1", "k2", "k3"})
data, _ := c.Data(ctx)
fmt.Println(data)
Output: map[k4:v4]
func (*Cache[K, V]) SetAdapter ¶
SetAdapter changes the adapter for this cache. Be very note that, this setting function is not concurrent-safe, which means you should not call this setting function concurrently in multiple goroutines.