package redis

import (
	
	
	
	
	
)

// cacheEntryState tracks the lifecycle of a local cache entry.
type cacheEntryState uint8

const (
	// cacheEntryInProgress marks a placeholder entry while a value is being fetched.
	cacheEntryInProgress cacheEntryState = iota
	// cacheEntryValid marks an entry that contains a value that can be returned.
	cacheEntryValid
)

// cacheEntry represents a cached command reply and its Redis-key associations.
type cacheEntry struct {
	cacheKey  string
	redisKeys []string
	value     []byte
	state     cacheEntryState

	token      uint64
	sizeBytes  int64
	reservedAt time.Time
	waitCh     chan struct{}
	waitClosed bool

	// lastAccessNs orders entries for eviction: a global atomic counter bumped
	// when the entry is created, reserved or fulfilled — all of which happen
	// under the shard write lock, on the MISS path. It is stored atomically
	// because the read path reads it under the shard's RLock.
	//
	// It is NOT bumped on a read any more; a read sets readSinceSweep instead.
	lastAccessNs atomic.Int64

	// readSinceSweep records that a READ has touched this entry since the last
	// eviction sweep. It is the second-chance bit for eviction: a victim is
	// chosen from the entries with the bit CLEAR, and the sweep clears the bits
	// when every candidate has been read.
	//
	// It exists because writing lastAccessNs on every hit was the dominant cost
	// of a cache hit under concurrency. ~40k resident entries at a few hundred
	// bytes each is a working set far larger than L2; the read already touches
	// the entry's cache line, but a STORE dirties it, and a dirty line costs a
	// writeback when it is evicted from the CPU cache. Measured on a 14-core
	// host at 256 concurrent readers, get=90/set=10, 99.94% hit rate:
	// 263,801 -> 328,842 reads/s (+24.7%) and 27.0 -> 18.9 CPU us/op, with the
	// hit rate unchanged. Removing the recency update altogether measured
	// +40.5%, so this recovers about three fifths of the available headroom
	// while keeping eviction honest.
	//
	// A load that finds the bit already set leaves the line SHARED, so every
	// core can hold it at once; only the first read after a sweep writes.
	readSinceSweep atomic.Bool

	// refreshKeep marks an IN_PROGRESS refresh reservation: fulfill publishes
	// it with refreshAccessNs and refreshRead instead of a fresh recency. See
	// stageRefreshAccess. Written and read under the shard write lock.
	refreshKeep     bool
	refreshRead     bool
	refreshAccessNs int64

	// validAt retains time.Now's monotonic component for the MaxStaleness
	// backstop, so wall-clock corrections cannot extend an entry's lifetime.
	// Written under Lock (Set/Fulfill), read under RLock (get).
	validAt time.Time

	// fetchSeq is the global cscFetchSeq value at the moment this entry's fetch was
	// ISSUED (Reserve), carried unchanged through fulfill. It lets a batched
	// invalidation tell "this value predates me" from "this value was refetched
	// after me": an invalidation snapshots cscFetchSeq at OBSERVE time, and a delete
	// is skipped when entry.fetchSeq > that snapshot (the fetch was issued after the
	// invalidate, so it reached a server that had already applied the write). Fetch-
	// ISSUE order is used, not fulfill-COMPLETION order, because the invalidation and
	// the reply travel on different connections with no ordering — a stale reply can
	// fulfill after the invalidate is observed (see deleteByRedisKey). Set/read under
	// the shard Lock.
	fetchSeq uint64

	// ownerConnID is the conn that fetched this entry (set by FulfillOwned; 0 =
	// none). Default CLIENT TRACKING sends a key's invalidation only to that
	// conn, so the entry must be evicted when it goes away (see EvictByConn).
	ownerConnID uint64
}

// lruSequence is the global monotonic counter feeding lastAccessNs. It totally
// orders recency across all entries in all shards for approximate-LRU eviction.
var lruSequence atomic.Int64

// nextLRUToken returns the next strictly-greater LRU token.
func nextLRUToken() int64 {
	return lruSequence.Add(1)
}

// cscFetchSeq is the global monotonic counter feeding cacheEntry.fetchSeq. It
// totally orders fetch-ISSUE (Reserve) events against invalidation OBSERVE
// events so a batched delete can skip an entry refetched after the invalidation
// (see cacheEntry.fetchSeq).
var cscFetchSeq atomic.Uint64

// nextFetchSeq returns the next strictly-greater fetch-issue sequence.
func nextFetchSeq() uint64 {
	return cscFetchSeq.Add(1)
}

