dcrd/lru
Matheus Degiovani 7f7ab16282
lru: Fix lookup race on small caches.
This fixes a possible race condition that could happen between the Add
and Lookup calls when the cache size was small.

When an entry was found in the cache during a Lookup call, its value
was fetched from the internal list node outside the mutex lock. This
could race with a corresponding Add call when the node was actually
the LRU node and was about to be overwritten.

This could manifest as a race specially when using caches with a very
small size, since it becomes more likely to perform a lookup on an entry
that is about to be concurrently overwritten.

This fixes the issue by moving the code that extracts the value from the
node to be performed with the lock held.
2020-11-09 15:32:22 -06:00
..
cache_test.go multi: Correct typos. 2019-08-22 10:20:03 -05:00
cache.go multi: Address a bunch of lint issues. 2019-12-30 13:54:41 -06:00
doc.go lru: add kv cache. 2019-11-12 00:00:06 -06:00
example_test.go lru: add kv cache. 2019-11-12 00:00:06 -06:00
go.mod lru: Implement a new module with generic LRU cache. 2019-03-18 10:08:21 -05:00
kv_test.go lru: add kv cache. 2019-11-12 00:00:06 -06:00
kv.go lru: Fix lookup race on small caches. 2020-11-09 15:32:22 -06:00
README.md multi: replace godoc.org with pkg.go.dev 2020-02-18 19:35:19 -06:00

lru

Build Status ISC License Doc

Package lru implements generic least-recently-used caches with near O(1) perf.

LRU Cache

A least-recently-used (LRU) cache is a cache that holds a limited number of items with an eviction policy such that when the capacity of the cache is exceeded, the least-recently-used item is automatically removed when inserting a new item. The meaning of used in this implementation is either accessing the item via a lookup or adding the item into the cache, including when the item already exists.

External Use

This package has intentionally been designed so it can be used as a standalone package for any projects needing to make use of well-tested and concurrent safe least-recently-used caches with near O(1) performance characteristics for lookups, inserts, and deletions.

Installation and Updating

$ go get -u github.com/decred/dcrd/lru

Examples

  • Basic Cache Usage Demonstrates creating a new cache instance, inserting items into the cache, causing an eviction of the least-recently-used item, and removing an item.

  • Basic KV Cache Usage Demonstrates creating a new k/v cache instance, inserting items into the cache, causing an eviction of the least-recently-used item, and removing an item.

License

Package lru is licensed under the copyfree ISC License.