cachehash

package
v0.0.0-...-7404e18 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheHash

type CacheHash struct {
	sync.Mutex
	// contains filtered or unexported fields
}

CacheHash is an LRU cache implemented with a hash map and a doubly linked list. The list stores key-value pairs in the order they were accessed, with the most recently accessed key-value pair at the front of the list. This allows for O(1) insertions, deletions, and lookups and ensures the most recently accesssed elements are persisted in the cache.

func (*CacheHash) Delete

func (c *CacheHash) Delete(k interface{}) (v interface{}, found bool)

Delete removes the key-value pair from the cache and returns the value and whether the key was found. v is nil if the key was not found.

func (*CacheHash) Eject

func (c *CacheHash) Eject()

Eject removes the least-recently used key-value pair from the cache.

func (*CacheHash) First

func (c *CacheHash) First() (k interface{}, v interface{})

First returns the key-value pair at the front of the list. Returns nil, nil if the cache is empty.

func (*CacheHash) Get

func (c *CacheHash) Get(k interface{}) (v interface{}, found bool)

Get returns the value associated with the key and whether the key was found in the cache. It also moves it to the front of the list. v is nil if the key was not found.

func (*CacheHash) GetNoMove

func (c *CacheHash) GetNoMove(k interface{}) (v interface{}, found bool)

GetNoMove returns the value associated with the key and whether the key was found in the cache.

func (*CacheHash) Has

func (c *CacheHash) Has(k interface{}) bool

Has returns whether the key is in the cache.

func (*CacheHash) Init

func (c *CacheHash) Init(maxLen int)

Init initializes the cache with a maximum length.

func (*CacheHash) Last

func (c *CacheHash) Last() (k interface{}, v interface{})

Last returns the key-value pair at the back of the list. Returns nil, nil if the cache is empty.

func (*CacheHash) Len

func (c *CacheHash) Len() int

Len returns the number of key-value pairs in the cache.

func (*CacheHash) RegisterCB

func (c *CacheHash) RegisterCB(newCB func(interface{}, interface{}))

RegisterCB registers a callback function to be called when an element is ejected from the cache.

func (*CacheHash) Upsert

func (c *CacheHash) Upsert(k interface{}, v interface{}) (didExist, didEject bool)

Upsert upserts a new key-value pair into the cache. If the key already exists, the value is updated and the key is moved to the front of the list. If the key does not exist in the cache, the key-value pair is added to the front of the list. Returns whether the key already existed in the cache and if the cache had to eject an entry to insert this one.

type ShardedCacheHash

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

func (*ShardedCacheHash) Add

func (c *ShardedCacheHash) Add(k interface{}, v interface{}) (didExist, didEject bool)

func (*ShardedCacheHash) Delete

func (c *ShardedCacheHash) Delete(k interface{}) (interface{}, bool)

func (*ShardedCacheHash) Get

func (c *ShardedCacheHash) Get(k interface{}) (interface{}, bool)

func (*ShardedCacheHash) GetNoMove

func (c *ShardedCacheHash) GetNoMove(k interface{}) (interface{}, bool)

func (*ShardedCacheHash) Has

func (c *ShardedCacheHash) Has(k interface{}) bool

func (*ShardedCacheHash) Init

func (c *ShardedCacheHash) Init(maxLen int, shards int)

func (*ShardedCacheHash) Lock

func (c *ShardedCacheHash) Lock(k interface{})

func (*ShardedCacheHash) RegisterCB

func (c *ShardedCacheHash) RegisterCB(newCB func(interface{}, interface{}))

func (*ShardedCacheHash) Unlock

func (c *ShardedCacheHash) Unlock(k interface{})

Jump to

Keyboard shortcuts

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