// CacheSizer calculates estimated memory usage in bytes for a cache entry.
//
// Experimental: this API may change in a minor release.
type CacheSizer func(cacheKey string, redisKeys []string, value []byte) int64

// CacheConfig configures a local cache instance.
//
// Experimental: this API may change in a minor release.
type CacheConfig struct {
	// MaxEntries limits the number of entries. Zero or negative means unlimited.
	MaxEntries int
	// MaxMemoryBytes limits estimated memory usage in bytes. Zero or negative means unlimited.
	//
	// If both MaxEntries and MaxMemoryBytes are unlimited, MaxEntries defaults to
	// defaultCacheMaxEntries so the cache cannot grow without bound. The cache is
	// sharded 16 ways (above small thresholds) and each shard enforces its 1/16
	// share, so an entry larger than MaxMemoryBytes/16 is never admitted —
	// size it to at least 16× your largest reply.
	MaxMemoryBytes int64
	// Sizer estimates memory usage per entry. If nil, a built-in approximation is used.
	//
	// Sizer may be invoked concurrently from multiple goroutines and must be
	// thread-safe. It must return quickly and must not call back into the
	// cache (Get, Set, Delete*, Flush, etc.): some call sites hold an internal
	// shard lock, so re-entry can deadlock.
	Sizer CacheSizer
	// StaleTimeout is the duration after which an IN_PROGRESS placeholder is
	// considered stale and eligible for takeover by a new Reserve call.
	// If zero, defaults to defaultStaleTimeout (5s).
	StaleTimeout time.Duration

	// DrainInterval is the background-drainer period (default 5ms; zero uses the
	// default): how often idle pool conns are swept for buffered "invalidate"
	// frames, roughly bounding cache-hit staleness. Values below 1ms are clamped
	// to 1ms.
	DrainInterval time.Duration

	// MaxStaleness caps how long a cached entry is served after it became valid,
	// regardless of invalidation. It is a correctness
	// BACKSTOP for lost invalidations or connection-lifecycle gaps ("Window 2"), not
	// the primary freshness mechanism. Keep it well above the invalidation round-trip
	// (e.g. seconds); per-entry refetch overhead scales ~1/MaxStaleness.
	//
	// Default: 0 (disabled).
	MaxStaleness time.Duration
}

// Cache is the thread-safe storage contract used by client-side caching.
//
// All methods may be called concurrently. Cache keys and Redis keys are opaque
// strings and must be preserved exactly. Removing a reservation must wake any
// Get calls waiting for it.
//
// Reserve must allow only one caller to fetch a missing key and return a token
// that is valid until FulfillOwned, Cancel, or an eviction removes that
// reservation. FulfillOwned and Cancel must modify only a reservation with the
// matching token. Get may wait for an in-progress reservation and must stop
// waiting when ctx is done.
//
// Experimental: this API may change in a minor release.
type Cache interface {
	Get(ctx context.Context, cacheKey string) ([]byte, bool)
	Reserve(cacheKey string, redisKeys []string) (token uint64, shouldFetch bool)
	// FulfillOwned publishes a reserved value and records the connection that
	// fetched it so the entry can be evicted if that connection loses tracking.
	FulfillOwned(cacheKey string, token, ownerConnID uint64, value []byte) bool
	Cancel(cacheKey string, token uint64) bool
	DeleteByRedisKey(redisKey string) int
	DeleteByCacheKey(cacheKey string) bool
	// EvictByConn removes every entry fetched by connID.
	EvictByConn(connID uint64) int
	Flush() int
}

const (
	defaultStaleTimeout    = 5 * time.Second
	defaultCacheShardCount = 16

	// defaultCacheMaxEntries bounds the cache when the config leaves both
	// MaxEntries and MaxMemoryBytes unlimited (matches the 10k-entry default
	// other Redis clients use, e.g. redis-py).
	defaultCacheMaxEntries = 10000

	// shardingThresholdEntries / shardingThresholdBytes: caches with capacity
	// below these thresholds fall back to a single shard so global LRU /
	// memory-cap semantics behave exactly as a non-sharded cache would.
	shardingThresholdEntries = 64
	shardingThresholdBytes   = 64 * 1024
)

