peterbourgon / diskv

A disk-backed key-value store.
http://godoc.org/github.com/peterbourgon/diskv
MIT License
1.4k stars 102 forks source link

Fix issue #40 #60

Closed floren closed 4 years ago

floren commented 4 years ago

When we call ReadStream, it checks if the desired value is in the cache and if not calls readWithRLock, which returns a diskv.siphon. The siphon will write to the cache when it gets an EOF from the file on disk.

If we call ReadStream twice on the same key, we get two siphon objects, both of which will write to the cache when they finish reading. However, the cacheWithLock function does not check if the key it has been told to cache already exists in the cache. If the key already exists, cacheWithLock blithely overwrites it (d.cache[key] = val) and then improperly adds to the cache size (d.cacheSize += valueSize).

This patch makes cacheWithLock blow away any existing cached value before it writes into the cache. Benchmarks show no noticeable difference.

Fixes #40

peterbourgon commented 4 years ago

Cool! Can you add some comments to the test to describe things like why 50 and 60 bytes are important, and why two streams are necessary?

floren commented 4 years ago

PTAL

floren commented 4 years ago

ping

peterbourgon commented 4 years ago

Brilliant, thanks!