cache

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheSizeZero           = fmt.Errorf("Cache: Size <= 0")
	ErrCacheRegisterAdapterNil = fmt.Errorf("Cache: Register adapter is nil")
	ErrCacheCanNotFindAdapter  = fmt.Errorf("Cache: Can not find adapter: ")
	ErrCacheUnknownAdapter     = fmt.Errorf("Cache: unknown adapter: ")
	ErrCacheKeyNotFind         = fmt.Errorf("Cache: key not find")
)

Functions

func HasRegister

func HasRegister(name MODE) bool

func Register

func Register(name MODE, adapter Instance)

Types

type ARCPlugin added in v1.0.2

type ARCPlugin struct {
	Options
	// contains filtered or unexported fields
}

func (*ARCPlugin) Get added in v1.0.2

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

Get a value from cache pool using key if it exists. If not exists and it has LoaderFunc, it will generate the value using you have specified LoaderFunc method returns value.

func (*ARCPlugin) GetALL added in v1.0.2

func (c *ARCPlugin) GetALL() map[interface{}]interface{}

Returns all key-value pairs in the cache.

func (*ARCPlugin) GetIFPresent added in v1.0.2

func (c *ARCPlugin) GetIFPresent(key interface{}) (interface{}, error)

Get a value from cache pool using key if it exists. If it dose not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.

func (*ARCPlugin) HasKey added in v1.0.2

func (c *ARCPlugin) HasKey(key interface{}) bool

func (*ARCPlugin) Keys added in v1.0.2

func (c *ARCPlugin) Keys() []interface{}

Keys returns a slice of the keys in the cache.

func (*ARCPlugin) Len added in v1.0.2

func (c *ARCPlugin) Len() int

Len returns the number of items in the cache.

func (*ARCPlugin) Purge added in v1.0.2

func (c *ARCPlugin) Purge()

Purge is used to completely clear the cache

func (*ARCPlugin) Remove added in v1.0.2

func (c *ARCPlugin) Remove(key interface{}) bool

Remove removes the provided key from the cache.

func (*ARCPlugin) Set added in v1.0.2

func (c *ARCPlugin) Set(key, value interface{})

type AddedFunc

type AddedFunc func(interface{}, interface{})

type Cache

type Cache interface {
	Set(interface{}, interface{})                  // set数据
	Get(interface{}) (interface{}, error)          // get数据
	GetIFPresent(interface{}) (interface{}, error) // 获取数据,如果数据不存在则通过cacheLoader获取数据,缓存并返回
	GetALL() map[interface{}]interface{}           // TODO:获得全量数据,业务慎用

	Remove(interface{}) bool // 删除key
	Purge()                  // 清除 plguin
	Keys() []interface{}     // 获得全部key
	Len() int                // 获得cache大小
	HasKey(interface{}) bool // 判断key是否存在
	// contains filtered or unexported methods
}

func NewARCPlugin added in v1.0.2

func NewARCPlugin(cb *Setting) Cache

NewARCPlugin returns a new plugin.

func NewFIFOPlugin added in v1.1.1

func NewFIFOPlugin(cb *Setting) Cache

NewFIFOPlugin returns a new plugin.

func NewLFUPlugin

func NewLFUPlugin(cb *Setting) Cache

NewLFUPlugin returns a new plugin.

func NewLRUPlugin

func NewLRUPlugin(cb *Setting) Cache

NewLRUPlugin returns a new plugin.

func NewSimplePlugin added in v1.0.2

func NewSimplePlugin(cb *Setting) Cache

func PluginInstance

func PluginInstance(cb *Setting) (adapter Cache)

type EvictedFunc

type EvictedFunc func(interface{}, interface{})

type FIFOPlugin added in v1.1.1

type FIFOPlugin struct {
	Options
	// contains filtered or unexported fields
}

func (*FIFOPlugin) Get added in v1.1.1

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

Get a value from cache pool using key if it exists. If it dose not exists key and has LoaderFunc, generate a value using `LoaderFunc` method returns value.

func (*FIFOPlugin) GetALL added in v1.1.1

func (c *FIFOPlugin) GetALL() map[interface{}]interface{}

Returns all key-value pairs in the cache.

func (*FIFOPlugin) GetIFPresent added in v1.1.1

func (c *FIFOPlugin) GetIFPresent(key interface{}) (interface{}, error)

func (*FIFOPlugin) HasKey added in v1.1.1

func (c *FIFOPlugin) HasKey(key interface{}) bool

func (*FIFOPlugin) Keys added in v1.1.1

func (c *FIFOPlugin) Keys() []interface{}

Returns a slice of the keys in the cache.

func (*FIFOPlugin) Len added in v1.1.1

func (c *FIFOPlugin) Len() int

Returns the number of items in the cache.

func (*FIFOPlugin) Purge added in v1.1.1

func (c *FIFOPlugin) Purge()

Completely clear the cache

func (*FIFOPlugin) Remove added in v1.1.1

func (c *FIFOPlugin) Remove(key interface{}) bool

Removes the provided key from the cache.

func (*FIFOPlugin) Set added in v1.1.1

func (c *FIFOPlugin) Set(key, value interface{})

set a new key-value pair

type Group

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

func (*Group) Do

func (g *Group) Do(key interface{}, fn func() (interface{}, error), isWait bool) (interface{}, bool, error)

type Instance

type Instance func(*Setting) Cache

type LFUPlugin

type LFUPlugin struct {
	Options
	// contains filtered or unexported fields
}

Discards the least frequently used items first.

func (*LFUPlugin) Get

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

Get a value from cache pool using key if it exists. If it dose not exists key and has LoaderFunc, generate a value using `LoaderFunc` method returns value.