// NewLocalCache creates a thread-safe local cache with approximate-LRU
// eviction. The cache is internally sharded by cache-key hash to reduce
// mutex contention under high concurrent access.
//
// Experimental: this API may change in a minor release.
func ( CacheConfig) *LocalCache {
	 := .Sizer
	if  == nil {
		 = defaultCacheSizer
	}

	 := .StaleTimeout
	if  <= 0 {
		 = defaultStaleTimeout
	}

	 := .MaxEntries
	 := .MaxMemoryBytes
	// An unbounded cache can grow until the process OOMs; require at least
	// one limit.
	if  <= 0 &&  <= 0 {
		 = defaultCacheMaxEntries
	}

	 := defaultCacheShardCount
	if  > 0 &&  < shardingThresholdEntries {
		 = 1
	}
	if  > 0 &&  < int64(shardingThresholdBytes) {
		 = 1
	}

	 := &LocalCache{
		shards:     make([]cacheShard, ),
		shardCount: uint32(),
		shardMask:  uint32( - 1),
		sizer:      ,
	}
	for  := range .shards {
		 := &.shards[]
		.entries = make(map[string]*cacheEntry)
		.byRedisKey = make(map[string]map[string]struct{})
		.byConnID = make(map[uint64]map[string]struct{})
		// Distribute capacity so the per-shard caps sum to exactly the
		// configured limits; a ceil-per-shard split would let total residency
		// exceed MaxEntries/MaxMemoryBytes.
		if  > 0 {
			.maxEntries =  / 
			if  < % {
				.maxEntries++
			}
		}
		if  > 0 {
			.maxMemoryBytes =  / int64()
			if int64() < %int64() {
				.maxMemoryBytes++
			}
		}
		.maxStaleness = .MaxStaleness
		.sizer = 
		.staleTimeout = 
	}
	return 
}

// effectiveMaxStaleness reports the cache's staleness bound (0 = none). Every
// shard carries the same value, so shard 0 is authoritative. Used by
// Options.init to run the batch-window-vs-staleness sanity warning for an
// INJECTED *LocalCache too, where no ClientSideCacheConfig exists to read.
func ( *LocalCache) () time.Duration {
	if len(.shards) == 0 {
		return 0
	}
	return .shards[0].maxStaleness
}

// LocalCache is the built-in sharded approximate-LRU cache.
//
// Experimental: this API may change in a minor release.
type LocalCache struct {
	shards     []cacheShard
	shardCount uint32
	shardMask  uint32
	sizer      CacheSizer

	nextToken atomic.Uint64
	hits      atomic.Uint64
	misses    atomic.Uint64

	// Invalidation accounting for refresh-on-invalidate (see CSCRefreshStats).
	// invalidations counts keys named in INCOMING pushes, tallied once at the
	// handler choke point before dedup/batching. deletions/deletionsNoop count
	// APPLIED deletes (post-dedup) and the subset that matched no live entry. The
	// gap between invalidations and deletions is the direct measure of dedup +
	// duplicate invalidations (and, under a flood, the spill-cap full-Flush).
	invalidations atomic.Uint64
	deletions     atomic.Uint64
	deletionsNoop atomic.Uint64
}

var _ Cache = (*LocalCache)(nil)

// cacheShard holds the state for one shard of LocalCache. The mutex
// protects entries, byRedisKey, byConnID, and usedBytes.
type cacheShard struct {
	mu         sync.RWMutex
	entries    map[string]*cacheEntry
	byRedisKey map[string]map[string]struct{}
	// byConnID is the owning-conn reverse index (twin of byRedisKey): conn id ->
	// its cache keys. Populated by FulfillOwned, cleaned in removeEntryLocked,
	// consumed by EvictByConn.
	byConnID  map[uint64]map[string]struct{}
	usedBytes int64

	maxEntries     int
	maxMemoryBytes int64
	maxStaleness   time.Duration
	sizer          CacheSizer
	staleTimeout   time.Duration
}

