more code

This commit is contained in:
developer committed 2026-09-27 11:34:26 +00:00
1 parent f24fd63e9a
commit ee72257c64
29 files changed
+5636 -440

No files matched your search

+10 -10
View File
@@ -22,8 +22,10 @@ type LRU struct {
shards []*shard
mask uint64
// perShard is the per-shard entry budget.
perShard int
// perShard is the per-shard entry budget. It is atomic because a config
// reload resizes the cache from the API goroutine while queries are
// inserting in every shard.
perShard atomic.Int64
// counters are the hot-path stats, kept as atomics so Get does not need
// the lock just to record a hit.
@@ -69,13 +71,10 @@ func NewLRU(size, shards int) *LRU {
l := &LRU{
shards: make([]*shard, shards),
mask: uint64(shards - 1),
perShard: size / shards,
seed: maphash.MakeSeed(),
prefetchGate: make(chan struct{}, 64),
}
if l.perShard < 1 {
l.perShard = 1
}
l.perShard.Store(int64(max(1, size/shards)))
for i := range l.shards {
l.shards[i] = &shard{
items: make(map[Key]*list.Element, 1024),
@@ -140,7 +139,7 @@ func (l *LRU) Get(k Key, now time.Time) (e *Entry, kind HitKind) {
}
s.mu.Unlock()
e.Hits++
e.Hits.Add(1)
if kind == Fresh {
l.hits.Add(1)
} else {
@@ -171,7 +170,8 @@ func (l *LRU) Put(k Key, e *Entry) {
s.bytes += e.Bytes
l.bytes.Add(int64(e.Bytes))
var dropped int
for s.ll.Len() > l.perShard {
budget := int(l.perShard.Load())
for s.ll.Len() > budget {
oldest := s.ll.Back()
if oldest == nil {
break
@@ -347,7 +347,7 @@ func (l *LRU) Stats() LRUStats {
Expired: l.expired.Load(),
HitRate: rate,
Shards: len(l.shards),
Capacity: l.perShard * len(l.shards),
Capacity: int(l.perShard.Load()) * len(l.shards),
}
}
@@ -358,5 +358,5 @@ func (l *LRU) SetCapacity(size, shards int) {
shards = len(l.shards)
}
shards = roundPow2(shards)
l.perShard = max(1, size/shards)
l.perShard.Store(int64(max(1, size/shards)))
}