cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultCacheType set default cache type for NewDefaultCache function
	DefaultCacheType = LRUCache
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Put puts an item into cache.
	Put(key uint64, value interface{})
	// Get retrieves an item from cache.
	Get(key uint64) (interface{}, bool)
	// Peek reads an item from cache. The action is no considered 'Use'.
	Peek(key uint64) (interface{}, bool)
	// Remove eliminates an item from cache.
	Remove(key uint64)
	// Elems return all items in cache.
	Elems() []*Item
	// Len returns current cache size
	Len() int
}

Cache is an interface for cache system

func NewCache

func NewCache(size int, cacheType Type) Cache

NewCache create Cache instance by CacheType

func NewDefaultCache

func NewDefaultCache(size int) Cache

NewDefaultCache create Cache instance by default cache type

type Entry

type Entry struct {
	Priority int
	Value    PriorityQueueItem
}

Entry a pair of region and it's priority

func (*Entry) Less

func (r *Entry) Less(other *Entry) bool

Less return true if the entry has smaller priority

type FIFO

type FIFO struct {
	syncutil.RWMutex
	// contains filtered or unexported fields
}

FIFO is 'First-In-First-Out' cache.

func NewFIFO

func NewFIFO(maxCount int) *FIFO

NewFIFO returns a new FIFO cache.

func (*FIFO) Elems

func (c *FIFO) Elems() []*Item

Elems returns all items in cache.

func (*FIFO) FromElems

func (c *FIFO) FromElems(key uint64) []*Item

FromElems returns all items that has a key greater than the specified one.

func (*FIFO) FromLastSameElems

func (c *FIFO) FromLastSameElems(checkFunc func(interface{}) (bool, string)) []*Item

FromLastSameElems returns continuous items that have the same comparable attribute with the the lastest one.

func (*FIFO) Len

func (c *FIFO) Len() int

Len returns current cache size.

func (*FIFO) Put

func (c *FIFO) Put(key uint64, value interface{})

Put puts an item into cache.

func (*FIFO) Remove

func (c *FIFO) Remove()

Remove takes the oldest item out.

type Item

type Item struct {
	Key   uint64
	Value interface{}
}

Item is the cache entry.

type LRU

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

LRU is 'Least-Recently-Used' cache.

func (*LRU) Elems

func (c *LRU) Elems() []*Item

Elems return all items in cache.

func (*LRU) Get

func (c *LRU) Get(key uint64) (interface{}, bool)

Get retrieves an item from cache.

func (*LRU) Len

func (c *LRU) Len() int

Len returns current cache size.

func (*LRU) Peek

func (c *LRU) Peek(key uint64) (interface{}, bool)

Peek reads an item from cache. The action is no considered 'Use'.

func (*LRU) Put

func (c *LRU) Put(key uint64, value interface{})

Put puts an item into cache.

func (*LRU) Remove

func (c *LRU) Remove(key uint64)

Remove eliminates an item from cache.

type PriorityQueue

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

PriorityQueue queue has priority and preempt

func NewPriorityQueue

func NewPriorityQueue(capacity int) *PriorityQueue

NewPriorityQueue construct of priority queue

func (*PriorityQueue) Elems

func (pq *PriorityQueue) Elems() []*Entry

Elems return all elements in queue

func (*PriorityQueue) Get

func (pq *PriorityQueue) Get(id uint64) *Entry

Get find entry by id from queue

func (*PriorityQueue) Len

func (pq *PriorityQueue) Len() int

Len return queue size

func (*PriorityQueue) Peek

func (pq *PriorityQueue) Peek() *Entry

Peek return the highest priority entry

func (*PriorityQueue) Put

func (pq *PriorityQueue) Put(priority int, value PriorityQueueItem) bool

Put put value with priority into queue

func (*PriorityQueue) Remove

func (pq *PriorityQueue) Remove(id uint64)

Remove remove value from queue

func (*PriorityQueue) Tail

func (pq *PriorityQueue) Tail() *Entry

Tail return the lowest priority entry

type PriorityQueueItem

type PriorityQueueItem interface {
	ID() uint64
}

PriorityQueueItem avoid convert cost

type TTLString

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

