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 ¶
- type Config
- type Item
- type Optimizer
- func (o *Optimizer) Close()
- func (o *Optimizer) ForceOptimize() int
- func (o *Optimizer) GetStats() map[string]interface{}
- func (o *Optimizer) SetCostLimit(limit int64)
- func (o *Optimizer) SetInterval(interval time.Duration)
- func (o *Optimizer) SetMaxItems(maxItems int)
- func (o *Optimizer) SetMemoryLimit(limit int64)
- func (o *Optimizer) SetSampleRatio(ratio float64)
- type OptimizerConfig
- type Stats
- type Store
- func (s *Store) Clear()
- func (s *Store) Close()
- func (s *Store) Count() int64
- func (s *Store) Delete(key uint64) bool
- func (s *Store) DeleteExpired() int
- func (s *Store) ForEach(f func(key uint64, item *Item) bool)
- func (s *Store) Get(key uint64) (*Item, bool)
- func (s *Store) Keys() []uint64
- func (s *Store) Set(key uint64, value interface{}, size, cost int64, expireAt int64)
- func (s *Store) SetBatch(items []*Item)
- func (s *Store) Size() int64
- func (s *Store) Stats() *Stats
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 表示存储在缓存中的条目。
type Optimizer ¶
type Optimizer struct {
// contains filtered or unexported fields
}
Optimizer 存储优化器 用于优化内存使用和性能,包括内存回收、分片重平衡等
func NewOptimizer ¶
func NewOptimizer(store *Store, config *OptimizerConfig) *Optimizer
NewOptimizer 创建一个新的优化器
func (*Optimizer) SetInterval ¶
SetInterval 设置优化间隔
func (*Optimizer) SetMaxItems ¶
SetMaxItems 设置每次优化的最大项数
func (*Optimizer) SetMemoryLimit ¶
SetMemoryLimit 设置内存限制
func (*Optimizer) SetSampleRatio ¶
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 ¶
NewStore creates a new storage instance.
NewStore 创建一个新的存储实例。
Parameters:
- config: Configuration options for the storage
Returns:
- *Store: A new storage instance