mcache

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear(ctx context.Context) error

Clear clears all data of the cache. Note that this function is sensitive and should be carefully used.

func Contains

func Contains(ctx context.Context, key string) (bool, error)

Contains checks and returns true if `key` exists in the cache, or else returns false.

func Data

func Data(ctx context.Context) (map[string]interface{}, error)

Data returns a copy of all key-value pairs in the cache as map type.

func Get

func Get(ctx context.Context, key string) (*mvar.Var, error)

Get retrieves and returns the associated value of given `key`. It returns nil if it does not exist or its value is nil.

func GetExpire

func GetExpire(ctx context.Context, key string) (time.Duration, error)

GetExpire retrieves and returns the expiration of `key` in the cache.

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 Keys

func Keys(ctx context.Context) ([]string, error)

Keys returns all keys in the cache as slice.

func Remove

func Remove(ctx context.Context, keys ...string) (lastValue *mvar.Var, err error)

Remove deletes one or more keys from cache.

func Set

func Set(ctx context.Context, key string, 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.

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

func SetMap(ctx context.Context, data map[string]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.

func Size

func Size(ctx context.Context) (int, error)

Size returns the size of the cache.

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.

func UpdateExpire

func UpdateExpire(ctx context.Context, key string, duration time.Duration) (oldDuration time.Duration, err error)

UpdateExpire updates the expiration of `key` and returns the old expiration duration value.

func Values

func Values(ctx context.Context) ([]interface{}, error)

Values returns all values in the cache as slice.

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

func NewAdapterMemory(capacity ...int) Adapter

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

func (c *AdapterMemory) Contains(ctx context.Context, key string) (bool, error)

Contains checks and returns true if `key` exists in the cache, or else returns false.

func (*AdapterMemory) Data

func (c *AdapterMemory) Data(_ context.Context) (map[string]any, error)

Data returns a copy of all key-value pairs in the cache as map type.

func (*AdapterMemory) Get

func (c *AdapterMemory) Get(_ context.Context, key string) (*mvar.Var, error)

Get retrieves and returns the associated value of given `key`.

func (*AdapterMemory) GetExpire

func (c *AdapterMemory) GetExpire(_ context.Context, key string) (time.Duration, error)

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) Remove

func (c *AdapterMemory) Remove(_ context.Context, keys ...string) (*mvar.Var, error)

Remove deletes one or more keys from cache.

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.

func (*AdapterMemory) UpdateExpire

func (c *AdapterMemory) UpdateExpire(_ context.Context, key string, duration time.Duration) (oldDuration time.Duration, err error)

UpdateExpire updates the expiration of `key` and returns the old expiration duration value.

func (*AdapterMemory) Values

func (c *AdapterMemory) Values(_ context.Context) ([]any, error)

Values returns all values in the cache as slice.

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache struct.

func New

func New(lruCap ...int) *Cache

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

func NewWithAdapter(adapter Adapter) *Cache

NewWithAdapter creates and returns a Cache object with given Adapter implements.

func (*Cache) GetAdapter

func (c *Cache) GetAdapter() Adapter

GetAdapter returns the adapter that is set in current Cache.

func (*Cache) KeyStrings

func (c *Cache) KeyStrings(ctx context.Context) ([]string, error)

KeyStrings returns all keys in the cache as string slice.

func (*Cache) Removes

func (c *Cache) Removes(ctx context.Context, keys []string) error

Removes deletes `keys` in the cache. It is a wrapper of `Remove` method.

func (*Cache) SetAdapter

func (c *Cache) SetAdapter(adapter Adapter)

SetAdapter changes the adapter for this cache.

type Func

type Func func(ctx context.Context) (value interface{}, err error)

Func is the cache function that calculates and returns the value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL