cache

package
v0.0.0-...-4b532c3 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultExpireDuration = time.Hour
	DefaultGcInterval     = 24 * time.Hour
	DefaultTopK           = 5
)

Default configuration constants

Variables

This section is empty.

Functions

func Decode

func Decode(value uint64) string

Decode converts uint64 back to location

func Encode

func Encode(ip string) uint64

Encode converts location to uint64 representation

func ForceGC

func ForceGC()

ForceGC triggers immediate garbage collection on the cache pool

func IPStr2Int

func IPStr2Int(s string) uint32

IPStr2Int converts IPv4 format string to big-endian uint32

func Init

func Init()

Init initializes the cache system with background garbage collection

func IntToIP

func IntToIP(v uint32) string

IntToIP converts a big-endian uint32 to IPv4 format string

func IsDebugMode

func IsDebugMode() bool

IsDebugMode returns whether debug mode is enabled

func Query

func Query(p *QueryParam) map[uint64]int

Query searches the cache for matching entries based on prompt or prompt hash

func Save

func Save(p *SaveParam)

Save stores a new cache entry with the specified parameters

func SetDebugMode

func SetDebugMode(t bool)

SetDebugMode enables or disables debug mode

func SetDefaultQueryTopK

func SetDefaultQueryTopK(k int)

SetDefaultQueryTopK sets the default TopK value for queries

func SetExpireDuration

func SetExpireDuration(d time.Duration)

SetExpireDuration sets the expiration duration for cache entries

func SetGcInterval

func SetGcInterval(d time.Duration)

SetGcInterval sets the garbage collection interval

Types

type CachePool

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

CachePool manages multiple cache instances using radix trees

func NewCachePool

func NewCachePool() *CachePool

NewCachePool creates a new cache pool instance

func (*CachePool) GC

func (cp *CachePool) GC()

GC performs garbage collection on all cache trees

func (*CachePool) QueryHash

func (cp *CachePool) QueryHash(key string, keys []uint64, topK int) map[uint64]int

QueryHash searches cache by pre-computed hash keys and returns topK matches

func (*CachePool) SaveHash

func (cp *CachePool) SaveHash(key string, keys []uint64, ip string)

SaveHash stores pre-computed hash keys in cache with location information

type MinHeap

type MinHeap []*MinHeapEntry

MinHeap implements heap.Interface for maintaining top-K elements

func (MinHeap) Len

func (h MinHeap) Len() int

Len returns the number of elements in the heap

func (MinHeap) Less

func (h MinHeap) Less(i, j int) bool

Less compares two elements for heap ordering (min-heap)

func (*MinHeap) Pop

func (h *MinHeap) Pop() any

Pop removes and returns the smallest element from the heap

func (*MinHeap) Push

func (h *MinHeap) Push(x any)

Push adds an element to the heap

func (MinHeap) Swap

func (h MinHeap) Swap(i, j int)

Swap exchanges elements and updates their indices

type MinHeapEntry

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

MinHeapEntry represents an entry in the min-heap with key-value pair and index

type ModelQueryRequest

type ModelQueryRequest struct {
	Cluster string `json:"cluster" form:"cluster" binding:"required"`
}

ModelQueryRequest defines parameters for model tree information queries

type QueryParam

type QueryParam struct {
	Cluster    string   `json:"cluster" form:"cluster" binding:"required"`
	PromptHash []uint64 `json:"prompt_hash" binding:"required"`
	// TopK represents the maximum number of results to return, 0 means use default configuration value
	TopK int `json:"top_k"`
}

QueryParam defines parameters for cache query operations

type RadixNode

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

func NewRadixNode

func NewRadixNode(prefix []uint64) *RadixNode

type RadixTree

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

RadixTree implements a radix tree for efficient prefix-based storage and retrieval

func NewRadixTree

func NewRadixTree(name string) *RadixTree

NewRadixTree creates a new radix tree with the given name

func (*RadixTree) CleanExpireNode

func (t *RadixTree) CleanExpireNode()

CleanExpireNode is a wrapper for traversal calls

func (*RadixTree) GetTreeInfo

func (t *RadixTree) GetTreeInfo() *RadixTreeInfo

GetTreeInfo gets information from the last traversal

func (*RadixTree) Insert

func (t *RadixTree) Insert(key []uint64, data uint64, _ any)

Insert adds a data entry to the radix tree All matching nodes during tree traversal need to insert data data is the unique index, value is the corresponding value (currently not needed)

func (*RadixTree) MatchAll

func (t *RadixTree) MatchAll(key []uint64, topK int) map[uint64]int

MatchAll returns all data that matches the common prefix of the key Returns: {data, matched prefix length} Currently uses uint64(ip32+ip32) to represent data, may need adjustment if adding content later

func (*RadixTree) Traversal

func (t *RadixTree) Traversal(handle StackRadixNodeHandle)

Traversal performs level-order traversal of the entire tree and calls handle for each node

type RadixTreeInfo

type RadixTreeInfo struct {
	Info       map[int]*RadixTreeLevelInfo `json:"info"`        // Level information by depth
	UpdateTime time.Time                   `json:"update_time"` // Last update timestamp
}

RadixTreeInfo contains aggregated information about the radix tree

func ModelQuery

func ModelQuery(p *ModelQueryRequest) *RadixTreeInfo

ModelQuery retrieves radix tree information for a specific cluster

type RadixTreeLevelInfo

type RadixTreeLevelInfo struct {
	NodesCount   int `json:"nodes_count"`    // Number of nodes at this level
	PrefixTotal  int `json:"prefix_total"`   // Total prefix count at this level
	MaxPrefixLen int `json:"max_prefix_len"` // Maximum prefix length at this level
	MinPrefixLen int `json:"min_prefix_len"` // Minimum prefix length at this level
}

RadixTreeLevelInfo contains statistics for a specific level in the radix tree

type SaveParam

type SaveParam struct {
	Cluster    string   `json:"cluster" form:"cluster" binding:"required"`
	PromptHash []uint64 `json:"prompt_hash" binding:"required"`
	IP         string   `json:"ip" binding:"required,ipv4"`
}

SaveParam defines parameters for cache save operations

type StackRadixNode

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

type StackRadixNodeHandle

type StackRadixNodeHandle func(current *RadixNode, n *StackRadixNode) bool

StackRadixNodeHandle processes corresponding node information during traversal If returns false, this node will no longer participate in subsequent traversal Usage: During global GC, if node has expired, skip traversing subsequent nodes Node processing in Handle doesn't require locking, default traversal has full tree lock

type TopKMap

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

TopKMap maintains top-K largest values using a min-heap TODO: Optimization potential - may not need min-heap directly due to RadixTree patterns

func NewTopKMap

func NewTopKMap(k int) *TopKMap

NewTopKMap creates a new TopKMap with specified capacity

func (*TopKMap) Add

func (m *TopKMap) Add(key uint64, value int)

Add inserts or updates a key-value pair, maintaining top-K order No locking - each request constructs TopK independently, no concurrent updates

func (*TopKMap) GetMap

func (m *TopKMap) GetMap() map[uint64]int

GetMap returns the current top-K entries as a map

Jump to

Keyboard shortcuts

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