// collectHotAndDeleteBatch applies a whole invalidation batch under ONE
// acquisition of this shard's lock.
//
// The per-key work is identical to collectHotAndDelete; only the locking
// granularity changes. The caller loops shards on the outside and keys on the
// inside, so a batch of N costs 16 lock acquisitions instead of 16*N -- which
// is what let the single batcher worker fall permanently behind a 20k/sec
// invalidation stream and leave the cache serving stale entries.
//
// keys and the parallel guards are indexed together: sinceTokens[i] and
// fetchSnaps[i] belong to keys[i].
//
// matched[i] is set when this shard removed something for keys[i]. The caller
// ORs it across shards, because one redis key can appear in several shards: a
// multi-key entry is filed under its CACHE key's shard, so each of its redis
// keys is indexed wherever that entry lives. Per-key match tracking is what
// keeps the no-op deletion count equal to the single-key path's.
func ( *cacheShard) ( []string,  []int64,  []uint64,  []cscRefreshTarget,  []bool) ([]cscRefreshTarget, int) {
	.mu.Lock()
	defer .mu.Unlock()

	 := 0
	for ,  := range  {
		,  := .byRedisKey[]
		if ! {
			continue
		}
		,  := [], []
		 := make([]string, 0, len())
		for  := range  {
			 = append(, )
		}
		for ,  := range  {
			,  := .entries[]
			if  && .fetchSeq >  {
				continue
			}
			if  && .state == cacheEntryValid && .lastAccessNs.Load() >  {
				 := make([]string, len(.redisKeys))
				copy(, .redisKeys)
				 = append(, cscRefreshTarget{
					cacheKey:  ,
					redisKeys: ,
					accessNs:  .lastAccessNs.Load(),
					// The second-chance bit, as collectHotAndDelete keeps it:
					// the refresh republishes the entry with it.
					read:     .readSinceSweep.Load(),
					valBytes: len(.value),
				})
			}
			if .removeEntryLocked() {
				[] = true
				++
			}
		}
	}
	return , 
}

// deleteManyByRedisKeyCollectingHot is the cache-level batch entry point: one
// pass over the shards, every key of the batch handled under each shard's
// single lock.
func ( *LocalCache) ( []string,  []int64,  []uint64,  []cscRefreshTarget) ([]cscRefreshTarget, int) {
	if len() == 0 {
		return , 0
	}
	 := 0
	 := make([]bool, len())
	for  := range .shards {
		var  int
		,  = .shards[].collectHotAndDeleteBatch(, , , , )
		 += 
	}
	// Applied-delete accounting, per KEY, exactly as the single-key path does
	// it: every key is one deletion, and a key that removed nothing is one
	// no-op. Deriving the no-op count from the batch total instead would
	// undercount -- a batch where one key matched and nine did not would
	// record zero no-ops rather than nine.
	.deletions.Add(uint64(len()))
	 := 0
	for ,  := range  {
		if ! {
			++
		}
	}
	if  > 0 {
		.deletionsNoop.Add(uint64())
	}
	return , 
}

// shardFor returns the shard responsible for cacheKey.
func ( *LocalCache) ( string) *cacheShard {
	if .shardCount == 1 {
		return &.shards[0]
	}
	return &.shards[fnv1a32()&.shardMask]
}

// fnv1a32 returns the FNV-1a 32-bit hash of s. Allocation-free.
func fnv1a32( string) uint32 {
	const (
		 uint32 = 2166136261
		  uint32 = 16777619
	)
	 := 
	for  := 0;  < len(); ++ {
		 ^= uint32([])
		 *= 
	}
	return 
}

const defaultCacheEntryOverhead int64 = 96

func defaultCacheSizer( string,  []string,  []byte) int64 {
	 := defaultCacheEntryOverhead + int64(len()+len())
	for ,  := range  {
		 += int64(len()) + 16
	}
	if  < 0 {
		return 0
	}
	return 
}

// Get returns a copy of a cached value, waiting for an in-progress fetch when
// necessary.
func ( *LocalCache) ( context.Context,  string) ([]byte, bool) {
	,  := .get(, , true)
	return , 
}

// getShared is Get without the defensive copy. The returned slice ALIASES the
// cache entry, so the caller must neither mutate nor retain it; it is valid
// only until the caller returns.
//
// Safe because a published value is immutable: cacheShard.get never writes
// through the slice, and a refetch REPLACES entry.value wholesale under the
// shard write lock rather than editing it in place, so an old slice a reader
// already holds keeps its contents.
//
// Not on the Cache interface, and deliberately so: Get's []byte return means a
// third-party implementation's caller may legitimately retain what it gets
// back, so the copy has to stay there. Only the built-in cache paired with the
// built-in read path (processCached, which parses the bytes and drops them)
// can skip it -- the same "only the built-in *LocalCache" gate miss coalescing
// uses.
func ( *LocalCache) ( context.Context,  string) ([]byte, bool) {
	return .get(, , false)
}

func ( *LocalCache) ( context.Context,  string,  bool) ([]byte, bool) {
	if  == nil {
		 = context.Background()
	}
	,  := .shardFor().get(, , )
	if  {
		.hits.Add(1)
	} else {
		.misses.Add(1)
	}
	return , 
}

// get is the read-side hot path. Holds only the shard's read lock; updates
// the LRU recency timestamp via atomic store on the entry — no write-lock
// upgrade is needed.
// get is the read-side hot path. clone=false returns a slice ALIASING the
// entry (see LocalCache.getShared for why that is safe and who may use it).
func ( *cacheShard) ( context.Context,  string,  bool) ([]byte, bool) {
	for {
		.mu.RLock()
		,  := .entries[]
		if ! {
			.mu.RUnlock()
			return nil, false
		}

		if .state == cacheEntryInProgress {
			 := .waitCh
			// Bound the wait by the placeholder's remaining stale window so an
			// abandoned reservation cannot block waiters indefinitely.
			 := .staleTimeout - time.Since(.reservedAt)
			.mu.RUnlock()
			if  == nil {
				// Defensive: treat a missing waitCh as a miss to avoid busy-looping.
				return nil, false
			}
			if  <= 0 {
				// Placeholder already stale; miss so the caller refetches.
				return nil, false
			}
			// Wait for the in-flight fetch to either publish (Fulfill) or abort (Cancel/Delete/Flush).
			 := time.NewTimer()
			select {
			case <-:
				.Stop()
			case <-.Done():
				.Stop()
				return nil, false
			case <-.C:
				return nil, false
			}
			continue
		}

		if .state != cacheEntryValid {
			.mu.RUnlock()
			return nil, false
		}

		// Max-staleness backstop: a Valid entry older than maxStaleness is treated
		// as a miss and evicted, so a lost invalidation or connection-lifecycle
		// staleness (Window 2) cannot keep a stale value resident past MaxStaleness.
		// Evict under the write lock so the next access re-fetches — a stale-but-present
		// entry would otherwise suppress the re-fetch via Reserve.
		if .maxStaleness > 0 && time.Since(.validAt) > .maxStaleness {
			.mu.RUnlock()
			.mu.Lock()
			if ,  := .entries[];  &&  ==  {
				.removeEntryLocked()
			}
			.mu.Unlock()
			return nil, false
		}

		 := .value
		if  {
			 = cloneBytes()
		}
		// Mark the second-chance bit instead of stamping a recency token. The
		// load short-circuits every read after the first since the last sweep,
		// which is the overwhelming majority, and leaves the entry's cache line
		// SHARED rather than taking it exclusively per hit. See
		// cacheEntry.readSinceSweep for the measurements.
		//
		// The store is skipped entirely on a shard that is nowhere near its
		// cap, because the bit is only ever read by eviction. That guard is
		// not a micro-optimisation: the surviving store was still 9.7% of all
		// CPU (15.24s of 157.84s, against 50ms for this Load). It stays hot
		// under churn because invalidated entries are refetched constantly and
		// every fresh entry's first read finds a clear bit, and each such
		// store invalidates, across every core, a line the readers hold
		// SHARED. len(s.entries) is safe to read here: writers hold the write
		// lock, and this path holds the read lock.
		if !.readSinceSweep.Load() && .nearCapacityLocked() {
			.readSinceSweep.Store(true)
		}
		.mu.RUnlock()
		return , true
	}
}

// Stats returns cumulative activity and current residency.
func ( *LocalCache) () CSCStats {
	return CSCStats{
		Hits:             .hits.Load(),
		Misses:           .misses.Load(),
		Entries:          .Len(),
		MemoryUsageBytes: .MemoryUsage(),
	}
}

// Reserve claims a missing cache key for fetching.
func ( *LocalCache) ( string,  []string) ( uint64,  bool) {
	 := cloneStrings()
	 := make(chan struct{})
	 := time.Now()
	 := .sizer(, , nil)
	if  < 0 {
		 = 0
	}
	 := .nextToken.Add(1)

	 := .shardFor()
	.mu.Lock()
	defer .mu.Unlock()

	if ,  := .entries[];  {
		switch .state {
		case cacheEntryValid:
			// Existing-VALID hit: record access; caller will re-Get to
			// retrieve. This IS an access, so it also earns the second chance
			// the read path grants -- it is under the write lock and off the
			// hot path, so stamping the token here costs nothing.
			.lastAccessNs.Store(nextLRUToken())
			.readSinceSweep.Store(true)
			return 0, false
		case cacheEntryInProgress:
			if time.Since(.reservedAt) < .staleTimeout {
				return 0, false
			}
			.removeEntryLocked()
		default:
			return 0, false
		}
	}

	if .maxMemoryBytes > 0 &&  > .maxMemoryBytes {
		return 0, true
	}

	 := &cacheEntry{
		cacheKey:   ,
		redisKeys:  ,
		state:      cacheEntryInProgress,
		token:      ,
		reservedAt: ,
		waitCh:     ,
		sizeBytes:  ,
		// Stamp fetch-ISSUE order now so a later invalidation can tell a value
		// refetched after it (keep) from one that predates it (evict). Carried
		// through fulfill unchanged. See cacheEntry.fetchSeq.
		fetchSeq: nextFetchSeq(),
	}
	.lastAccessNs.Store(nextLRUToken())

	.setEntryLocked()
	// Evict only Valid victims. If still over capacity the shard holds only
	// in-flight placeholders: rather than abort a peer's fetch, drop this
	// reservation (the caller fetches uncached). The hard cap holds either way.
	.evictValidLocked()
	if .overCapacityLocked() {
		.removeEntryLocked()
		return 0, true
	}
	if .entries[] !=  {
		return 0, true
	}
	return , true
}

