cache

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: MIT Imports: 13 Imported by: 0

README

cache

LRU: 最近没使用的换出缓存,go routine安全

接口LRUFacade

    // Get returns a value from the cache, and marks the entry as most recently used.
    Get(key interface{}) (v Value, ok bool)
    // Peek returns a value from the cache without changing the LRU order.
    Peek(key interface{}) (v Value, ok bool)
    // Set sets a value in the cache.
    Set(key interface{}, value Value)
    // Delete removes an entry from the cache, and returns if the entry existed.
    Delete(key interface{}) bool
//创建容量为10000的单个LRU Cache
var s = NewSingleLRUCache(10000)

//创建容量为20000的多路LRU Cache
var m = NeWideLRUCache(20000)

//创建容量为20000的多路LRU Cache
//并通过remap.WithPrime(211)明确指定将Cache分成211组
//需要传入一个素数让分组更均匀
var m = NeWideLRUCache(20000, remap.WithPrime(211))

//创建容量为30000的多路LRU Cache,其中在对key做映射时,
//对key做xxhash运算然后用求的的uint64值来计算它对应多路Cache中具体哪一个。
var x = NeWideLRUCache(20000)

Map: 对Map进行了读写锁的封装,go routine安全

接口MapFacade

//MapFacade an interface to define a Map
type MapFacade interface {
    //Set : set key-value
    Set(key interface{}, value interface{})
    //Get : get value
    Get(key interface{}) (interface{}, bool)
    //Exist : return true if key in map
    Exist(key interface{}) bool
    //Delete : delete a key
    Delete(key interface{})
}
//创建单个Map
var s = NewSingleMap()

//创建容多路Map
var m = NeWideMap()

//创建多路Map
//并通过remap.WithPrime(211)明确指定将Cache分成211组
//需要传入一个素数让分组更均匀
var m = NeWideMap(remap.WithPrime(211))

//创建多路Map,其中在对key做映射时,
//对key做xxhash运算然后用求的的uint64值来计算它对应多路Cache中具体哪一个。
var x = NewWideXHashMap()

特别说明

对LRU和Map进行分组能提高至少一倍的效率,但就算使用单个Map或LRU本身的效率也非常高,可以根据具体的场景来取舍。

LRU的读写测试大概耗时为300ns,分组LRU大概耗时为130ns。
Map的读写测试大概耗时为150ns,分组Map大概耗时为50ns。

Documentation

Overview

Package cache implements a LRU cache.

The implementation borrows heavily from SmallLRUCache (originally by Nathan Schrenk). The object maintains a doubly-linked list of elements. When an element is accessed, it is promoted to the head of the list. When space is needed, the element at the tail of the list (the least recently used element) is evicted.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTTLKeyExists When key exists will return this error.
	ErrTTLKeyExists = status.Error(codes.AlreadyExists, "ttl.key.exists")
	// ErrTTLKeyNotFound When key not found will return this error.
	ErrTTLKeyNotFound = status.Error(codes.NotFound, "ttl.key.not.found")
)

Functions

This section is empty.

Types

type GetOptFn added in v0.8.1

type GetOptFn func(*getOption)

func WithRemoveAfterGet added in v0.8.1

func WithRemoveAfterGet() GetOptFn

func WithUpdateTTL added in v0.8.3

func WithUpdateTTL(ttl int64) GetOptFn

type Item

type Item struct {
	Key   interface{}
	Value Value
}

Item is what is stored in the cache

type LRUCache

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

LRUCache is a typical LRU cache implementation. If the cache reaches the capacity, the least recently used item is deleted from the cache. Note the capacity is not the number of items, but the total sum of the Size() of each item.

func NewLRUCache

func NewLRUCache(capacity int64) *LRUCache

NewLRUCache creates a new empty cache with the given capacity.

func (*LRUCache) Capacity

func (lru *LRUCache) Capacity() int64

Capacity returns the cache maximum capacity.

func (*LRUCache) Clear

