storage

package
v0.0.0-...-dfb9c52 Latest Latest
Warning

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

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

Documentation

Overview

Package storage 提供高性能的分片存储实现

Package storage provides high-performance sharded storage implementation. Package storage 提供高性能的分片存储实现。

This package serves as the core storage layer for the cache, supporting high-concurrency read/write operations and atomic operations. It uses sharding to reduce lock contention and improve performance in multi-threaded environments. The storage is optimized for fast lookups and efficient memory usage.

本包作为缓存的核心存储层,支持高并发读写和原子操作。它使用分片技术来减少锁竞争, 提高多线程环境中的性能。存储层针对快速查找和高效内存使用进行了优化。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// ShardCount is the number of shards, recommended to be a power of 2.
	// ShardCount 是分片数量,建议为2的幂次方。
	ShardCount int

	// InitialCapacity is the initial capacity per shard.
	// InitialCapacity 是每个分片的初始容量。
	InitialCapacity int

	// LoadFactor determines when to resize the internal maps.
	// LoadFactor 决定何时调整内部映射的大小。
	LoadFactor float64

	// TrackAccessTime enables tracking of access times for items.
	// TrackAccessTime 是否启用访问时间追踪。
	TrackAccessTime bool

	// BatchSize is the buffer size for batch operations.
	// BatchSize 是批量操作的缓冲区大小。
	BatchSize int

	// MaxMemoryBytes is the maximum memory usage in bytes.
	// MaxMemoryBytes 是最大内存使用量(字节)。
	MaxMemoryBytes int64

	// AsyncAccessUpdate enables asynchronous updates of access times.
	// AsyncAccessUpdate 是否启用异步更新访问时间。
	AsyncAccessUpdate bool
}

Config contains configuration options for the storage. Config 存储层配置选项。

type Item

type Item struct {
	Key        uint64      // Unique identifier / 键
	Value      interface{} // Cached value / 值
	Size       int64       // Size in bytes / 条目大小(字节)
	AccessTime int64       // Last access timestamp (Unix nano) / 最后访问时间(Unix纳秒)
	ExpireAt   int64       // Expiration timestamp (Unix nano, 0 means never expire) / 过期时间(Unix纳秒,0表示永不过期)
	Cost       int64       // Cost value for admission control / 成本,用于容量控制
	Flags      uint32      // Bit flags for item status / 标志位,用于标记条目状态
}

Item represents an entry stored in the cache. Item 表示存储在缓存中的条目。

func (*Item) Clone

func (item *Item) Clone() *Item

Clone creates a copy of the item.

Clone 创建条目的副本。

Returns:

  • *Item: A new item with the same values

func (*Item) IsExpired

func (item *Item) IsExpired() bool

IsExpired checks if the item has expired.

IsExpired 判断条目是否已过期。

Returns:

  • bool: True if the item has expired, false otherwise

type Optimizer

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

Optimizer 存储优化器 用于优化内存使用和性能,包括内存回收、分片重平衡等

func NewOptimizer

func NewOptimizer(store *Store, config *OptimizerConfig) *Optimizer

NewOptimizer 创建一个新的优化器

func (*Optimizer) Close

func (o *Optimizer) Close()

Close 关闭优化器

func (*Optimizer) ForceOptimize

func (o *Optimizer) ForceOptimize() int

ForceOptimize 强制执行一次优化

func (*Optimizer) GetStats

func (o *Optimizer) GetStats() map[string]interface{}

GetStats 获取优化器的统计信息

func (*Optimizer) SetCostLimit

func (o *Optimizer) SetCostLimit(limit int64)

SetCostLimit 设置成本限制

func (*Optimizer) SetInterval

func (o *Optimizer) SetInterval(interval time.Duration)

SetInterval 设置优化间隔

func (*Optimizer) SetMaxItems

func (o *Optimizer) SetMaxItems(maxItems int)

SetMaxItems 设置每次优化的最大项数

func (*Optimizer) SetMemoryLimit

func (o *Optimizer) SetMemoryLimit(limit int64)

SetMemoryLimit 设置内存限制

func (*Optimizer) SetSampleRatio

func (o *Optimizer) SetSampleRatio(ratio float64)

SetSampleRatio 设置采样比例

type OptimizerConfig

type OptimizerConfig struct {
	// 优化间隔(秒)
	Interval int64

	// 每次优化的最大项数
	MaxItems int

	// 内存限制(字节)
	MemoryLimit int64

	// 成本限制
	CostLimit int64

	// 采样比例(0-1)
	SampleRatio float64
}

OptimizerConfig 优化器配置

type Stats

type Stats struct {
	Hits            uint64 // Cache hit count / 命中次数
	Misses          uint64 // Cache miss count / 未命中次数
	Evictions       uint64 // Eviction count / 淘汰次数
	Expirations     uint64 // Expiration count / 过期次数
	ItemCount       int64  // Current item count / 条目数量
	BytesSize       int64  // Current size in bytes / 总大小(字节)
	EvictionCost    int64  // Cost of evicted items / 淘汰成本
	EvictionCount   uint64 // Number of eviction operations / 淘汰次数
	ExpiredCount    uint64 // Number of expired items / 过期条目数
	ConflictCount   uint64 // Hash conflict count / 冲突次数
	OverwriteCount  uint64 // Overwrite count / 覆盖次数
	AsyncQueueSize  int64  // Size of async operation queue / 异步队列大小
	AsyncDropCount  uint64 // Count of dropped async operations / 异步丢弃次数
	ReadLatencyNs   int64  // Average read latency in ns / 读取延迟(纳秒)
	WriteLatencyNs  int64  // Average write latency in ns / 写入延迟(纳秒)
	DeleteLatencyNs int64  // Average delete latency in ns / 删除延迟(纳秒)
}

Stats collects storage statistics. Stats 存储统计信息。

type Store

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

Store is the main implementation of sharded storage. It reduces lock contention by dividing the keyspace into multiple shards.

Store 是分片存储的主要实现。 通过将键空间分割为多个分片来减少锁竞争,提高并发性能。

func NewStore

func NewStore(config *Config) *Store

NewStore creates a new storage instance.

NewStore 创建一个新的存储实例。

Parameters:

  • config: Configuration options for the storage

Returns:

  • *Store: A new storage instance

func (*Store) Clear

func (s *Store) Clear()

Clear 清空存储

func (*Store) Close

func (s *Store) Close()

Close 关闭存储

func (*Store) Count

func (s *Store) Count() int64

Count 返回条目数量

func (*Store) Delete

func (s *Store) Delete(key uint64) bool

Delete 删除指定键的条目

func (*Store) DeleteExpired

func (s *Store) DeleteExpired() int

DeleteExpired 删除所有过期的条目

func (*Store) ForEach

func (s *Store) ForEach(f func(key uint64, item *Item) bool)

ForEach 遍历所有条目

func (*Store) Get

func (s *Store) Get(key uint64) (*Item, bool)

Get 获取指定键的条目

func (*Store) Keys

func (s *Store) Keys() []uint64

Keys 返回所有键

func (*Store) Set

func (s *Store) Set(key uint64, value interface{}, size, cost int64, expireAt int64)

Set 设置指定键的条目

func (*Store) SetBatch

func (s *Store) SetBatch(items []*Item)

SetBatch 批量设置条目

func (*Store) Size

func (s *Store) Size() int64

Size 返回存储大小(字节)

func (*Store) Stats

func (s *Store) Stats() *Stats

Stats 返回存储统计信息

Jump to

Keyboard shortcuts

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