lru

package
v0.0.0-...-8f072b4 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CACHE_MISS = "CACHE_MISS"
	STORED     = "STORED"
	NOT_STORED = "NOT_STORED"
	REMOVED    = "REMOVED"
	NOT_FOUND  = "NOT_FOUND"
	FLUSHED    = "FLUSH"
	ERR_FLUSH  = "ERR_FLUSH"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type LRUCache

type LRUCache struct {
	// Size represents the maximum number of allowable
	// key-value pairs in the cache.
	Size int32

	// Count records the number of key-value pairs
	// currently in the cache.
	Count int32

	// Full tracks if Count is equal to Size
	Full bool

	// DLL is a doubly linked list containing all key-value pairs
	DLL *List `json:"omitempty"`

	// Hashtable maps to nodes in the doubly linked list
	Hashtable map[string]*Node

	// Mux is a mutex lock
	Mux sync.Mutex
}

LRUCache represents a cache object

func NewLRU

func NewLRU(config config.Configuration) *LRUCache

NewLRU will initialize the cache

func (*LRUCache) Add

Add will add a key/value pair to the cache if the key does not exist already. It will not evict a key/value pair from the cache. If the cache is full, the key/value pair does not get added.

func (*LRUCache) CountKeys

func (cache *LRUCache) CountKeys(args request.CacheRequest) response.CacheResponse

CountKeys return the number of keys in the cache

func (*LRUCache) Delete

func (cache *LRUCache) Delete(args request.CacheRequest) response.CacheResponse

Delete removes a key/value pair from the cache Returns NOT_FOUND if the key does not exist.

func (*LRUCache) DeleteByKey

func (cache *LRUCache) DeleteByKey(key string) response.CacheResponse

DeleteByKey functions the same as Delete, however it is used in various locations to reduce the cost of allocating request objects for internal deletion mechanisms e.g. the cache crawlers.

func (*LRUCache) Flush

func (cache *LRUCache) Flush(args request.CacheRequest) response.CacheResponse

Flush removes all key/value pairs from the cache even if they have not expired

func (*LRUCache) Get

Get will fetch a key/value pair from the cache

func (*LRUCache) GetHashtableReference

func (cache *LRUCache) GetHashtableReference() *map[string]*Node

func (*LRUCache) Put

Put will add a key/value pair to the cache, possibly overwriting an existing key/value pair. Put will evict a key/value pair if the cache is full.

type List

type List struct {
	// Head is the head node. It is a special case node.
	// It does not get populated and is a reference node
	// for accessing the most recently used key-value pair.
	Head *Node `json:"-"`

	// Tail is the tail node. It is a special case node.
	// It does not get populated and is a reference node
	// for accessing the least recently used key-value pair.
	Tail *Node `json:"-"`

	// Size is the size of the list.
	Size int32
	Mux  sync.Mutex
}

func InitList

func InitList() *List

InitList initializes the doubly-linked list.

type Node

type Node struct {
	// Key of the key-value pair
	Key string

	// Value of the key-value pair
	Value interface{}

	// TTL is the time-to-live for the key-value pair
	TTL int64

	// CreatedAt is the time the key-value pair was entered
	// into the cache.
	CreatedAt int64

	// Prev points to the previous node in the doubly
	// linked list. Omit this from snapshot serialization.
	Prev *Node `json:"-"`

	// Next points to the next node in the doubly linked
	// list. Omit this from snapshot serialization.
	Next *Node `json:"-"`

	// Mux is a mutex lock.
	Mux sync.Mutex
}

func GetLastNode

func GetLastNode(ll *List) (*Node, error)

Returns the last node in the list

func Insert

func Insert(ll *List, key string, value interface{}, ttl int64) (*Node, error)

Insert will insert key-value pairs nodes into the doubly linked list.

func RemoveLast

func RemoveLast(ll *List) (*Node, error)

RemoveLast removes the least recently used item in the list.

func RemoveNode

func RemoveNode(ll *List, node *Node) (*Node, error)

RemoveNode removes a specific node from the list.

Jump to

Keyboard shortcuts

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