func (lru *LRUCache) Clear()

Clear will clear the entire cache.

func (*LRUCache) Delete

func (lru *LRUCache) Delete(key interface{}) bool

Delete removes an entry from the cache, and returns if the entry existed.

func (*LRUCache) Evictions

func (lru *LRUCache) Evictions() int64

Evictions returns the eviction count.

func (*LRUCache) Exist added in v1.0.3

func (lru *LRUCache) Exist(key interface{}) bool

Exist : return true if key in map

func (*LRUCache) Get

func (lru *LRUCache) Get(key interface{}) (v Value, ok bool)

Get returns a value from the cache, and marks the entry as most recently used.

func (*LRUCache) Init added in v0.1.1

func (lru *LRUCache) Init(capacity int64)

Init : init memory

func (*LRUCache) Items

func (lru *LRUCache) Items() []Item

Items returns all the values for the cache, ordered from most recently used to last recently used.

func (*LRUCache) Keys

func (lru *LRUCache) Keys() []interface{}

Keys returns all the keys for the cache, ordered from most recently used to last recently used.

func (*LRUCache) Length

func (lru *LRUCache) Length() int64

Length returns how many elements are in the cache

func (*LRUCache) Peek

func (lru *LRUCache) Peek(key interface{}) (v Value, ok bool)

Peek returns a value from the cache without changing the LRU order.

func (*LRUCache) Set

func (lru *LRUCache) Set(key interface{}, value Value)

Set sets a value in the cache.

func (*LRUCache) SetAndGetRemoved

func (lru *LRUCache) SetAndGetRemoved(key interface{}, value Value) (removedValueList []Value)

SetAndGetRemoved sets a value in the cache and returns the removed value list

func (*LRUCache) SetCapacity

func (lru *LRUCache) SetCapacity(capacity int64)

SetCapacity will set the capacity of the cache. If the capacity is smaller, and the current cache size exceed that capacity, the cache will be shrank.

func (*LRUCache) SetIfAbsent

func (lru *LRUCache) SetIfAbsent(key interface{}, value Value)

SetIfAbsent will set the value in the cache if not present. If the value exists in the cache, we don't set it.

func (*LRUCache) Size

func (lru *LRUCache) Size() int64

Size returns the sum of the objects' Size() method.

func (*LRUCache) Stats

func (lru *LRUCache) Stats() (length, size, capacity, evictions int64)

Stats returns a few stats on the cache.

func (*LRUCache) StatsJSON

func (lru *LRUCache) StatsJSON() string

StatsJSON returns stats as a JSON object in a string.

type LRUFacade added in v0.2.7

type LRUFacade interface {
	// Get returns a value from the cache, and marks the entry as most recently used.
	Get(key interface{}) (v Value, ok bool)
	// Peek returns a value from the cache without changing the LRU order.
	Peek(key interface{}) (v Value, ok bool)
	//Exist : return true if key in map
	Exist(key interface{}) bool
	// Set sets a value in the cache.
	Set(key interface{}, value Value)
	// Delete removes an entry from the cache, and returns if the entry existed.
	Delete(key interface{}) bool
}

LRUFacade an interface to define a LRU cache

func NeWideLRUCache added in v0.2.7

func NeWideLRUCache(capacity int64, opts ...remap.Option) LRUFacade

NeWideLRUCache new wide lru cache

func NewSingleLRUCache added in v0.2.7

func NewSingleLRUCache(capacity int64) LRUFacade

NewSingleLRUCache create a single lru cache

func NewWideXHashLRUCache added in v0.2.7

func NewWideXHashLRUCache(capacity int64, opts ...remap.Option) LRUFacade

NewWideXHashLRUCache new wide lru cache use xxhash as group

type Map

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

Map with locked map

func NewMap

func NewMap() *Map

func (*Map) Delete

func (m *Map) Delete(key interface{})

Delete : delete a key

func (*Map) Exist

func (m *Map) Exist(key interface{}) bool

Exist : return true if key in map

