This implements a new module named lru which provides a concurrency safe least-recently-used cache with nearly early O(1) lookups, inserts, and deletions. The cache is limited to a maximum number of items with eviction for the oldest entry when the limit is exceeded. This will allow the various concrete implementations to be updated to make use of this shared module instead of duplicating code. There is no additional performance hit to making use of this generic version because the internal list already causes the item to be boxed anyway, so the addition of the interface for the type does not cause an additional alloc. This can be seen by comparing this generic implementation to the existing concrete nonce implementation in peer: BenchmarkCache 1 allocs/op BenchmarkLruNonceList 1 allocs/op It also includes comprehensive tests, a benchmark, full package documentation, and a basic usage example.
25 lines
917 B
Go
25 lines
917 B
Go
// Copyright (c) 2019 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
/*
|
|
Package lru implements a generic least-recently-used cache 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 meaining 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 a well-test least-recently-used
|
|
cache with near O(1) performance characteristics for lookups, inserts, and
|
|
deletions.
|
|
*/
|
|
package lru
|