// FulfillOwned publishes a reserved value and records ownerConnID so
// EvictByConn can drop it when that connection is removed. ownerConnID == 0
// leaves the value unowned.
func ( *LocalCache) ( string, ,  uint64,  []byte) bool {
	return .fulfill(, , , )
}

func ( *LocalCache) ( string, ,  uint64,  []byte) bool {
	 := cloneBytes()

	 := .shardFor()
	.mu.Lock()
	defer .mu.Unlock()

	,  := .entries[]
	if ! || .state != cacheEntryInProgress || .token !=  {
		return false
	}

	 := .sizer(, .redisKeys, )
	if  < 0 {
		 = 0
	}
	if .maxMemoryBytes > 0 &&  > .maxMemoryBytes {
		.removeEntryLocked()
		return false
	}

	.usedBytes +=  - .sizeBytes
	.value = 
	.sizeBytes = 
	.state = cacheEntryValid
	.validAt = time.Now()
	.token = 0
	if .refreshKeep {
		// A refresh republish keeps the old entry's standing, set here in the
		// same lock as the publish so the eviction pass below, and any insert
		// after it, already see it.
		.lastAccessNs.Store(.refreshAccessNs)
		.readSinceSweep.Store(.refreshRead)
		.refreshKeep = false
	} else {
		.lastAccessNs.Store(nextLRUToken())
	}
	if  != 0 {
		.ownerConnID = 
		.indexConnLocked(, )
	}
	.closeWaitersLocked()

	// The entry just fulfilled is not a candidate in its own eviction pass. It
	// is born with its second-chance bit clear, so in a warm shard (every
	// resident entry read) it would be the only clear-bit candidate and be
	// evicted right here: a memory-capped shard, where the value only goes
	// over the cap now, could then admit nothing, and since a clear candidate
	// always existed the sweep that resets the others never ran. Excluding it
	// lets that sweep run and take the oldest entry instead.
	.evictIfNeededLocked()
	,  := .entries[]
	return  &&  ==  && .state == cacheEntryValid
}

// stageRefreshAccess sets the recency a refresh republish keeps on the
// IN_PROGRESS reservation token: lastAccessNs becomes accessNs and the
// second-chance bit becomes read, both taken from the invalidated entry.
// fulfill applies them in the same lock as the publish.
//
// The refresh does not count as a reader access: a fresh token would keep the
// key above the refresh horizon, so every later invalidation would refresh it
// again after all readers stop -- a self-sustaining refetch loop. Keeping the
// bit stops a hot key from being the first eviction victim only because the
// refresh republished it. Setting both before the publish, not after it,
// closes the gap in which an insert could evict the republished entry while
// its bit was still clear. No-op when the reservation is gone.
func ( *LocalCache) ( string,  uint64,  int64,  bool) {
	 := .shardFor()
	.mu.Lock()
	defer .mu.Unlock()
	if ,  := .entries[];  && .state == cacheEntryInProgress && .token ==  {
		.refreshKeep = true
		.refreshAccessNs = 
		.refreshRead = 
	}
}

// EvictByConn removes every entry fetched by connID and returns the count.
// Called when a conn is removed/swapped: the server stops delivering those
// keys' invalidations, so keeping them risks stale serves. Errs toward a miss.
func ( *LocalCache) ( uint64) int {
	if  == 0 {
		return 0
	}
	 := 0
	for  := range .shards {
		 += .shards[].evictByConn()
	}
	return 
}

func ( *cacheShard) ( uint64) int {
	.mu.Lock()
	defer .mu.Unlock()

	,  := .byConnID[]
	if ! {
		return 0
	}
	 := make([]string, 0, len())
	for  := range  {
		 = append(, )
	}
	 := 0
	for ,  := range  {
		if .removeEntryLocked() {
			++
		}
	}
	return 
}

