Documentation
¶
Index ¶
- Constants
- type Entry
- type Store
- func (s *Store) Delete(key string) bool
- func (s *Store) Expire(key string, ttl time.Duration) bool
- func (s *Store) ForEach(fn func(key string, value []byte, ttl time.Duration) bool)
- func (s *Store) Get(key string) ([]byte, bool)
- func (s *Store) Keys() []string
- func (s *Store) Len() int
- func (s *Store) Set(key string, value []byte, ttl time.Duration)
- func (s *Store) ShardStats() []int
- func (s *Store) Stats() StoreStats
- type StoreStats
- type Sweeper
Constants ¶
const (
// DefaultShardCount is the number of shards used to reduce lock contention.
DefaultShardCount = 256
)
const ( // DefaultSweepInterval is how often the background sweeper checks a shard. DefaultSweepInterval = 100 * time.Millisecond )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry struct {
Value []byte
ExpiresAt time.Time // Zero value means no expiry.
CreatedAt time.Time
}
Entry represents a single cached value with metadata.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a sharded in-memory key-value store with TTL support. It distributes keys across multiple shards to minimize lock contention.
func NewWithShards ¶
NewWithShards creates a new Store with the specified number of shards. shardCount should be a power of 2 for efficient masking.
func (*Store) Expire ¶
Expire updates the TTL on an existing key. Returns true if the key was found. A ttl of 0 removes the expiry (makes the key persistent).
func (*Store) ForEach ¶
ForEach iterates over all non-expired entries, calling fn for each one. The callback receives a copy of the value. If fn returns false, iteration stops.
func (*Store) Get ¶
Get retrieves the value for a key. Returns the value, whether it exists, and any error. Performs lazy expiry — expired entries are deleted on access.
func (*Store) Keys ¶
Keys returns all non-expired keys in the store. This is intended for debugging — it scans all shards.
func (*Store) Set ¶
Set stores a key-value pair with an optional TTL. If ttl is 0, the entry never expires. Overwrites any existing entry for the key.
func (*Store) ShardStats ¶
ShardStats returns per-shard item counts (for debugging/metrics).
func (*Store) Stats ¶
func (s *Store) Stats() StoreStats
Stats returns a snapshot of the store's operational metrics.
type StoreStats ¶
type StoreStats struct {
Hits uint64
Misses uint64
Sets uint64
Deletes uint64
Expirations uint64
// contains filtered or unexported fields
}
StoreStats tracks operational metrics for the store.
func (*StoreStats) Snapshot ¶
func (s *StoreStats) Snapshot() StoreStats
Snapshot returns a copy of the current stats.
type Sweeper ¶
type Sweeper struct {
// contains filtered or unexported fields
}
Sweeper runs a background goroutine that actively expires stale entries. It uses a Redis-inspired approach: sample random keys from each shard and delete expired ones. If the expiry rate is high, repeat immediately.
func NewSweeper ¶
NewSweeper creates a new TTL sweeper for the given store.