cache

package
v0.20.2 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: Apache-2.0 Imports: 3 Imported by: 5

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

This section is empty.

Functions

This section is empty.

Types

type Item

type Item[T any] struct {
	Key   string
	Value T
}

Item is what is stored in the cache

type LRUCache

type LRUCache[T any] 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.

func NewLRUCache

func NewLRUCache[T any](capacity int64) *LRUCache[T]

NewLRUCache creates a new empty cache with the given capacity.

func (*LRUCache[T]) Delete

func (lru *LRUCache[T]) Delete(key string)

Delete removes an entry from the cache

func (*LRUCache[T]) Evictions

func (lru *LRUCache[T]) Evictions() int64

Evictions returns the number of evictions

func (*LRUCache[T]) Get

func (lru *LRUCache[T]) Get(key string) (v T, ok bool)

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

func (*LRUCache[T]) Hits added in v0.13.0

func (lru *LRUCache[T]) Hits() int64

Hits returns number of cache hits since creation

func (*LRUCache[T]) Items

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

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

func (*LRUCache[T]) Len added in v0.10.0

func (lru *LRUCache[T]) Len() int

Len returns the size of the cache (in entries)

func (*LRUCache[T]) MaxCapacity added in v0.10.0

func (lru *LRUCache[T]) MaxCapacity() int64

MaxCapacity returns the cache maximum capacity.

func (*LRUCache[T]) Misses added in v0.13.0

func (lru *LRUCache[T]) Misses() int64

Misses returns number of cache misses since creation

func (*LRUCache[T]) Set

func (lru *LRUCache[T]) Set(key string, value T) bool

Set sets a value in the cache.

func (*LRUCache[T]) SetCapacity

func (lru *LRUCache[T]) 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[T]) UsedCapacity added in v0.10.0

func (lru *LRUCache[T]) UsedCapacity() int64

UsedCapacity returns the size of the cache (in bytes)

Directories

Path Synopsis
bf

Jump to

Keyboard shortcuts

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