// indexConnLocked records cacheKey under connID in the owning-connection index.
func ( *cacheShard) ( uint64,  string) {
	 := .byConnID[]
	if  == nil {
		 = make(map[string]struct{})
		.byConnID[] = 
	}
	[] = struct{}{}
}

// Cancel removes the reservation matching token.
func ( *LocalCache) ( string,  uint64) bool {
	 := .shardFor()
	.mu.Lock()
	defer .mu.Unlock()

	,  := .entries[]
	if ! || .state != cacheEntryInProgress || .token !=  {
		return false
	}

	.removeEntryLocked()
	return true
}

// DeleteByRedisKey removes entries associated with redisKey. It is the applied-
// delete path used when refresh-on-invalidate is off (the collecting variant is
// used when it is on); counting deletions here keeps DeletionStats accurate on
// both paths. The two are disjoint — neither calls the other — so no double count.
func ( *LocalCache) ( string) int {
	 := 0
	for  := range .shards {
		 += .shards[].deleteByRedisKey()
	}
	.deletions.Add(1)
	if  == 0 {
		.deletionsNoop.Add(1)
	}
	return 
}

func ( *cacheShard) ( string) int {
	.mu.Lock()
	defer .mu.Unlock()

	,  := .byRedisKey[]
	if ! {
		return 0
	}

	// Remove IN_PROGRESS placeholders too: an invalidation can arrive on a
	// different stream than the in-flight reply (the background drainer), so the
	// fetch may predate the write. Removing makes the racing Fulfill fail and
	// waiters refetch, so a raced-invalidation value is never published.
	 := make([]string, 0, len())
	for  := range  {
		 = append(, )
	}

	 := 0
	for ,  := range  {
		if .removeEntryLocked() {
			++
		}
	}
	return 
}

// DeleteByCacheKey removes one entry by its internal cache key.
func ( *LocalCache) ( string) bool {
	 := .shardFor()
	.mu.Lock()
	defer .mu.Unlock()
	return .removeEntryLocked()
}

// Flush removes all entries.
func ( *LocalCache) () int {
	 := 0
	for  := range .shards {
		 += .shards[].flush()
	}
	return 
}

func ( *cacheShard) () int {
	.mu.Lock()
	defer .mu.Unlock()

	// Flush placeholders too (see deleteByRedisKey): a flush (FLUSHDB, or the
	// owned-cache flush on Close) means everything, including in-flight fetches,
	// may be stale.
	 := 0
	for  := range .entries {
		if .removeEntryLocked() {
			++
		}
	}
	return 
}

// Len returns the current number of entries and reservations.
func ( *LocalCache) () int {
	 := 0
	for  := range .shards {
		 := &.shards[]
		.mu.RLock()
		 += len(.entries)
		.mu.RUnlock()
	}
	return 
}

// MemoryUsage returns the cache's estimated memory usage in bytes.
func ( *LocalCache) () int64 {
	var  int64
	for  := range .shards {
		 := &.shards[]
		.mu.RLock()
		 += .usedBytes
		.mu.RUnlock()
	}
	return 
}

func ( *cacheShard) ( *cacheEntry) {
	if ,  := .entries[.cacheKey];  {
		.removeEntryLocked(.cacheKey)
	}

	.entries[.cacheKey] = 
	.usedBytes += .sizeBytes

	for ,  := range .redisKeys {
		 := .byRedisKey[]
		if  == nil {
			 = make(map[string]struct{})
			.byRedisKey[] = 
		}
		[.cacheKey] = struct{}{}
	}
}

func ( *cacheShard) ( string) bool {
	,  := .entries[]
	if ! {
		return false
	}

	delete(.entries, )
	.usedBytes -= .sizeBytes
	if .usedBytes < 0 {
		.usedBytes = 0
	}

	for ,  := range .redisKeys {
		 := .byRedisKey[]
		if  == nil {
			continue
		}
		delete(, )
		if len() == 0 {
			delete(.byRedisKey, )
		}
	}

	if .ownerConnID != 0 {
		if  := .byConnID[.ownerConnID];  != nil {
			delete(, )
			if len() == 0 {
				delete(.byConnID, .ownerConnID)
			}
		}
	}

	.closeWaitersLocked()
	return true
}

func ( *cacheShard) ( *cacheEntry) {
	if .waitCh != nil && !.waitClosed {
		close(.waitCh)
		.waitClosed = true
	}
}

