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 interface{}) (bool, error)
- func Data(ctx context.Context) (map[interface{}]interface{}, error)
- func Get(ctx context.Context, key interface{}) (*gvar.Var, error)
- func GetExpire(ctx context.Context, key interface{}) (time.Duration, error)
- func GetOrSet(ctx context.Context, key interface{}, value interface{}, ...) (*gvar.Var, error)
- func GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), ...) (*gvar.Var, error)
- func GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), ...) (*gvar.Var, error)
- func KeyStrings(ctx context.Context) ([]string, error)
- func Keys(ctx context.Context) ([]interface{}, error)
- func MustContains(ctx context.Context, key interface{}) bool
- func MustData(ctx context.Context) map[interface{}]interface{}
- func MustGet(ctx context.Context, key interface{}) *gvar.Var
- func MustGetExpire(ctx context.Context, key interface{}) time.Duration
- func MustGetOrSet(ctx context.Context, key interface{}, value interface{}, ...) *gvar.Var
- func MustGetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), ...) *gvar.Var
- func MustGetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), ...) *gvar.Var
- func MustKeyStrings(ctx context.Context) []string
- func MustKeys(ctx context.Context) []interface{}
- func MustSize(ctx context.Context) int
- func MustValues(ctx context.Context) []interface{}
- func Remove(ctx context.Context, keys ...interface{}) (value *gvar.Var, err error)
- func Removes(ctx context.Context, keys []interface{}) error
- func Set(ctx context.Context, key interface{}, value interface{}, ...) error
- func SetIfNotExist(ctx context.Context, key interface{}, value interface{}, ...) (bool, error)
- func SetIfNotExistFunc(ctx context.Context, key interface{}, f func() (interface{}, error), ...) (bool, error)
- func SetIfNotExistFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), ...) (bool, error)
- func SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error
- func Size(ctx context.Context) (int, error)
- func Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error)
- func UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)
- func Values(ctx context.Context) ([]interface{}, error)
- type Adapter
- type AdapterMemory
- func (c *AdapterMemory) Clear(ctx context.Context) error
- func (c *AdapterMemory) Close(ctx context.Context) error
- func (c *AdapterMemory) Contains(ctx context.Context, key interface{}) (bool, error)
- func (c *AdapterMemory) Data(ctx context.Context) (map[interface{}]interface{}, error)
- func (c *AdapterMemory) Get(ctx context.Context, key interface{}) (*gvar.Var, error)
- func (c *AdapterMemory) GetExpire(ctx context.Context, key interface{}) (time.Duration, error)
- func (c *AdapterMemory) GetOrSet(ctx context.Context, key interface{}, value interface{}, ...) (*gvar.Var, error)
- func (c *AdapterMemory) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), ...) (*gvar.Var, error)
- func (c *AdapterMemory) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), ...) (*gvar.Var, error)
- func (c *AdapterMemory) Keys(ctx context.Context) ([]interface{}, error)
- func (c *AdapterMemory) Remove(ctx context.Context, keys ...interface{}) (*gvar.Var, error)
- func (c *AdapterMemory) Set(ctx context.Context, key interface{}, value interface{}, ...) error
- func (c *AdapterMemory) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, ...) (bool, error)
- func (c *AdapterMemory) SetIfNotExistFunc(ctx context.Context, key interface{}, f func() (interface{}, error), ...) (bool, error)
- func (c *AdapterMemory) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), ...) (bool, error)
- func (c *AdapterMemory) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error
- func (c *AdapterMemory) Size(ctx context.Context) (size int, err error)
- func (c *AdapterMemory) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error)
- func (c *AdapterMemory) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)
- func (c *AdapterMemory) Values(ctx context.Context) ([]interface{}, error)
- type Cache
- func (c *Cache) KeyStrings(ctx context.Context) ([]string, error)
- func (c *Cache) MustContains(ctx context.Context, key interface{}) bool
- func (c *Cache) MustData(ctx context.Context) map[interface{}]interface{}
- func (c *Cache) MustGet(ctx context.Context, key interface{}) *gvar.Var
- func (c *Cache) MustGetExpire(ctx context.Context, key interface{}) time.Duration
- func (c *Cache) MustGetOrSet(ctx context.Context, key interface{}, value interface{}, ...) *gvar.Var
- func (c *Cache) MustGetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), ...) *gvar.Var
- func (c *Cache) MustGetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), ...) *gvar.Var
- func (c *Cache) MustKeyStrings(ctx context.Context) []string
- func (c *Cache) MustKeys(ctx context.Context) []interface{}
- func (c *Cache) MustSize(ctx context.Context) int
- func (c *Cache) MustValues(ctx context.Context) []interface{}
- func (c *Cache) Removes(ctx context.Context, keys []interface{}) error
- func (c *Cache) SetAdapter(adapter Adapter)
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 ¶
func GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (*gvar.Var, 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 GetOrSetFunc ¶
func GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, 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 GetOrSetFuncLock ¶
func GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, 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 ¶
func MustGetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) *gvar.Var
MustGetOrSet acts like GetOrSet, but it panics if any error occurs.
func MustGetOrSetFunc ¶
func MustGetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) *gvar.Var
MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs.
func MustGetOrSetFuncLock ¶
func MustGetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) *gvar.Var
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 ¶
func SetIfNotExist(ctx context.Context, key interface{}, value interface{}, 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 SetIfNotExistFunc ¶
func SetIfNotExistFunc(ctx context.Context, key interface{}, f func() (interface{}, error), 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 interface{}, f func() (interface{}, error), 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 ¶
func Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, 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 UpdateExpire ¶
func UpdateExpire(ctx context.Context, key interface{}, 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 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 interface{}, value interface{}, 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[interface{}]interface{}, 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 interface{}, value interface{}, 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 interface{}, f func() (interface{}, error), 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 interface{}, f func() (interface{}, error), 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 interface{}) (*gvar.Var, 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 interface{}, value interface{}, duration time.Duration) (result *gvar.Var, 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 interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, 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 interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error)
// Contains checks and returns true if `key` exists in the cache, or else returns false.
Contains(ctx context.Context, key interface{}) (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[interface{}]interface{}, err error)
// Keys returns all keys in the cache as slice.
Keys(ctx context.Context) (keys []interface{}, err error)
// Values returns all values in the cache as slice.
Values(ctx context.Context) (values []interface{}, 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 interface{}, value interface{}) (oldValue *gvar.Var, 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 interface{}, 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 interface{}) (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 ...interface{}) (lastValue *gvar.Var, 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 struct {
// contains filtered or unexported fields
}
AdapterMemory is an adapter implements using memory.
func NewAdapterMemory ¶
func NewAdapterMemory(lruCap ...int) *AdapterMemory
NewAdapterMemory creates and returns a new memory cache object.
func (*AdapterMemory) Clear ¶
func (c *AdapterMemory) 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) Close ¶
func (c *AdapterMemory) Close(ctx context.Context) error
Close closes the cache.
func (*AdapterMemory) Contains ¶
func (c *AdapterMemory) Contains(ctx context.Context, key interface{}) (bool, error)
Contains checks and returns true if `key` exists in the cache, or else returns false.
func (*AdapterMemory) Data ¶
func (c *AdapterMemory) Data(ctx context.Context) (map[interface{}]interface{}, error)
Data returns a copy of all key-value pairs in the cache as map type.
func (*AdapterMemory) 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 (*AdapterMemory) 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) GetOrSet ¶
func (c *AdapterMemory) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (*gvar.Var, 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) GetOrSetFunc ¶
func (c *AdapterMemory) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, 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) GetOrSetFuncLock ¶
func (c *AdapterMemory) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (*gvar.Var, 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) Keys ¶
func (c *AdapterMemory) Keys(ctx context.Context) ([]interface{}, error)
Keys returns all keys in the cache as slice.
func (*AdapterMemory) 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 (*AdapterMemory) Set ¶
func (c *AdapterMemory) Set(ctx context.Context, key interface{}, value interface{}, 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) SetIfNotExist ¶
func (c *AdapterMemory) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, 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) SetIfNotExistFunc ¶
func (c *AdapterMemory) SetIfNotExistFunc(ctx context.Context, key interface{}, f func() (interface{}, error), 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) SetIfNotExistFuncLock ¶
func (c *AdapterMemory) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), 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) SetMap ¶
func (c *AdapterMemory) SetMap(ctx context.Context, data map[interface{}]interface{}, 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) Size ¶
func (c *AdapterMemory) Size(ctx context.Context) (size int, err error)
Size returns the size of the cache.
func (*AdapterMemory) Update ¶
func (c *AdapterMemory) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, 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) UpdateExpire ¶
func (c *AdapterMemory) UpdateExpire(ctx context.Context, key interface{}, 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 struct {
// contains filtered or unexported fields
}
Cache struct.
func New ¶
New creates and returns a new cache object using default memory adapter. Note that the LRU feature is only available using memory adapter.
func NewWithAdapter ¶
NewWithAdapter creates and returns a Cache object with given Adapter implements.
func (*Cache) KeyStrings ¶
KeyStrings returns all keys in the cache as string slice.
func (*Cache) MustContains ¶
MustContains acts like Contains, but it panics if any error occurs.
func (*Cache) MustGetExpire ¶
MustGetExpire acts like GetExpire, but it panics if any error occurs.
func (*Cache) MustGetOrSet ¶
func (c *Cache) MustGetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) *gvar.Var
MustGetOrSet acts like GetOrSet, but it panics if any error occurs.
func (*Cache) MustGetOrSetFunc ¶
func (c *Cache) MustGetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) *gvar.Var
MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs.
func (*Cache) MustGetOrSetFuncLock ¶
func (c *Cache) MustGetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) *gvar.Var
MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs.
func (*Cache) MustKeyStrings ¶
MustKeyStrings acts like KeyStrings, but it panics if any error occurs.
func (*Cache) MustValues ¶
MustValues acts like Values, but it panics if any error occurs.
func (*Cache) 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.