cache

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package cache provides caching implementations for RockyardKV.

This package includes an LRU (Least Recently Used) block cache that is used to cache SST file data blocks and index blocks, reducing disk I/O and improving read performance.

Reference: RocksDB v10.7.5

  • cache/lru_cache.h
  • cache/lru_cache.cc

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Insert adds a block to the cache. If the key already exists, it updates the value.
	// Returns the handle to the cached block.
	Insert(key CacheKey, value []byte, charge uint64) *Handle

	// Lookup retrieves a block from the cache.
	// Returns nil if not found.
	Lookup(key CacheKey) *Handle

	// Release releases a handle obtained from Insert or Lookup.
	// The caller must call Release when done using the handle.
	Release(handle *Handle)

	// Erase removes a key from the cache.
	Erase(key CacheKey)

	// SetCapacity sets the maximum capacity of the cache.
	SetCapacity(capacity uint64)

	// GetCapacity returns the maximum capacity of the cache.
	GetCapacity() uint64

	// GetUsage returns the current usage of the cache.
	GetUsage() uint64

	// GetPinnedUsage returns the usage of currently pinned entries.
	GetPinnedUsage() uint64

	// GetOccupancyCount returns the number of entries in the cache.
	GetOccupancyCount() uint64

	// Close releases all resources associated with the cache.
	Close()
}

Cache is the interface for all cache implementations.

type CacheKey

type CacheKey struct {
	FileNumber  uint64
	BlockOffset uint64
}

CacheKey uniquely identifies a cached block.

type Handle

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

Handle represents a reference to a cached block.

func (*Handle) Charge

func (h *Handle) Charge() uint64

Charge returns the memory charge of this entry.

func (*Handle) Value

func (h *Handle) Value() []byte

Value returns the cached block data.

type LRUCache

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

LRUCache is a thread-safe LRU cache with a fixed capacity.

func NewLRUCache

func NewLRUCache(capacity uint64) *LRUCache

NewLRUCache creates a new LRU cache with the given capacity in bytes.

func (*LRUCache) Close

func (c *LRUCache) Close()

Close releases all resources.

func (*LRUCache) Erase

func (c *LRUCache) Erase(key CacheKey)

Erase removes a key from the cache.

func (*LRUCache) GetCapacity

func (c *LRUCache) GetCapacity() uint64

GetCapacity returns the maximum capacity.

func (*LRUCache) GetHitCount

func (c *LRUCache) GetHitCount() uint64

GetHitCount returns the number of cache hits.

func (*LRUCache) GetHitRate

func (c *LRUCache) GetHitRate() float64

GetHitRate returns the cache hit rate (0.0 to 1.0).

func (*LRUCache) GetMissCount

func (c *LRUCache) GetMissCount() uint64

GetMissCount returns the number of cache misses.

func (*LRUCache) GetOccupancyCount

func (c *LRUCache) GetOccupancyCount() uint64

GetOccupancyCount returns the number of entries.

func (*LRUCache) GetPinnedUsage

func (c *LRUCache) GetPinnedUsage() uint64

GetPinnedUsage returns the usage of currently pinned entries.

func (*LRUCache) GetUsage

func (c *LRUCache) GetUsage() uint64

GetUsage returns the current usage.

func (*LRUCache) Insert

func (c *LRUCache) Insert(key CacheKey, value []byte, charge uint64) *Handle

Insert adds a block to the cache.

func (*LRUCache) Lookup

func (c *LRUCache) Lookup(key CacheKey) *Handle

Lookup retrieves a block from the cache.

func (*LRUCache) Release

func (c *LRUCache) Release(handle *Handle)

Release releases a handle.

func (*LRUCache) SetCapacity

func (c *LRUCache) SetCapacity(capacity uint64)

SetCapacity sets the maximum capacity.

type ShardedLRUCache

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

ShardedLRUCache is an LRU cache with multiple shards for reduced lock contention.

func NewShardedLRUCache

func NewShardedLRUCache(capacity uint64, numShards int) *ShardedLRUCache

NewShardedLRUCache creates a new sharded LRU cache. numShards should be a power of 2 for best performance.

func (*ShardedLRUCache) Close

func (c *ShardedLRUCache) Close()

Close releases all resources.

func (*ShardedLRUCache) Erase

func (c *ShardedLRUCache) Erase(key CacheKey)

Erase removes a key from the cache.

func (*ShardedLRUCache) GetCapacity

func (c *ShardedLRUCache) GetCapacity() uint64

GetCapacity returns the maximum capacity.

func (*ShardedLRUCache) GetHitCount

func (c *ShardedLRUCache) GetHitCount() uint64

GetHitCount returns the total number of cache hits.

func (*ShardedLRUCache) GetHitRate

func (c *ShardedLRUCache) GetHitRate() float64

GetHitRate returns the overall cache hit rate.

func (*ShardedLRUCache) GetMissCount

func (c *ShardedLRUCache) GetMissCount() uint64

GetMissCount returns the total number of cache misses.

func (*ShardedLRUCache) GetOccupancyCount

func (c *ShardedLRUCache) GetOccupancyCount() uint64

GetOccupancyCount returns the number of entries.

func (*ShardedLRUCache) GetPinnedUsage

func (c *ShardedLRUCache) GetPinnedUsage() uint64

GetPinnedUsage returns the usage of currently pinned entries.

func (*ShardedLRUCache) GetUsage

func (c *ShardedLRUCache) GetUsage() uint64

GetUsage returns the current usage.

func (*ShardedLRUCache) Insert

func (c *ShardedLRUCache) Insert(key CacheKey, value []byte, charge uint64) *Handle

Insert adds a block to the cache.

func (*ShardedLRUCache) Lookup

func (c *ShardedLRUCache) Lookup(key CacheKey) *Handle

Lookup retrieves a block from the cache.

func (*ShardedLRUCache) Release

func (c *ShardedLRUCache) Release(handle *Handle)

Release releases a handle.

func (*ShardedLRUCache) SetCapacity

func (c *ShardedLRUCache) SetCapacity(capacity uint64)

SetCapacity sets the maximum capacity.

Jump to

Keyboard shortcuts

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