// nearCapacityLocked reports whether this shard is close enough to its cap
// that an eviction could plausibly happen soon.
//
// The second-chance bit only ever matters as an eviction input. A shard well
// below its cap will not evict, so marking reads there is pure cost; the
// measurement is in cacheShard.get, at the call site. The threshold is
// deliberately loose (three quarters) so the bit is already being maintained
// by the time eviction actually starts choosing victims.
//
// The trade: a read made while the shard is below the threshold leaves no
// trace. When a gradually warming shard later reaches its cap, an entry read
// only during that warm-up looks as cold as one never read, so the first
// evictions go by insertion order among them and a key hot early on can take
// one avoidable miss. Recovering those reads would need the per-read store
// this gate exists to skip (9.7% of CPU under churn, measured at the call
// site); reads from the threshold on are tracked as usual.
func ( *cacheShard) () bool {
	if .maxEntries > 0 && len(.entries)*4 >= .maxEntries*3 {
		return true
	}
	if .maxMemoryBytes > 0 && .usedBytes*4 >= .maxMemoryBytes*3 {
		return true
	}
	return false
}

func ( *cacheShard) () bool {
	if .maxEntries > 0 && len(.entries) > .maxEntries {
		return true
	}
	if .maxMemoryBytes > 0 && .usedBytes > .maxMemoryBytes {
		return true
	}
	return false
}

// evictIfNeededLocked evicts by approximate LRU (O(N) scan; rare in
// well-sized caches) until under capacity. Used by Set/Fulfill: it prefers a
// Valid victim but falls back to the oldest IN_PROGRESS placeholder to keep the
// hard cap (that placeholder's Fulfill then fails and its waiters refetch).
//
// keep, when non-nil, is never chosen: it is the entry the caller just
// published and must not evict in the same pass.
func ( *cacheShard) ( *cacheEntry) {
	for .overCapacityLocked() {
		 := .oldestLocked(cacheEntryValid, )
		if  == nil {
			 = .oldestLocked(cacheEntryInProgress, )
		}
		if  == nil {
			return
		}
		.removeEntryLocked(.cacheKey)
	}
}

// evictValidLocked evicts only Valid entries until under capacity. Unlike
// evictIfNeededLocked it never evicts a placeholder, so Reserve can't abort a
// peer's in-flight fetch.
func ( *cacheShard) () {
	for .overCapacityLocked() {
		 := .oldestLocked(cacheEntryValid, nil)
		if  == nil {
			return
		}
		.removeEntryLocked(.cacheKey)
	}
}

// oldestLocked returns the eviction victim in the given state, or nil when no
// entry is in that state. keep, when non-nil, is skipped entirely: it is
// neither a victim nor part of the sweep.
//
// Second chance. A read no longer stamps a recency token (that store was the
// dominant cost of a cache hit; see cacheEntry.readSinceSweep), so recency is
// carried by the readSinceSweep bit and ordering by lastAccessNs, which is the
// token assigned when the entry was created, reserved or fulfilled.
//
// The victim is the oldest entry whose bit is CLEAR — never read since the last
// sweep. When every candidate has been read, that is the sweep: clear all their
// bits, give them a fresh chance, and fall back to the oldest by token. So a
// read protects an entry from exactly one eviction pass, which is what
// approximate LRU asks for.
//
// Ordering by lastAccessNs rather than by map order is what keeps the choice
// DETERMINISTIC. Go randomises map iteration, so picking "any entry with a
// clear bit" would evict a different key run to run, which callers (and the
// LRU tests) reasonably do not expect.
func ( *cacheShard) ( cacheEntryState,  *cacheEntry) *cacheEntry {
	var ,  *cacheEntry
	var ,  int64 = math.MaxInt64, math.MaxInt64
	 := false
	for ,  := range .entries {
		if .state !=  ||  ==  {
			continue
		}
		 := .lastAccessNs.Load()
		if  <  {
			,  = , 
		}
		if .readSinceSweep.Load() {
			 = true
			continue
		}
		if  <  {
			,  = , 
		}
	}
	if  != nil {
		return 
	}
	if  {
		// Every candidate had been read: consume their second chances so the
		// next pass can distinguish them again.
		for ,  := range .entries {
			if .state ==  &&  !=  {
				.readSinceSweep.Store(false)
			}
		}
	}
	return 
}

func cloneBytes( []byte) []byte {
	if  == nil {
		return nil
	}
	 := make([]byte, len())
	copy(, )
	return 
}

func cloneStrings( []string) []string {
	if len() == 0 {
		return nil
	}
	 := make([]string, len())
	copy(, )
	return 
}