gcache

package
v1.13.3 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2020 License: MIT Imports: 13 Imported by: 70

Documentation

Overview

Package gcache provides high performance and concurrent-safe in-memory cache for process.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(key interface{}) bool

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

func Data

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

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

func Get

func Get(key interface{}) interface{}

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

func GetOrSet

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

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{}, duration time.Duration) interface{}

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{}, duration time.Duration) interface{}

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

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

func KeyStrings

func KeyStrings() []string

KeyStrings returns all keys in the cache as string slice.

func Keys

func Keys() []interface{}

Keys returns all keys in the cache as slice.

func Remove

func Remove(keys ...interface{}) (value interface{})

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

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)

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

Size returns the size of the cache.

func Values

func Values() []interface{}

Values returns all values in the cache as slice.

Types

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.

func (*Cache) Clear

func (c *Cache) Clear()

Clear clears all data of the cache.

func (Cache) Close

func (c Cache) Close()

Close closes the cache.

func (Cache) Contains

func (c Cache) Contains(key interface{}) bool

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

func (Cache) Data

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

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

func (Cache) Get

func (c Cache) Get(key interface{}) interface{}

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

func (Cache) GetOrSet

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

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 (Cache) GetOrSetFunc

func (c Cache) GetOrSetFunc(key interface{}, f func() interface{}, duration time.Duration) interface{}

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. It does nothing if function <f> returns nil.

func (Cache) GetOrSetFuncLock

func (c Cache) GetOrSetFuncLock(key interface{}, f func() interface{}, duration time.Duration) interface{}

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. It does nothing if function <f> returns nil.

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

func (Cache) GetVar added in v1.13.0

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

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

func (Cache) KeyStrings

func (c Cache) KeyStrings() []string

KeyStrings returns all keys in the cache as string slice.

func (Cache) Keys

func (c Cache) Keys() []interface{}

Keys returns all keys in the cache as slice.

func (Cache) Remove

func (c Cache) Remove(keys ...interface{}) (value interface{})

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 (Cache) Removes

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

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

func (Cache) Set

func (c Cache) 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 (Cache) SetIfNotExist

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

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 (Cache) Sets

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

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

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

func (Cache) Size

func (c Cache) Size() (size int)

Size returns the size of the cache.

func (Cache) Values

func (c Cache) Values() []interface{}

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