gcache

package
v2.0.0-...-cbac34e Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2019 License: MIT Imports: 12 Imported by: 0

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 interface{}) 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>.

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

func GetOrSetFunc

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

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

func GetOrSetFuncLock

func GetOrSetFuncLock(key interface{}, f func() interface{}, duration interface{}) 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>.

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

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

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(key interface{}) interface{}

Remove deletes the <key> in the cache, and returns its value.

func Removes

func Removes(keys []interface{})

Removes deletes <keys> in the cache.

func Set

func Set(key interface{}, value interface{}, duration interface{})

Set sets cache with <key>-<value> pair, which is expired after <duration>.

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

func SetIfNotExist

func SetIfNotExist(key interface{}, value interface{}, duration interface{}) bool

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

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

func Sets

func Sets(data map[interface{}]interface{}, duration interface{})

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

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

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 interface{}) 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>.

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

func (Cache) GetOrSetFunc

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

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

func (Cache) GetOrSetFuncLock

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

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

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

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(key interface{}) (value interface{})

Remove deletes the <key> in the cache, and returns its value.

func (Cache) Removes

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

Removes deletes <keys> in the cache.

func (Cache) Set

func (c Cache) Set(key interface{}, value interface{}, duration interface{})

Set sets cache with <key>-<value> pair, which is expired after <duration>.

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

func (Cache) SetIfNotExist

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

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

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

func (Cache) Sets

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

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

The parameter <duration> can be either type of int or time.Duration. If <duration> is type of int, it means <duration> milliseconds. If <duration> <=0 means it does not expire.

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