Fix the following issue:
The small lib/lru package is now being used by callers that need to inspect and manage a cache without reaching into its implementation. Extend its public API while fixing the byte-accounting bug when an existing key is updated.
Add these methods to *Cache: Bytes() int64, Peek(key string) (Value, bool), Keys() []string, Remove(key string) (Value, bool), Clear(), Resize(maxBytes int64), Stats() returning a struct value with exported Hits and Misses fields of type uint64, and ResetStats().
The observable contract is:
- An entry consumes
len(key) + value.Len()bytes.Bytesreports the sum for entries currently retained. Replacing an existing key must account for both larger and smaller values, make that key most recently used, and must not itself invokeOnEvicted. - A positive byte limit is enforced after every add or replacement by evicting least-recently-used entries until the cache fits. If one entry is larger than the limit, it is added and then evicted through the normal callback path. A limit of zero continues to mean unlimited capacity.
Getkeeps its current lookup and recency behavior and increments exactly one counter:Hitsfor a found key orMissesfor an absent key.Peekreturns the same value/found pair without changing recency or either counter.Keysreturns the current keys from most to least recently used and does not change recency or counters.ResetStatszeros both counters without changing entries.Removereturns the removed value andtrue, updates byte usage, and invokesOnEvictedonce when the key exists. For a missing key it returns the zeroValueandfalsewithout a callback.Clearremoves every entry, leaves bothLen()andBytes()at zero, and invokesOnEvictedonce per entry in least-to-most-recently-used order.Resizechanges the byte limit immediately. A positive limit evicts least-recently-used entries, usingOnEvicted, until the cache fits; resizing to zero disables the limit without discarding retained entries.
Keep the existing New, Add, Get, RemoveOldest, Len, Value, and OnEvicted surfaces compatible. In particular, Get and replacement still promote an entry, RemoveOldest removes the least-recently-used entry and invokes the callback, and existing zero-limit callers remain unlimited. The cache may remain documented as unsafe for concurrent access.