Repository navigation
v0.6.0
This release adds NewWeighted, which bounds a cache by the total weight of its entries, such as the bytes of their values, as well as by entry count. Caches made by New behave as before, but each entry now stores a weight, so some of their writes are slightly slower (see Performance). The file format is unchanged: weights aren't saved, and v0.6.0 reads and writes the same data as v0.5.1.
// At most 1,000 entries whose values add up to at most 64 MiB.
cache, err := fastcache.NewWeighted(1000, 64<<20, func(_ string, v []byte) int {
return len(v)
})Added
NewWeighted(maxEntries, maxWeight, weigh)returns a cache that holds at mostmaxEntriesentries whose weights add up to at mostmaxWeight. You supplyweigh, afunc(K, V) int, such as one that returns the length of the value. (#71, closes #70)- When a new or heavier value doesn't fit, the oldest entries are evicted until it does. A
Setthat makes a value heavier evicts only other entries, so its key stays in the cache and keeps its place in eviction order. - Each entry keeps the weight it was stored with, so the total can't drift if data that a value points to changes later.
- To bound a cache by weight alone, pass
math.MaxIntasmaxEntries. Storage is allocated as entries are stored, not up front.
- When a new or heavier value doesn't fit, the oldest entries are evicted until it does. A
ErrInvalidWeightis returned bySet,GetOrSetandSetIfAbsentin a weighted cache when a value's weight is negative or greater thanmaxWeight. The cache is left unchanged.GetOrSetandSetIfAbsentcheck the weight only when the key is absent. (#71)ErrInvalidMaxWeightis returned byNewWeightedwhenmaxWeightisn't positive.NewWeightedalso returns an error whenweighis nil, and the same errors asNewfor an invalidmaxEntries. (#71)
Saving and loading weighted caches
- Weighted caches save like any other cache. Their weights aren't saved.
LoadFrom,LoadFromFileandLoadFromFileOrNewreturn caches bounded only by entry count. To load saved entries into a weighted cache, pass itsSettoLoadFromFunc, which stops withErrInvalidWeightat the first saved value heavier thanmaxWeight. (#71)
Performance
Each entry now stores its weight, which makes it 8 bytes larger, in caches made by New too. Measured against v0.5.1 on an Apple M5 Pro:
Get: 1.5% slower (about 0.15 ns), with no change in allocations.- Updates of
[string, []byte], the type the README benchmarks: 3% slower, with no change in memory. - Small types such as
[string, string]and[int, int]: the extra 8 bytes move each entry up an allocation size class. Updates take 8% to 10% longer and allocate 16 B more, and each stored entry takes 16 B more memory. Inserts into a full[int, int]cache take 9% longer. - Benchmark suite (
GOMAXPROCS=4): the geomean is within 0.05% of v0.5.1's (48.52 ns against 48.50 ns), and the fork is still 2.4× faster than the original fastcache and otter.Setis 3% to 8% slower from 16 to 1,024 keys,SetGet1% to 8% slower up to 2,048 keys, andSetParallel5% to 6% slower at 1 and 16 keys. From 4,096 keys up there is no measurable change.
Trade-offs
- In a weighted cache, an update that makes a value heavier locks the cache-wide FIFO queue, as adding a key does, because it may have to evict. Updates that add no weight still lock only the key's shard. (#71)
- The benchmark suite measures only caches made by
New, notNewWeighted.
Documentation
- The README lists weighted capacity among the features and in the comparison with the original fastcache, and no longer lists "No size-based limits" under Limitations. (#71)
- The README's benchmark table was rerun on this release with the same settings. Its geomean now reads 48.5 ns against 116.2 ns for otter and 118.4 ns for the original. The changed
GetParallelranges reflect run-to-run spread, not the new code. (#71)
Internal
- Each entry records its weight, and the cache keeps a running total that inserts, updates, deletes, evictions and
Resetadjust. Eviction can skip the entry being updated and return it to the front of the queue. (#71)
Full changelog: v0.5.1...v0.6.0