gcache

package
v1.15.7 Latest Latest
Warning

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

Go to latest
Published: May 2, 2021 License: MIT Imports: 12 Imported by: 70

Documentation

Overview

Package gcache provides kinds of cache management for process. It default provides a concurrent-safe in-memory cache adapter for process.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(key interface{}) (bool, error)

Contains returns true if <key> exists in the cache, or else returns false.

func Data

func Data() (map[interface{}]interface{}, error)

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

func Get

func Get(key interface{}) (interface{}, error)

Get returns the value of <key>. It returns nil if it does not exist or its value is nil.

func GetExpire added in v1.13.4

func GetExpire(key interface{}) (time.Duration, error)

GetExpire retrieves and returns the expiration of <key>. It returns -1 if the <key> does not exist in the cache.

func GetOrSet

func GetOrSet(key interface{}, value interface{}, duration time.Duration) (interface{}, error)

GetOrSet 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(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)

GetOrSetFunc 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(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)

GetOrSetFuncLock 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.

Note that the function <f> is executed within writing mutex lock.

func GetVar added in v1.13.0

func GetVar(key interface{}) (*gvar.Var, error)

GetVar retrieves and returns the value of <key> as gvar.Var.

func KeyStrings

func KeyStrings() ([]string, error)

KeyStrings returns all keys in the cache as string slice.

func Keys

func Keys() ([]interface{}, error)

Keys returns all keys in the cache as slice.

func Remove

func Remove(keys ...interface{}) (value interface{}, err error)

Remove deletes the one or more keys from cache, and returns its value. If multiple keys are given, it returns the value of the deleted last item.

func Removes

func Removes(keys []interface{})

Removes deletes <keys> in the cache. Deprecated, use Remove instead.

func Set

func Set(key interface{}, value interface{}, duration time.Duration)

Set sets cache with <key>-<value> pair, which is expired after <duration>. It does not expire if <duration> == 0.

func SetIfNotExist

func SetIfNotExist(key interface{}, value interface{}, duration time.Duration) (bool, error)

SetIfNotExist sets cache with <key>-<value> pair if <key> does not exist in the cache, which is expired after <duration>. It does not expire if <duration> == 0.

func Sets

func Sets(data map[interface{}]interface{}, duration time.Duration) error

Sets batch sets cache with key-value pairs by <data>, which is expired after <duration>.

It does not expire if <duration> == 0.

func Size

func Size() (int, error)

Size returns the size of the cache.

func Update added in v1.13.4

func Update(key interface{}, value interface{}) (oldValue interface{}, exist bool, err error)

Update updates the value of <key> without changing its expiration and returns the old value. The returned <exist> value is false if the <key> does not exist in the cache.

func UpdateExpire added in v1.13.4