func (*LFUPlugin) GetALL

func (c *LFUPlugin) GetALL() map[interface{}]interface{}

Returns all key-value pairs in the cache.

func (*LFUPlugin) GetIFPresent

func (c *LFUPlugin) GetIFPresent(key interface{}) (interface{}, error)

Get a value from cache pool using key if it exists. If it dose not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.

func (*LFUPlugin) HasKey

func (c *LFUPlugin) HasKey(key interface{}) bool

func (*LFUPlugin) Keys

func (c *LFUPlugin) Keys() []interface{}

Returns a slice of the keys in the cache.

func (*LFUPlugin) Len

func (c *LFUPlugin) Len() int

Returns the number of items in the cache.

func (*LFUPlugin) Purge

func (c *LFUPlugin) Purge()

Completely clear the cache

func (*LFUPlugin) Remove

func (c *LFUPlugin) Remove(key interface{}) bool

Removes the provided key from the cache.

func (*LFUPlugin) Set

func (c *LFUPlugin) Set(key, value interface{})

set a new key-value pair

type LRUPlugin

type LRUPlugin struct {
	Options
	// contains filtered or unexported fields
}

func (*LRUPlugin) Get

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

Get a value from cache pool using key if it exists. If it dose not exists key and has LoaderFunc, generate a value using `LoaderFunc` method returns value.

func (*LRUPlugin) GetALL

func (c *LRUPlugin) GetALL() map[interface{}]interface{}

Returns all key-value pairs in the cache.

func (*LRUPlugin) GetIFPresent

func (c *LRUPlugin) GetIFPresent(key interface{}) (interface{}, error)

func (*LRUPlugin) HasKey

func (c *LRUPlugin) HasKey(key interface{}) bool

func (*LRUPlugin) Keys

func (c *LRUPlugin) Keys() []interface{}

Returns a slice of the keys in the cache.

func (*LRUPlugin) Len

func (c *LRUPlugin) Len() int

Returns the number of items in the cache.

func (*LRUPlugin) Purge

func (c *LRUPlugin) Purge()

Completely clear the cache

func (*LRUPlugin) Remove

func (c *LRUPlugin) Remove(key interface{}) bool

Removes the provided key from the cache.

func (*LRUPlugin) Set

func (c *LRUPlugin) Set(key, value interface{})

set a new key-value pair

type LoaderFunc

type LoaderFunc func(interface{}) (interface{}, error)

type MODE

type MODE string
const (
	LRU    MODE = "lru"    // Least Recently Used mode  最近最少使用
	LFU    MODE = "lfu"    // Least Frequently Used mode 最小频繁使用模式
	SIMPLE MODE = "simple" // Simple mode: Random 随机
	ARC    MODE = "arc"    // Adjustable Replacement Cache mode 可调换缓存模式
	FIFO   MODE = "fifo"   // First In, First Out 先入先出模式
)

type Options

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

type Setting

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

func New

func New(size int) *Setting

func (*Setting) ARC added in v1.0.2

func (cb *Setting) ARC() *Setting

func (*Setting) AddedFunc

func (cb *Setting) AddedFunc(addedFunc AddedFunc) *Setting

func (*Setting) EvictType

func (cb *Setting) EvictType(tp MODE) *Setting

func (*Setting) EvictedFunc

func (cb *Setting) EvictedFunc(evictedFunc EvictedFunc) *Setting

func (*Setting) Expiration

func (cb *Setting) Expiration(expiration time.Duration) *Setting

func (*Setting) FIFO added in v1.1.1

func (cb *Setting) FIFO() *Setting

func (*Setting) LFU

func (cb *Setting) LFU() *Setting

func (*Setting) LRU

func (cb *Setting) LRU() *Setting

func (*Setting) LoaderFunc

func (cb *Setting) LoaderFunc(loaderFunc LoaderFunc) *Setting

func (*Setting) Setting

func (cb *Setting) Setting() Cache

func (*Setting) Simple added in v1.0.2

func (cb *Setting) Simple() *Setting

type SimplePlugin added in v1.0.2

type SimplePlugin struct {
	Options
	// contains filtered or unexported fields
}

SimplePlugin has no clear priority for evict cache. It depends on key-value map order.

func (*SimplePlugin) Get added in v1.0.2

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

Get a value from cache pool using key if it exists. If it dose not exists key and has LoaderFunc, generate a value using `LoaderFunc` method returns value.

func (*SimplePlugin) GetALL added in v1.0.2

func (c *SimplePlugin) GetALL() map[interface{}]interface{}

Returns all key-value pairs in the cache.

func (*SimplePlugin) GetIFPresent added in v1.0.2

func (c *SimplePlugin) GetIFPresent(key interface{}) (interface{}, error)

Get a value from cache pool using key if it exists. If it dose not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.

func (*SimplePlugin) HasKey added in v1.0.2

func (c *SimplePlugin) HasKey(key interface{}) bool

func (*SimplePlugin) Keys added in v1.0.2

func (c *SimplePlugin) Keys() []interface{}

Returns a slice of the keys in the cache.

func (*SimplePlugin) Len added in v1.0.2

func (c *SimplePlugin) Len() int

Returns the number of items in the cache.

func (*SimplePlugin) Purge added in v1.0.2

func (c *SimplePlugin) Purge()

Completely clear the cache

func (*SimplePlugin) Remove added in v1.0.2

func (c *SimplePlugin) Remove(key interface{}) bool

Removes the provided key from the cache.

func (*SimplePlugin) Set added in v1.0.2

func (c *SimplePlugin) Set(key, value interface{})

set a new key-value pair

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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