Documentation
¶
Index ¶
- func Clear(ctx context.Context) error
- func Contains(ctx context.Context, key string) (bool, error)
- func Data(ctx context.Context) (map[string]interface{}, error)
- func Get(ctx context.Context, key string) (*mvar.Var, error)
- func GetExpire(ctx context.Context, key string) (time.Duration, error)
- func GetOrSet(ctx context.Context, key string, value interface{}, duration time.Duration) (*mvar.Var, error)
- func GetOrSetFunc(ctx context.Context, key string, f Func, duration time.Duration) (*mvar.Var, error)
- func GetOrSetFuncLock(ctx context.Context, key string, f Func, duration time.Duration) (*mvar.Var, error)
- func Keys(ctx context.Context) ([]string, error)
- func Remove(ctx context.Context, keys ...string) (lastValue *mvar.Var, err error)
- func Set(ctx context.Context, key string, value interface{}, duration time.Duration) error
- func SetIfNotExist(ctx context.Context, key string, value interface{}, duration time.Duration) (bool, error)
- func SetIfNotExistFunc(ctx context.Context, key string, f Func, duration time.Duration) (bool, error)
- func SetIfNotExistFuncLock(ctx context.Context, key string, f Func, duration time.Duration) (bool, error)
- func SetMap(ctx context.Context, data map[string]interface{}, duration time.Duration) error
- func Size(ctx context.Context) (int, error)
- func Update(ctx context.Context, key string, value interface{}) (oldValue *mvar.Var, exist bool, err error)
- func UpdateExpire(ctx context.Context, key string, duration time.Duration) (oldDuration time.Duration, err error)
- func Values(ctx context.Context) ([]interface{}, error)
- type Adapter
- type AdapterMemory
- func (c *AdapterMemory) Clear(_ context.Context) error
- func (c *AdapterMemory) Close(_ context.Context) error
- func (c *AdapterMemory) Contains(ctx context.Context, key string) (bool, error)
- func (c *AdapterMemory) Data(_ context.Context) (map[string]any, error)
- func (c *AdapterMemory) Get(_ context.Context, key string) (*mvar.Var, error)
- func (c *AdapterMemory) GetExpire(_ context.Context, key string) (time.Duration, error)
- func (c *AdapterMemory) GetOrSet(_ context.Context, key string, value interface{}, duration time.Duration) (*mvar.Var, error)
- func (c *AdapterMemory) GetOrSetFunc(ctx context.Context, key string, f Func, duration time.Duration) (*mvar.Var, error)
- func (c *AdapterMemory) GetOrSetFuncLock(ctx context.Context, key string, f Func, duration time.Duration) (*mvar.Var, error)
- func (c *AdapterMemory) Keys(_ context.Context) ([]string, error)
- func (c *AdapterMemory) Remove(_ context.Context, keys ...string) (*mvar.Var, error)
- func (c *AdapterMemory) Set(_ context.Context, key string, value interface{}, duration time.Duration) error
- func (c *AdapterMemory) SetIfNotExist(_ context.Context, key string, value interface{}, duration time.Duration) (bool, error)
- func (c *AdapterMemory) SetIfNotExistFunc(ctx context.Context, key string, f Func, duration time.Duration) (bool, error)
- func (c *AdapterMemory) SetIfNotExistFuncLock(ctx context.Context, key string, f Func, duration time.Duration) (bool, error)
- func (c *AdapterMemory) SetMap(_ context.Context, data map[string]interface{}, duration time.Duration) error
- func (c *AdapterMemory) Size(_ context.Context) (int, error)
- func (c *AdapterMemory) Update(_ context.Context, key string, value any) (oldValue *mvar.Var, exist bool, err error)
- func (c *AdapterMemory) UpdateExpire(_ context.Context, key string, duration time.Duration) (oldDuration time.Duration, err error)
- func (c *AdapterMemory) Values(_ context.Context) ([]any, error)
- type Cache
- type Func
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clear ¶
Clear clears all data of the cache. Note that this function is sensitive and should be carefully used.
func Contains ¶
Contains checks and returns true if `key` exists in the cache, or else returns false.
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.
func GetOrSet ¶
func GetOrSet(ctx context.Context, key string, value interface{}, duration time.Duration) (*mvar.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.
func GetOrSetFunc ¶
func GetOrSetFunc(ctx context.Context, key string, f Func, duration time.Duration) (*mvar.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.
func GetOrSetFuncLock ¶
func GetOrSetFuncLock(ctx context.Context, key string, f Func, duration time.Duration) (*mvar.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 is recommended to use this function instead of `GetOrSetFunc` if you think there might be concurrent insertions to the same `key`.
func Set ¶
Set sets cache with `key`-`value` pair, which is expired after `duration`. It does not expire if `duration` == 0.
func SetIfNotExist ¶
func SetIfNotExist(ctx context.Context, key string, value interface{}, duration time.Duration) (bool, error)
SetIfNotExist sets cache with `key`-`value` pair if `key` does not exist in the cache. It returns true if `key` is set, or returns false if the `key` already exists.
func SetIfNotExistFunc ¶
func SetIfNotExistFunc(ctx context.Context, key string, f Func, duration time.Duration) (bool, error)
SetIfNotExistFunc sets `key` with result of function `f` if `key` does not exist in the cache. It returns true if `key` is set, or returns false if the `key` already exists. The function `f` is executed only if `key` does not exist in the cache.
func SetIfNotExistFuncLock ¶
func SetIfNotExistFuncLock(ctx context.Context, key string, f Func, duration time.Duration) (bool, error)
SetIfNotExistFuncLock sets `key` with result of function `f` if `key` does not exist in the cache. It returns true if `key` is set, or returns false if the `key` already exists. The function `f` is executed only if `key` does not exist in the cache. It is recommended to use this function instead of `SetIfNotExistFunc` if you think there might be concurrent insertions to the same `key`.
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.
func Update ¶
func Update(ctx context.Context, key string, value interface{}) (oldValue *mvar.Var, exist bool, err error)
Update updates the value of `key` without changing its expiration and returns the old value.
Types ¶
type Adapter ¶
type Adapter interface {
// Set sets cache with `key`-`value` pair, which is expired after `duration`.
// It does not expire if `duration` is 0.
// It deletes the key if `duration` < 0 or given `value` is nil.
Set(ctx context.Context, key string, value interface{}, duration time.Duration) error
// SetMap batch sets cache with key-value pairs by `data` map, which is expired after `duration`.
SetMap(ctx context.Context, data map[string]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 if the `key` does not exist in the
// cache and it sets `value` successfully to the cache, otherwise it returns false.
SetIfNotExist(ctx context.Context, key string, 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.
SetIfNotExistFunc(ctx context.Context, key string, f Func, 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 executes function `f` within writing mutex lock for concurrent safety purpose.
SetIfNotExistFuncLock(ctx context.Context, key string, f Func, 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.
Get(ctx context.Context, key string) (*mvar.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`.
GetOrSet(ctx context.Context, key string, value interface{}, duration time.Duration) (result *mvar.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.
GetOrSetFunc(ctx context.Context, key string, f Func, duration time.Duration) (result *mvar.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.
// It executes function `f` within writing mutex lock for concurrent safety purpose.
GetOrSetFuncLock(ctx context.Context, key string, f Func, duration time.Duration) (result *mvar.Var, err error)
// Contains checks and returns true if `key` exists in the cache, or else returns false.
Contains(ctx context.Context, key string) (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.
Data(ctx context.Context) (data map[string]any, err error)
// Keys returns all keys in the cache as slice.
Keys(ctx context.Context) (keys []string, 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.
Update(ctx context.Context, key string, value interface{}) (oldValue *mvar.Var, exist bool, err error)
// UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
UpdateExpire(ctx context.Context, key string, duration time.Duration) (oldDuration time.Duration, err error)
// GetExpire retrieves and returns the expiration of `key` in the cache.
GetExpire(ctx context.Context, key string) (time.Duration, error)
// Remove deletes one or more keys from cache.
Remove(ctx context.Context, keys ...string) (lastValue *mvar.Var, err error)
// Clear clears all data of the cache.
Clear(ctx context.Context) error
// Close closes the cache if necessary.
Close(ctx context.Context) error
}
Adapter is the adapter for cache features.
func NewAdapterMemory ¶
NewAdapterMemory creates and returns a new memory adapter.
type AdapterMemory ¶
type AdapterMemory struct {
// contains filtered or unexported fields
}
AdapterMemory is an adapter for memory cache. It is thread-safe and supports LRU elimination.
func (*AdapterMemory) Clear ¶
func (c *AdapterMemory) Clear(_ context.Context) error
Clear clears all data of the cache.
func (*AdapterMemory) Close ¶
func (c *AdapterMemory) Close(_ context.Context) error
Close closes the cache and stops the cleanup goroutine.
func (*AdapterMemory) Contains ¶
Contains checks and returns true if `key` exists in the cache, or else returns false.
func (*AdapterMemory) GetExpire ¶
GetExpire retrieves and returns the expiration of `key` in the cache.
func (*AdapterMemory) GetOrSet ¶
func (c *AdapterMemory) GetOrSet(_ context.Context, key string, value interface{}, duration time.Duration) (*mvar.Var, error)
GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and returns `value`.
func (*AdapterMemory) GetOrSetFunc ¶
func (c *AdapterMemory) GetOrSetFunc(ctx context.Context, key string, f Func, duration time.Duration) (*mvar.Var, error)
GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of function `f`.
func (*AdapterMemory) GetOrSetFuncLock ¶
func (c *AdapterMemory) GetOrSetFuncLock(ctx context.Context, key string, f Func, duration time.Duration) (*mvar.Var, error)
GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of function `f` with lock.
func (*AdapterMemory) Keys ¶
func (c *AdapterMemory) Keys(_ context.Context) ([]string, error)
Keys returns all keys in the cache as slice.
func (*AdapterMemory) Set ¶
func (c *AdapterMemory) Set(_ context.Context, key string, value interface{}, duration time.Duration) error
Set sets cache with `key`-`value` pair.
func (*AdapterMemory) SetIfNotExist ¶
func (c *AdapterMemory) SetIfNotExist(_ context.Context, key string, value interface{}, duration time.Duration) (bool, error)
SetIfNotExist sets cache with `key`-`value` pair if `key` does not exist in the cache.
func (*AdapterMemory) SetIfNotExistFunc ¶
func (c *AdapterMemory) SetIfNotExistFunc(ctx context.Context, key string, f Func, duration time.Duration) (bool, error)
SetIfNotExistFunc sets `key` with result of function `f`.
func (*AdapterMemory) SetIfNotExistFuncLock ¶
func (c *AdapterMemory) SetIfNotExistFuncLock(ctx context.Context, key string, f Func, duration time.Duration) (bool, error)
SetIfNotExistFuncLock sets `key` with result of function `f` with lock.
func (*AdapterMemory) SetMap ¶
func (c *AdapterMemory) SetMap(_ context.Context, data map[string]interface{}, duration time.Duration) error
SetMap batch sets cache with key-value pairs by `data` map.
func (*AdapterMemory) Size ¶
func (c *AdapterMemory) Size(_ context.Context) (int, error)
Size returns the size of the cache.
func (*AdapterMemory) Update ¶
func (c *AdapterMemory) Update(_ context.Context, key string, value any) (oldValue *mvar.Var, exist bool, err error)
Update updates the value of `key` without changing its expiration and returns the old value.
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) GetAdapter ¶
GetAdapter returns the adapter that is set in current Cache.
func (*Cache) KeyStrings ¶
KeyStrings returns all keys in the cache as string slice.
func (*Cache) SetAdapter ¶
SetAdapter changes the adapter for this cache.