func (*Map) Get

func (m *Map) Get(key interface{}) (interface{}, bool)

Get : get value

func (*Map) Init added in v0.1.1

func (m *Map) Init()

func (*Map) Set

func (m *Map) Set(key interface{}, value interface{})

Set : set key-value

type MapFacade added in v0.2.7

type MapFacade interface {
	//Set : set key-value
	Set(key interface{}, value interface{})
	//Get : get value
	Get(key interface{}) (interface{}, bool)
	//Exist : return true if key in map
	Exist(key interface{}) bool
	//Delete : delete a key
	Delete(key interface{})
}

MapFacade an interface to define a Map

func NewSingleMap added in v0.2.7

func NewSingleMap() MapFacade

NewSingleMap create a single map

func NewWideMap added in v0.2.8

func NewWideMap(opts ...remap.Option) MapFacade

NewWideMap new wide Map

func NewWideXHashMap added in v0.2.7

func NewWideXHashMap(opts ...remap.Option) MapFacade

NewWideXHashMap new wide Map use xxhash as group

type SetOptFn added in v0.8.1

type SetOptFn func(*setOption)

func WithKeepTTL added in v0.8.3

func WithKeepTTL() SetOptFn

func WithMustNotExist added in v0.8.1

func WithMustNotExist() SetOptFn

func WithTTL added in v0.8.1

func WithTTL(ttl int64) SetOptFn

type TTLCache added in v0.8.0

type TTLCache interface {
	// Set key value into ttl cache.
	Set(ctx context.Context, key string, value []byte, fns ...SetOptFn) error
	// Get value by key from ttl cache.
	Get(ctx context.Context, key string, fns ...GetOptFn) ([]byte, error)
	// Remove value by key.
	Remove(ctx context.Context, key string) error
	// Clear cache.
	Clear(ctx context.Context)
}

func NewTTLMemCache added in v0.8.2

func NewTTLMemCache(size int, ttl int64) TTLCache

NewTTLMemCache New ttl cache

func NewTTLRdsCache added in v0.8.2

func NewTTLRdsCache(cmd redis.Cmdable, prefix string, ttl int64) TTLCache

type Value

type Value interface {
	// Size returns how big this value is. If you want to just track
	// the cache by number of objects, you may return the size as 1.
	Size() int
}

Value is the interface values that go into LRUCache need to satisfy

type WideLRUCache added in v0.2.7

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

WideLRUCache use LruCache group array as a wide lru cache

func (*WideLRUCache) Delete added in v0.2.7

func (w *WideLRUCache) Delete(key interface{}) bool

Delete removes an entry from the cache, and returns if the entry existed.

func (*WideLRUCache) Exist added in v1.0.3

func (w *WideLRUCache) Exist(key interface{}) bool

Exist : return true if key in map

func (*WideLRUCache) Get added in v0.2.7

func (w *WideLRUCache) Get(key interface{}) (v Value, ok bool)

Get returns a value from the cache, and marks the entry as most recently used.

func (*WideLRUCache) Peek added in v0.2.7

func (w *WideLRUCache) Peek(key interface{}) (v Value, ok bool)

Peek returns a value from the cache without changing the LRU order.

func (*WideLRUCache) Set added in v0.2.7

func (w *WideLRUCache) Set(key interface{}, value Value)

Set sets a value in the cache.

type WideMap added in v0.2.7

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

WideMap use Map group array as a wide map

func (*WideMap) Delete added in v0.2.7

func (w *WideMap) Delete(key interface{})

Delete : delete a key

func (*WideMap) Exist added in v0.2.7

func (w *WideMap) Exist(key interface{}) bool

Exist : return true if key in map

func (*WideMap) Get added in v0.2.7

func (w *WideMap) Get(key interface{}) (interface{}, bool)

Get : get value

func (*WideMap) Set added in v0.2.7

func (w *WideMap) Set(key interface{}, value interface{})

Set : set key-value

Jump to

Keyboard shortcuts

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