TTLString is simple TTL saves key string and value.

func NewStringTTL

func NewStringTTL(ctx context.Context, gcInterval, ttl time.Duration) *TTLString

NewStringTTL creates a new TTLString cache.

func (TTLString) Clear

func (c TTLString) Clear()

Clear removes all items in the ttl cache.

func (*TTLString) Get

func (c *TTLString) Get(id string) (interface{}, bool)

Get return the value by key id

func (*TTLString) GetAllID

func (c *TTLString) GetAllID() []string

GetAllID returns all key ids

func (TTLString) Len

func (c TTLString) Len() int

Len returns current cache size.

func (*TTLString) Pop

func (c *TTLString) Pop() (string, interface{}, bool)

Pop one key/value that is not expired

func (*TTLString) Put

func (c *TTLString) Put(key string, value interface{})

Put put the string key with the value

func (*TTLString) PutWithTTL

func (c *TTLString) PutWithTTL(key string, value interface{}, ttl time.Duration)

PutWithTTL puts an item into cache with specified TTL.

func (TTLString) UpdateTTL

func (c TTLString) UpdateTTL(duration time.Duration)

UpdateTTL updates the TTL for the cache.

type TTLUint64

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

TTLUint64 is simple TTL saves only uint64s.

func NewIDTTL

func NewIDTTL(ctx context.Context, gcInterval, ttl time.Duration) *TTLUint64

NewIDTTL creates a new TTLUint64 cache.

func (TTLUint64) Clear

func (c TTLUint64) Clear()

Clear removes all items in the ttl cache.

func (*TTLUint64) Exists

func (c *TTLUint64) Exists(id uint64) bool

Exists checks if an ID exists in cache.

func (*TTLUint64) Get

func (c *TTLUint64) Get(id uint64) (interface{}, bool)

Get return the value by key id

func (*TTLUint64) GetAllID

func (c *TTLUint64) GetAllID() []uint64

GetAllID returns all ids.

func (TTLUint64) Len

func (c TTLUint64) Len() int

Len returns current cache size.

func (*TTLUint64) Put

func (c *TTLUint64) Put(id uint64, value interface{})

Put saves an ID in cache.

func (*TTLUint64) PutWithTTL

func (c *TTLUint64) PutWithTTL(key uint64, value interface{}, ttl time.Duration)

PutWithTTL puts an item into cache with specified TTL.

func (*TTLUint64) Remove

func (c *TTLUint64) Remove(key uint64)

Remove remove key

func (TTLUint64) UpdateTTL

func (c TTLUint64) UpdateTTL(duration time.Duration)

UpdateTTL updates the TTL for the cache.

type TwoQueue

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

TwoQueue is a fixed size 2Q cache. 2Q is an enhancement over the standard LRU cache in that it tracks both frequently and recently used entries separately. This avoids a burst in access to new entries from evicting frequently used entries. It adds some additional tracking overhead to the standard LRU cache, and is computationally about 2x the cost, and adds some metadata over head. The ARCCache is similar, but does not require setting any parameters. TwoQueue implementation is based on https://github.com/hashicorp/golang-lru/blob/master/2q.go

func (*TwoQueue) Elems

func (c *TwoQueue) Elems() []*Item

Elems return all items in cache.

func (*TwoQueue) Get

func (c *TwoQueue) Get(key uint64) (interface{}, bool)

Get retrieves an item from cache.

func (*TwoQueue) Len

func (c *TwoQueue) Len() int

Len returns current cache size.

func (*TwoQueue) Peek

func (c *TwoQueue) Peek(key uint64) (interface{}, bool)

Peek reads an item from cache. The action is no considered 'Use'.

func (*TwoQueue) Put

func (c *TwoQueue) Put(key uint64, value interface{})

Put puts an item into cache.

func (*TwoQueue) Remove

func (c *TwoQueue) Remove(key uint64)

Remove eliminates an item from cache.

type Type

type Type int

Type is cache's type such as LRUCache and etc.

const (
	// LRUCache is for LRU cache
	LRUCache Type = 1
	// TwoQueueCache is for 2Q cache
	TwoQueueCache Type = 2
)

Jump to

Keyboard shortcuts

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