Repository navigation
v0.5.0
This release adds iterators that walk the cache in eviction order, and functions that select and convert entries when saving and loading. LoadFromFileOrNew now creates the cache with the capacity you pass it. The file format is unchanged: data saved by v0.4.0 loads in v0.5.0, and data saved by v0.5.0 loads in v0.4.0.
Warning
Breaking change:
LoadFromFileOrNewuses themaxEntriesyou pass, not the saved capacity. Before this release it usedmaxEntriesonly when the file failed to load, and otherwise returned a cache with the capacity it was saved with. Saving writes that capacity back, so a program that changed its cache size kept the old one on every run until the file was deleted. Now the cache always hasmaxEntriescapacity. When the file holds more entries than that, the cache keeps the newest and counts the others as evictions inStats. A missing or corrupted file still gives an empty cache. (#59, fixes #55)- A
maxEntriesthat isn't positive is now an error even when the file loads. (#59) - To keep the saved capacity, call
LoadFromFileand fall back toNew(maxEntries)when it returns an error.LoadFromFileandLoadFromstill restore the saved capacity.
Added
AllFIFO,KeysFIFOandValuesFIFOiterate oldest first, which is the order in which the cache evicts. Updating a key keeps its place; deleting a key and setting it again makes it the newest.All,KeysandValueskeep their faster, unordered iteration. (#57, closes #54)SaveToFuncsaves the entries that a function selects, as the values it returns, oldest first. Use it to save part of a cache, or to save values that gob can't encode, such as structs with unexported fields, by converting them to a type it can. The gob check applies to the returned type instead ofV. (#58, closes #56)LoadFromFuncpasses each saved entry, oldest first, to a function, such as theSetmethod of a cache you already have. It ignores the saved capacity and returns the first error that the function returns. It reads whatSaveTo,SaveToFileandSaveToFuncwrite, andLoadFromreads whatSaveToFuncwrites. (#58, closes #56)
Performance
SaveTois 4.8% faster on 500k entries, andSaveToFileConcurrentwith 8 workers is 3.9% faster, because the oldest-first sort moves smaller references. (#57)Get,Set,Delete,All,KeysandValuesrun the same code as in v0.4.0, and their benchmarks show no change.
Trade-offs
AllFIFO,KeysFIFOandValuesFIFOcopy and sort every entry before they yield the first one, and hold memory in proportion toLenuntil the loop ends. On 500k entries a full loop takes about 8× as long as withAll, and a loop that stops after 10 entries about 17,000× as long. (#57)SaveToFunckeeps the values it saves in memory until it writes them, because the format puts the entry count first. (#58)LoadFromFileOrNewwith amaxEntriessmaller than the file's entry count still decodes every entry, then evicts the extras. (#59)
Internal
- Saving and the FIFO iterators share one oldest-first snapshot. Its workers take every n-th shard instead of reading shard numbers from a channel. (#57)
- All save and load functions share one encoder and one decoder. (#58)
LoadFromno longer adds the entry's index to an error from inserting it. (#58)
Full changelog: v0.4.0...v0.5.0