kv

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a thread-safe, memory KV cache.

Cache supports a hard memory cap and auto memory deallocation under low pressue.

Note that actual memory consumption might be higher than configured cap.

func New

func New(size int) *Cache

New() creates a new Cache instance.

Number of shards = (size + 64K - 1) / 64K.

High lock contention might occur if size is too small. It's advised to use at least NumCPU number of shards.

An acceptable size is 32MB (512 shards).

func (*Cache) Del

func (c *Cache) Del(k []byte)

Del() deletes KV pair from cache with key k.

func (*Cache) Get

func (c *Cache) Get(dst, k []byte) (res []byte)

Get() copies value for key k into dst[:len(value)]. A new slice will be allocated if dst is nil or not long enough. Returns nil if not found.

func (*Cache) Has

func (c *Cache) Has(k []byte) (found bool)

Has() checks whether key k exists in cache.

Has() is slightly cheaper than HasGet() and Get().

func (*Cache) HasGet

func (c *Cache) HasGet(dst, k []byte) (res []byte, found bool)

HasGet() copies value for key k into dst[:len(value)] if pair exists. A new slice will be allocated if dst is nil or not long enough. Returns nil, false if not found.

HasGet() is equal to Get() in performance.

func (*Cache) Iterator

func (c *Cache) Iterator() *Iterator

Iterator() creates a new Iterator instance.

func (*Cache) Reset

func (c *Cache) Reset()

Reset removes all KV pairs from cache

func (*Cache) Set

func (c *Cache) Set(k, v []byte)

Set() sets k, v into the cache. KV pairs with total length exceeding (64KB - 4B) are silently dropped.

k and v are safe to be modified after Set() returns

func (*Cache) Stats

func (c *Cache) Stats() Stats

Stats() acquires global stats of Cache.

type Iterator

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

func (*Iterator) GetNext

func (it *Iterator) GetNext(kDst, vDst []byte) ([]byte, []byte, bool)

GetNext() copies key and value of next KV pair into kDst[:len(key)] and vDst[:len(value)] if next pair exists. New slices will be allocated if is nil or not long enough. Returns nil, nil, false when there are no more pairs.

Pairs acuqired by one Iterator are not guaranteed to be consistent in time. Iteration could pick up pairs inserted after creation of Iterator.

GetNext() does not increment the Gets stat.

type Stats

type Stats struct {
	// Number of Get() calls (including Has() but not Iterator.GetNext()).
	Gets uint64
	// Number of Set() calls.
	Sets          uint64
	Misses        uint64
	Wraps         uint64
	Collisions    uint64
	Allocations   uint64
	Deallocations uint64
	Allocated     uint64
}

Stats represents global stats of Cache.

Directories

Path Synopsis
utils
bench command
LLM usage: the bench utility is generated with deepseek-v4-pro.
LLM usage: the bench utility is generated with deepseek-v4-pro.

Jump to

Keyboard shortcuts

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