func UpdateExpire(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 if the <key> does not exist in the cache.

func Values

func Values() ([]interface{}, error)

Values returns all values in the cache as slice.

Types

type Adapter added in v1.14.0

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 <key> if <duration> < 0.
	Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error

	// Sets batch sets cache with key-value pairs by <data>, 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.
	Sets(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> dose not exist in the
	// cache and it sets <value> successfully to the cache, or else it returns false.
	//
	// The parameter <value> can be type of <func() interface{}>, but it dose 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.
	SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (bool, error)

	// Get retrieves and returns the associated value of given <key>.
	// It returns nil if it does not exist, its value is nil or it's expired.
	Get(ctx context.Context, key interface{}) (interface{}, 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) (interface{}, 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) (interface{}, 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 does nothing if function <f> returns nil.
	//
	// Note that the function <f> should be executed within writing mutex lock for concurrent
	// safety purpose.
	GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)

	// Contains returns true if <key> exists in the cache, or else returns false.
	Contains(ctx context.Context, key interface{}) (bool, error)

	// GetExpire retrieves and returns the expiration of <key> in the cache.
	//
	// 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{}) (value 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 interface{}, 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)

	// 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 leads lots of memory usage, you can implement this function
	// if necessary.
	Data(ctx context.Context) (map[interface{}]interface{}, error)

	// Keys returns all keys in the cache as slice.
	Keys(ctx context.Context) ([]interface{}, error)

	// Values returns all values in the cache as slice.
	Values(ctx context.Context) ([]interface{}, 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 adapter for cache features implements.

type Cache

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

Cache struct.

func Ctx added in v1.15.2

func Ctx(ctx context.Context) *Cache

Ctx is a chaining function, which shallowly clones current object and sets the context for next operation.

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 (*Cache) Clear

func (c *Cache) Clear() error

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

func (*Cache) Clone added in v1.15.2

func (c *Cache) Clone() *Cache

Clone returns a shallow copy of current object.

func (*Cache) Close

func (c *Cache) Close() error

Close closes the cache if necessary.

func (*Cache) Contains

func (c *Cache) Contains(key interface{}) (bool, error)

Contains returns true if <key> exists in the cache, or else returns false.

func (*Cache) Ctx added in v1.15.2

func (c *Cache) Ctx(ctx context.Context) *Cache

Ctx is a chaining function, which shallowly clones current object and sets the context for next operation.

func (*Cache) Data

func (c *Cache) Data() (map[interface{}]interface{}, error)

Data returns a copy of all key-value pairs in the cache as map type. Note that this function may leads lots of memory usage, you can implement this function if necessary.

func (*Cache) Get

func (c *Cache) Get(key interface{}) (interface{}, error)

Get retrieves and returns the associated value of given <key>. It returns nil if it does not exist, its value is nil or it's expired.

func (*Cache) GetExpire added in v1.13.4

func (c *Cache) GetExpire(key interface{}) (time.Duration, error)

GetExpire retrieves and returns the expiration of <key> in the cache.

It returns 0 if the <key> does not expire. It returns -1 if the <key> does not exist in the cache.

func (*Cache) GetOrSet

func (c *Cache) GetOrSet(key interface{}, value interface{}, duration time.Duration) (interface{}, 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 (*Cache) GetOrSetFunc

func (c *Cache) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, 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 (*Cache) GetOrSetFuncLock

func (c *Cache) GetOrSetFuncLock(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, 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 does nothing if function <f> returns nil.

Note that the function <f> should be executed within writing mutex lock for concurrent safety purpose.

func (*Cache) GetVar added in v1.13.0

func (c *Cache) GetVar(key interface{}) (*gvar.Var, error)

GetVar retrieves and returns the value of <key> as gvar.Var.

func (*Cache) KeyStrings

func (c *Cache) KeyStrings() ([]string, error)

KeyStrings returns all keys in the cache as string slice.

func (*Cache) Keys

func (c *Cache) Keys() ([]interface{}, error)

Keys returns all keys in the cache as slice.

func (*Cache) Remove

func (c *Cache) Remove(keys ...interface{}) (value interface{}, 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 (*Cache) Removes

func (c *Cache) Removes(keys []interface{}) error

Removes deletes <keys> in the cache. Deprecated, use Remove instead.

func (*Cache) Set

func (c *Cache) Set(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 <key> if <duration> < 0.

func (*Cache) SetAdapter added in v1.14.0

func (c *Cache) SetAdapter(adapter Adapter)

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.

func (*Cache) SetIfNotExist

func (c *Cache) SetIfNotExist(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> dose not exist in the cache and it sets <value> successfully to the cache, or else it returns false.

The parameter <value> can be type of <func() interface{}>, but it dose 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 (*Cache) Sets

func (c *Cache) Sets(data map[interface{}]interface{}, duration time.Duration) error

Sets batch sets cache with key-value pairs by <data>, 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 (*Cache) Size

func (c *Cache) Size() (size int, err error)

Size returns the number of items in the cache.

func (*Cache) Update added in v1.13.4

func (c *Cache) Update(key interface{}, value interface{}) (oldValue interface{}, 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 (*Cache) UpdateExpire added in v1.13.4

func (c *Cache) UpdateExpire(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.

func (*Cache) Values

func (c *Cache) Values() ([]interface{}, error)

Values returns all values in the cache as slice.

Jump to

Keyboard shortcuts

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