├── .github ├── CODEOWNERS └── workflows │ ├── ci.yml │ └── two-step-pr-approval.yml ├── .gitignore ├── .go-version ├── .golangci.yml ├── 2q.go ├── 2q_test.go ├── LICENSE ├── README.md ├── arc ├── arc.go ├── arc_test.go ├── go.mod └── go.sum ├── doc.go ├── expirable ├── expirable_lru.go └── expirable_lru_test.go ├── go.mod ├── internal └── list.go ├── lru.go ├── lru_test.go ├── simplelru ├── LICENSE_list ├── lru.go ├── lru_interface.go └── lru_test.go └── testing_test.go /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Each line is a file pattern followed by one or more owners. 2 | # More on CODEOWNERS files: https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners 3 | 4 | # Default owner 5 | * @hashicorp/team-ip-compliance @hashicorp/raft-force 6 | 7 | # Add override rules below. Each line is a file/folder pattern followed by one or more owners. 8 | # Being an owner means those groups or individuals will be added as reviewers to PRs affecting 9 | # those areas of the code. 10 | # Examples: 11 | # /docs/ @docs-team 12 | # *.js @js-team 13 | # *.go @go-team 14 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | tags: ["*"] 7 | pull_request: 8 | branches: [main] 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: set up go 1.19 16 | uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0 17 | with: 18 | go-version: 1.19 19 | id: go 20 | 21 | - name: checkout 22 | uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0 23 | 24 | - name: build, test and generate coverage report 25 | run: | 26 | go test -timeout=60s -race -v ./... -coverprofile=coverage.out 27 | go build -race ./... 28 | 29 | - name: build and test ARC 30 | working-directory: ./arc 31 | run: | 32 | go test -timeout=60s -race 33 | go build -race 34 | 35 | - name: Upload the coverage report 36 | uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 37 | with: 38 | path: coverage.out 39 | name: Coverage-report 40 | 41 | - name: Display the coverage report 42 | run: go tool cover -func=coverage.out 43 | 44 | - name: install golangci-lint 45 | run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $GITHUB_WORKSPACE v1.53.3 46 | 47 | - name: run golangci-lint 48 | run: $GITHUB_WORKSPACE/golangci-lint run --out-format=github-actions ./... ./simplelru/... ./expirable/... 49 | 50 | - name: run golangci-lint on ARC 51 | working-directory: ./arc 52 | run: $GITHUB_WORKSPACE/golangci-lint run --out-format=github-actions ./... 53 | -------------------------------------------------------------------------------- /.github/workflows/two-step-pr-approval.yml: -------------------------------------------------------------------------------- 1 | name: Two-Stage PR Review Process 2 | 3 | on: 4 | pull_request: 5 | types: [opened, synchronize, reopened, labeled, unlabeled, ready_for_review, converted_to_draft] 6 | pull_request_review: 7 | types: [submitted] 8 | 9 | jobs: 10 | manage-pr-status: 11 | runs-on: ubuntu-latest 12 | permissions: 13 | pull-requests: write 14 | contents: write 15 | steps: 16 | - name: Checkout code 17 | uses: actions/checkout@v4 18 | 19 | - name: Two stage PR review 20 | uses: hashicorp/two-stage-pr-approval@v0.1.0 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | -------------------------------------------------------------------------------- /.go-version: -------------------------------------------------------------------------------- 1 | 1.19 2 | -------------------------------------------------------------------------------- /.golangci.yml: -------------------------------------------------------------------------------- 1 | # Copyright (c) HashiCorp, Inc. 2 | # SPDX-License-Identifier: MPL-2.0 3 | 4 | linters: 5 | fast: false 6 | disable-all: true 7 | enable: 8 | - revive 9 | - megacheck 10 | - govet 11 | - unconvert 12 | - gas 13 | - gocyclo 14 | - dupl 15 | - misspell 16 | - unparam 17 | - unused 18 | - typecheck 19 | - ineffassign 20 | # - stylecheck 21 | - exportloopref 22 | - gocritic 23 | - nakedret 24 | - gosimple 25 | - prealloc 26 | 27 | # golangci-lint configuration file 28 | linters-settings: 29 | revive: 30 | ignore-generated-header: true 31 | severity: warning 32 | rules: 33 | - name: package-comments 34 | severity: warning 35 | disabled: true 36 | - name: exported 37 | severity: warning 38 | disabled: false 39 | arguments: ["checkPrivateReceivers", "disableStutteringCheck"] 40 | 41 | issues: 42 | exclude-use-default: false 43 | exclude-rules: 44 | - path: _test\.go 45 | linters: 46 | - dupl 47 | -------------------------------------------------------------------------------- /2q.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package lru 5 | 6 | import ( 7 | "errors" 8 | "sync" 9 | 10 | "github.com/hashicorp/golang-lru/v2/simplelru" 11 | ) 12 | 13 | const ( 14 | // Default2QRecentRatio is the ratio of the 2Q cache dedicated 15 | // to recently added entries that have only been accessed once. 16 | Default2QRecentRatio = 0.25 17 | 18 | // Default2QGhostEntries is the default ratio of ghost 19 | // entries kept to track entries recently evicted 20 | Default2QGhostEntries = 0.50 21 | ) 22 | 23 | // TwoQueueCache is a thread-safe fixed size 2Q cache. 24 | // 2Q is an enhancement over the standard LRU cache 25 | // in that it tracks both frequently and recently used 26 | // entries separately. This avoids a burst in access to new 27 | // entries from evicting frequently used entries. It adds some 28 | // additional tracking overhead to the standard LRU cache, and is 29 | // computationally about 2x the cost, and adds some metadata over 30 | // head. The ARCCache is similar, but does not require setting any 31 | // parameters. 32 | type TwoQueueCache[K comparable, V any] struct { 33 | size int 34 | recentSize int 35 | recentRatio float64 36 | ghostRatio float64 37 | 38 | recent simplelru.LRUCache[K, V] 39 | frequent simplelru.LRUCache[K, V] 40 | recentEvict simplelru.LRUCache[K, struct{}] 41 | lock sync.RWMutex 42 | } 43 | 44 | // New2Q creates a new TwoQueueCache using the default 45 | // values for the parameters. 46 | func New2Q[K comparable, V any](size int) (*TwoQueueCache[K, V], error) { 47 | return New2QParams[K, V](size, Default2QRecentRatio, Default2QGhostEntries) 48 | } 49 | 50 | // New2QParams creates a new TwoQueueCache using the provided 51 | // parameter values. 52 | func New2QParams[K comparable, V any](size int, recentRatio, ghostRatio float64) (*TwoQueueCache[K, V], error) { 53 | if size <= 0 { 54 | return nil, errors.New("invalid size") 55 | } 56 | if recentRatio < 0.0 || recentRatio > 1.0 { 57 | return nil, errors.New("invalid recent ratio") 58 | } 59 | if ghostRatio < 0.0 || ghostRatio > 1.0 { 60 | return nil, errors.New("invalid ghost ratio") 61 | } 62 | 63 | // Determine the sub-sizes 64 | recentSize := int(float64(size) * recentRatio) 65 | evictSize := int(float64(size) * ghostRatio) 66 | 67 | // Allocate the LRUs 68 | recent, err := simplelru.NewLRU[K, V](size, nil) 69 | if err != nil { 70 | return nil, err 71 | } 72 | frequent, err := simplelru.NewLRU[K, V](size, nil) 73 | if err != nil { 74 | return nil, err 75 | } 76 | recentEvict, err := simplelru.NewLRU[K, struct{}](evictSize, nil) 77 | if err != nil { 78 | return nil, err 79 | } 80 | 81 | // Initialize the cache 82 | c := &TwoQueueCache[K, V]{ 83 | size: size, 84 | recentSize: recentSize, 85 | recentRatio: recentRatio, 86 | ghostRatio: ghostRatio, 87 | recent: recent, 88 | frequent: frequent, 89 | recentEvict: recentEvict, 90 | } 91 | return c, nil 92 | } 93 | 94 | // Get looks up a key's value from the cache. 95 | func (c *TwoQueueCache[K, V]) Get(key K) (value V, ok bool) { 96 | c.lock.Lock() 97 | defer c.lock.Unlock() 98 | 99 | // Check if this is a frequent value 100 | if val, ok := c.frequent.Get(key); ok { 101 | return val, ok 102 | } 103 | 104 | // If the value is contained in recent, then we 105 | // promote it to frequent 106 | if val, ok := c.recent.Peek(key); ok { 107 | c.recent.Remove(key) 108 | c.frequent.Add(key, val) 109 | return val, ok 110 | } 111 | 112 | // No hit 113 | return 114 | } 115 | 116 | // Add adds a value to the cache. 117 | func (c *TwoQueueCache[K, V]) Add(key K, value V) { 118 | c.lock.Lock() 119 | defer c.lock.Unlock() 120 | 121 | // Check if the value is frequently used already, 122 | // and just update the value 123 | if c.frequent.Contains(key) { 124 | c.frequent.Add(key, value) 125 | return 126 | } 127 | 128 | // Check if the value is recently used, and promote 129 | // the value into the frequent list 130 | if c.recent.Contains(key) { 131 | c.recent.Remove(key) 132 | c.frequent.Add(key, value) 133 | return 134 | } 135 | 136 | // If the value was recently evicted, add it to the 137 | // frequently used list 138 | if c.recentEvict.Contains(key) { 139 | c.ensureSpace(true) 140 | c.recentEvict.Remove(key) 141 | c.frequent.Add(key, value) 142 | return 143 | } 144 | 145 | // Add to the recently seen list 146 | c.ensureSpace(false) 147 | c.recent.Add(key, value) 148 | } 149 | 150 | // ensureSpace is used to ensure we have space in the cache 151 | func (c *TwoQueueCache[K, V]) ensureSpace(recentEvict bool) { 152 | // If we have space, nothing to do 153 | recentLen := c.recent.Len() 154 | freqLen := c.frequent.Len() 155 | if recentLen+freqLen < c.size { 156 | return 157 | } 158 | 159 | // If the recent buffer is larger than 160 | // the target, evict from there 161 | if recentLen > 0 && (recentLen > c.recentSize || (recentLen == c.recentSize && !recentEvict)) { 162 | k, _, _ := c.recent.RemoveOldest() 163 | c.recentEvict.Add(k, struct{}{}) 164 | return 165 | } 166 | 167 | // Remove from the frequent list otherwise 168 | c.frequent.RemoveOldest() 169 | } 170 | 171 | // Len returns the number of items in the cache. 172 | func (c *TwoQueueCache[K, V]) Len() int { 173 | c.lock.RLock() 174 | defer c.lock.RUnlock() 175 | return c.recent.Len() + c.frequent.Len() 176 | } 177 | 178 | // Cap returns the capacity of the cache 179 | func (c *TwoQueueCache[K, V]) Cap() int { 180 | return c.size 181 | } 182 | 183 | // Resize changes the cache size. 184 | func (c *TwoQueueCache[K, V]) Resize(size int) (evicted int) { 185 | c.lock.Lock() 186 | defer c.lock.Unlock() 187 | 188 | // Recalculate the sub-sizes 189 | recentSize := int(float64(size) * c.recentRatio) 190 | evictSize := int(float64(size) * c.ghostRatio) 191 | c.size = size 192 | c.recentSize = recentSize 193 | 194 | // ensureSpace 195 | diff := c.recent.Len() + c.frequent.Len() - size 196 | if diff < 0 { 197 | diff = 0 198 | } 199 | for i := 0; i < diff; i++ { 200 | c.ensureSpace(true) 201 | } 202 | 203 | // Reallocate the LRUs 204 | c.recent.Resize(size) 205 | c.frequent.Resize(size) 206 | c.recentEvict.Resize(evictSize) 207 | 208 | return diff 209 | } 210 | 211 | // Keys returns a slice of the keys in the cache. 212 | // The frequently used keys are first in the returned slice. 213 | func (c *TwoQueueCache[K, V]) Keys() []K { 214 | c.lock.RLock() 215 | defer c.lock.RUnlock() 216 | k1 := c.frequent.Keys() 217 | k2 := c.recent.Keys() 218 | return append(k1, k2...) 219 | } 220 | 221 | // Values returns a slice of the values in the cache. 222 | // The frequently used values are first in the returned slice. 223 | func (c *TwoQueueCache[K, V]) Values() []V { 224 | c.lock.RLock() 225 | defer c.lock.RUnlock() 226 | v1 := c.frequent.Values() 227 | v2 := c.recent.Values() 228 | return append(v1, v2...) 229 | } 230 | 231 | // Remove removes the provided key from the cache. 232 | func (c *TwoQueueCache[K, V]) Remove(key K) { 233 | c.lock.Lock() 234 | defer c.lock.Unlock() 235 | if c.frequent.Remove(key) { 236 | return 237 | } 238 | if c.recent.Remove(key) { 239 | return 240 | } 241 | if c.recentEvict.Remove(key) { 242 | return 243 | } 244 | } 245 | 246 | // Purge is used to completely clear the cache. 247 | func (c *TwoQueueCache[K, V]) Purge() { 248 | c.lock.Lock() 249 | defer c.lock.Unlock() 250 | c.recent.Purge() 251 | c.frequent.Purge() 252 | c.recentEvict.Purge() 253 | } 254 | 255 | // Contains is used to check if the cache contains a key 256 | // without updating recency or frequency. 257 | func (c *TwoQueueCache[K, V]) Contains(key K) bool { 258 | c.lock.RLock() 259 | defer c.lock.RUnlock() 260 | return c.frequent.Contains(key) || c.recent.Contains(key) 261 | } 262 | 263 | // Peek is used to inspect the cache value of a key 264 | // without updating recency or frequency. 265 | func (c *TwoQueueCache[K, V]) Peek(key K) (value V, ok bool) { 266 | c.lock.RLock() 267 | defer c.lock.RUnlock() 268 | if val, ok := c.frequent.Peek(key); ok { 269 | return val, ok 270 | } 271 | return c.recent.Peek(key) 272 | } 273 | -------------------------------------------------------------------------------- /2q_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package lru 5 | 6 | import ( 7 | "testing" 8 | ) 9 | 10 | func Benchmark2Q_Rand(b *testing.B) { 11 | l, err := New2Q[int64, int64](8192) 12 | if err != nil { 13 | b.Fatalf("err: %v", err) 14 | } 15 | 16 | trace := make([]int64, b.N*2) 17 | for i := 0; i < b.N*2; i++ { 18 | trace[i] = getRand(b) % 32768 19 | } 20 | 21 | b.ResetTimer() 22 | 23 | var hit, miss int 24 | for i := 0; i < 2*b.N; i++ { 25 | if i%2 == 0 { 26 | l.Add(trace[i], trace[i]) 27 | } else { 28 | if _, ok := l.Get(trace[i]); ok { 29 | hit++ 30 | } else { 31 | miss++ 32 | } 33 | } 34 | } 35 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss)) 36 | } 37 | 38 | func Benchmark2Q_Freq(b *testing.B) { 39 | l, err := New2Q[int64, int64](8192) 40 | if err != nil { 41 | b.Fatalf("err: %v", err) 42 | } 43 | 44 | trace := make([]int64, b.N*2) 45 | for i := 0; i < b.N*2; i++ { 46 | if i%2 == 0 { 47 | trace[i] = getRand(b) % 16384 48 | } else { 49 | trace[i] = getRand(b) % 32768 50 | } 51 | } 52 | 53 | b.ResetTimer() 54 | 55 | for i := 0; i < b.N; i++ { 56 | l.Add(trace[i], trace[i]) 57 | } 58 | var hit, miss int 59 | for i := 0; i < b.N; i++ { 60 | if _, ok := l.Get(trace[i]); ok { 61 | hit++ 62 | } else { 63 | miss++ 64 | } 65 | } 66 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss)) 67 | } 68 | 69 | func Test2Q_RandomOps(t *testing.T) { 70 | size := 128 71 | l, err := New2Q[int64, int64](128) 72 | if err != nil { 73 | t.Fatalf("err: %v", err) 74 | } 75 | 76 | n := 200000 77 | for i := 0; i < n; i++ { 78 | key := getRand(t) % 512 79 | r := getRand(t) 80 | switch r % 3 { 81 | case 0: 82 | l.Add(key, key) 83 | case 1: 84 | l.Get(key) 85 | case 2: 86 | l.Remove(key) 87 | } 88 | 89 | if l.recent.Len()+l.frequent.Len() > size { 90 | t.Fatalf("bad: recent: %d freq: %d", 91 | l.recent.Len(), l.frequent.Len()) 92 | } 93 | } 94 | } 95 | 96 | func Test2Q_Get_RecentToFrequent(t *testing.T) { 97 | l, err := New2Q[int, int](128) 98 | if err != nil { 99 | t.Fatalf("err: %v", err) 100 | } 101 | 102 | // Touch all the entries, should be in t1 103 | for i := 0; i < 128; i++ { 104 | l.Add(i, i) 105 | } 106 | if n := l.recent.Len(); n != 128 { 107 | t.Fatalf("bad: %d", n) 108 | } 109 | if n := l.frequent.Len(); n != 0 { 110 | t.Fatalf("bad: %d", n) 111 | } 112 | 113 | // Get should upgrade to t2 114 | for i := 0; i < 128; i++ { 115 | if _, ok := l.Get(i); !ok { 116 | t.Fatalf("missing: %d", i) 117 | } 118 | } 119 | if n := l.recent.Len(); n != 0 { 120 | t.Fatalf("bad: %d", n) 121 | } 122 | if n := l.frequent.Len(); n != 128 { 123 | t.Fatalf("bad: %d", n) 124 | } 125 | 126 | // Get be from t2 127 | for i := 0; i < 128; i++ { 128 | if _, ok := l.Get(i); !ok { 129 | t.Fatalf("missing: %d", i) 130 | } 131 | } 132 | if n := l.recent.Len(); n != 0 { 133 | t.Fatalf("bad: %d", n) 134 | } 135 | if n := l.frequent.Len(); n != 128 { 136 | t.Fatalf("bad: %d", n) 137 | } 138 | } 139 | 140 | func Test2Q_Add_RecentToFrequent(t *testing.T) { 141 | l, err := New2Q[int, int](128) 142 | if err != nil { 143 | t.Fatalf("err: %v", err) 144 | } 145 | 146 | // Add initially to recent 147 | l.Add(1, 1) 148 | if n := l.recent.Len(); n != 1 { 149 | t.Fatalf("bad: %d", n) 150 | } 151 | if n := l.frequent.Len(); n != 0 { 152 | t.Fatalf("bad: %d", n) 153 | } 154 | 155 | // Add should upgrade to frequent 156 | l.Add(1, 1) 157 | if n := l.recent.Len(); n != 0 { 158 | t.Fatalf("bad: %d", n) 159 | } 160 | if n := l.frequent.Len(); n != 1 { 161 | t.Fatalf("bad: %d", n) 162 | } 163 | 164 | // Add should remain in frequent 165 | l.Add(1, 1) 166 | if n := l.recent.Len(); n != 0 { 167 | t.Fatalf("bad: %d", n) 168 | } 169 | if n := l.frequent.Len(); n != 1 { 170 | t.Fatalf("bad: %d", n) 171 | } 172 | } 173 | 174 | func Test2Q_Add_RecentEvict(t *testing.T) { 175 | l, err := New2Q[int, int](4) 176 | if err != nil { 177 | t.Fatalf("err: %v", err) 178 | } 179 | 180 | // Add 1,2,3,4,5 -> Evict 1 181 | l.Add(1, 1) 182 | l.Add(2, 2) 183 | l.Add(3, 3) 184 | l.Add(4, 4) 185 | l.Add(5, 5) 186 | if n := l.recent.Len(); n != 4 { 187 | t.Fatalf("bad: %d", n) 188 | } 189 | if n := l.recentEvict.Len(); n != 1 { 190 | t.Fatalf("bad: %d", n) 191 | } 192 | if n := l.frequent.Len(); n != 0 { 193 | t.Fatalf("bad: %d", n) 194 | } 195 | 196 | // Pull in the recently evicted 197 | l.Add(1, 1) 198 | if n := l.recent.Len(); n != 3 { 199 | t.Fatalf("bad: %d", n) 200 | } 201 | if n := l.recentEvict.Len(); n != 1 { 202 | t.Fatalf("bad: %d", n) 203 | } 204 | if n := l.frequent.Len(); n != 1 { 205 | t.Fatalf("bad: %d", n) 206 | } 207 | 208 | // Add 6, should cause another recent evict 209 | l.Add(6, 6) 210 | if n := l.recent.Len(); n != 3 { 211 | t.Fatalf("bad: %d", n) 212 | } 213 | if n := l.recentEvict.Len(); n != 2 { 214 | t.Fatalf("bad: %d", n) 215 | } 216 | if n := l.frequent.Len(); n != 1 { 217 | t.Fatalf("bad: %d", n) 218 | } 219 | } 220 | 221 | func Test2Q_Resize(t *testing.T) { 222 | l, err := New2Q[int, int](100) 223 | if err != nil { 224 | t.Fatalf("err: %v", err) 225 | } 226 | 227 | // Touch all the entries, should be in t1 228 | for i := 0; i < 100; i++ { 229 | l.Add(i, i) 230 | } 231 | 232 | evicted := l.Resize(50) 233 | if evicted != 50 { 234 | t.Fatalf("bad: %d", evicted) 235 | } 236 | 237 | if n := l.recent.Len(); n != 50 { 238 | t.Fatalf("bad: %d", n) 239 | } 240 | if n := l.frequent.Len(); n != 0 { 241 | t.Fatalf("bad: %d", n) 242 | } 243 | 244 | l, err = New2Q[int, int](100) 245 | if err != nil { 246 | t.Fatalf("err: %v", err) 247 | } 248 | for i := 0; i < 100; i++ { 249 | l.Add(i, i) 250 | } 251 | 252 | for i := 0; i < 50; i++ { 253 | l.Add(i, i) 254 | } 255 | 256 | evicted = l.Resize(50) 257 | if evicted != 50 { 258 | t.Fatalf("bad: %d", evicted) 259 | } 260 | 261 | if n := l.recent.Len(); n != 12 { 262 | t.Fatalf("bad: %d", n) 263 | } 264 | if n := l.frequent.Len(); n != 38 { 265 | t.Fatalf("bad: %d", n) 266 | } 267 | 268 | l, err = New2Q[int, int](100) 269 | if err != nil { 270 | t.Fatalf("err: %v", err) 271 | } 272 | for i := 0; i < 100; i++ { 273 | l.Add(i, i) 274 | l.Add(i, i) 275 | } 276 | 277 | evicted = l.Resize(50) 278 | if evicted != 50 { 279 | t.Fatalf("bad: %d", evicted) 280 | } 281 | 282 | if n := l.recent.Len(); n != 0 { 283 | t.Fatalf("bad: %d", n) 284 | } 285 | if n := l.frequent.Len(); n != 50 { 286 | t.Fatalf("bad: %d", n) 287 | } 288 | } 289 | 290 | func Test2Q(t *testing.T) { 291 | l, err := New2Q[int, int](128) 292 | if err != nil { 293 | t.Fatalf("err: %v", err) 294 | } 295 | 296 | for i := 0; i < 256; i++ { 297 | l.Add(i, i) 298 | } 299 | if l.Len() != 128 { 300 | t.Fatalf("bad len: %v", l.Len()) 301 | } 302 | if l.Cap() != 128 { 303 | t.Fatalf("expect %d, but %d", 128, l.Cap()) 304 | } 305 | 306 | for i, k := range l.Keys() { 307 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 308 | t.Fatalf("bad key: %v", k) 309 | } 310 | } 311 | for i, v := range l.Values() { 312 | if v != i+128 { 313 | t.Fatalf("bad key: %v", v) 314 | } 315 | } 316 | for i := 0; i < 128; i++ { 317 | if _, ok := l.Get(i); ok { 318 | t.Fatalf("should be evicted") 319 | } 320 | } 321 | for i := 128; i < 256; i++ { 322 | if _, ok := l.Get(i); !ok { 323 | t.Fatalf("should not be evicted") 324 | } 325 | } 326 | for i := 128; i < 192; i++ { 327 | l.Remove(i) 328 | if _, ok := l.Get(i); ok { 329 | t.Fatalf("should be deleted") 330 | } 331 | } 332 | 333 | l.Purge() 334 | if l.Len() != 0 { 335 | t.Fatalf("bad len: %v", l.Len()) 336 | } 337 | if _, ok := l.Get(200); ok { 338 | t.Fatalf("should contain nothing") 339 | } 340 | } 341 | 342 | // Test that Contains doesn't update recent-ness 343 | func Test2Q_Contains(t *testing.T) { 344 | l, err := New2Q[int, int](2) 345 | if err != nil { 346 | t.Fatalf("err: %v", err) 347 | } 348 | 349 | l.Add(1, 1) 350 | l.Add(2, 2) 351 | if !l.Contains(1) { 352 | t.Errorf("1 should be contained") 353 | } 354 | 355 | l.Add(3, 3) 356 | if l.Contains(1) { 357 | t.Errorf("Contains should not have updated recent-ness of 1") 358 | } 359 | } 360 | 361 | // Test that Peek doesn't update recent-ness 362 | func Test2Q_Peek(t *testing.T) { 363 | l, err := New2Q[int, int](2) 364 | if err != nil { 365 | t.Fatalf("err: %v", err) 366 | } 367 | 368 | l.Add(1, 1) 369 | l.Add(2, 2) 370 | if v, ok := l.Peek(1); !ok || v != 1 { 371 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 372 | } 373 | 374 | l.Add(3, 3) 375 | if l.Contains(1) { 376 | t.Errorf("should not have updated recent-ness of 1") 377 | } 378 | } 379 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 HashiCorp, Inc. 2 | 3 | Mozilla Public License, version 2.0 4 | 5 | 1. Definitions 6 | 7 | 1.1. "Contributor" 8 | 9 | means each individual or legal entity that creates, contributes to the 10 | creation of, or owns Covered Software. 11 | 12 | 1.2. "Contributor Version" 13 | 14 | means the combination of the Contributions of others (if any) used by a 15 | Contributor and that particular Contributor's Contribution. 16 | 17 | 1.3. "Contribution" 18 | 19 | means Covered Software of a particular Contributor. 20 | 21 | 1.4. "Covered Software" 22 | 23 | means Source Code Form to which the initial Contributor has attached the 24 | notice in Exhibit A, the Executable Form of such Source Code Form, and 25 | Modifications of such Source Code Form, in each case including portions 26 | thereof. 27 | 28 | 1.5. "Incompatible With Secondary Licenses" 29 | means 30 | 31 | a. that the initial Contributor has attached the notice described in 32 | Exhibit B to the Covered Software; or 33 | 34 | b. that the Covered Software was made available under the terms of 35 | version 1.1 or earlier of the License, but not also under the terms of 36 | a Secondary License. 37 | 38 | 1.6. "Executable Form" 39 | 40 | means any form of the work other than Source Code Form. 41 | 42 | 1.7. "Larger Work" 43 | 44 | means a work that combines Covered Software with other material, in a 45 | separate file or files, that is not Covered Software. 46 | 47 | 1.8. "License" 48 | 49 | means this document. 50 | 51 | 1.9. "Licensable" 52 | 53 | means having the right to grant, to the maximum extent possible, whether 54 | at the time of the initial grant or subsequently, any and all of the 55 | rights conveyed by this License. 56 | 57 | 1.10. "Modifications" 58 | 59 | means any of the following: 60 | 61 | a. any file in Source Code Form that results from an addition to, 62 | deletion from, or modification of the contents of Covered Software; or 63 | 64 | b. any new file in Source Code Form that contains any Covered Software. 65 | 66 | 1.11. "Patent Claims" of a Contributor 67 | 68 | means any patent claim(s), including without limitation, method, 69 | process, and apparatus claims, in any patent Licensable by such 70 | Contributor that would be infringed, but for the grant of the License, 71 | by the making, using, selling, offering for sale, having made, import, 72 | or transfer of either its Contributions or its Contributor Version. 73 | 74 | 1.12. "Secondary License" 75 | 76 | means either the GNU General Public License, Version 2.0, the GNU Lesser 77 | General Public License, Version 2.1, the GNU Affero General Public 78 | License, Version 3.0, or any later versions of those licenses. 79 | 80 | 1.13. "Source Code Form" 81 | 82 | means the form of the work preferred for making modifications. 83 | 84 | 1.14. "You" (or "Your") 85 | 86 | means an individual or a legal entity exercising rights under this 87 | License. For legal entities, "You" includes any entity that controls, is 88 | controlled by, or is under common control with You. For purposes of this 89 | definition, "control" means (a) the power, direct or indirect, to cause 90 | the direction or management of such entity, whether by contract or 91 | otherwise, or (b) ownership of more than fifty percent (50%) of the 92 | outstanding shares or beneficial ownership of such entity. 93 | 94 | 95 | 2. License Grants and Conditions 96 | 97 | 2.1. Grants 98 | 99 | Each Contributor hereby grants You a world-wide, royalty-free, 100 | non-exclusive license: 101 | 102 | a. under intellectual property rights (other than patent or trademark) 103 | Licensable by such Contributor to use, reproduce, make available, 104 | modify, display, perform, distribute, and otherwise exploit its 105 | Contributions, either on an unmodified basis, with Modifications, or 106 | as part of a Larger Work; and 107 | 108 | b. under Patent Claims of such Contributor to make, use, sell, offer for 109 | sale, have made, import, and otherwise transfer either its 110 | Contributions or its Contributor Version. 111 | 112 | 2.2. Effective Date 113 | 114 | The licenses granted in Section 2.1 with respect to any Contribution 115 | become effective for each Contribution on the date the Contributor first 116 | distributes such Contribution. 117 | 118 | 2.3. Limitations on Grant Scope 119 | 120 | The licenses granted in this Section 2 are the only rights granted under 121 | this License. No additional rights or licenses will be implied from the 122 | distribution or licensing of Covered Software under this License. 123 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 124 | Contributor: 125 | 126 | a. for any code that a Contributor has removed from Covered Software; or 127 | 128 | b. for infringements caused by: (i) Your and any other third party's 129 | modifications of Covered Software, or (ii) the combination of its 130 | Contributions with other software (except as part of its Contributor 131 | Version); or 132 | 133 | c. under Patent Claims infringed by Covered Software in the absence of 134 | its Contributions. 135 | 136 | This License does not grant any rights in the trademarks, service marks, 137 | or logos of any Contributor (except as may be necessary to comply with 138 | the notice requirements in Section 3.4). 139 | 140 | 2.4. Subsequent Licenses 141 | 142 | No Contributor makes additional grants as a result of Your choice to 143 | distribute the Covered Software under a subsequent version of this 144 | License (see Section 10.2) or under the terms of a Secondary License (if 145 | permitted under the terms of Section 3.3). 146 | 147 | 2.5. Representation 148 | 149 | Each Contributor represents that the Contributor believes its 150 | Contributions are its original creation(s) or it has sufficient rights to 151 | grant the rights to its Contributions conveyed by this License. 152 | 153 | 2.6. Fair Use 154 | 155 | This License is not intended to limit any rights You have under 156 | applicable copyright doctrines of fair use, fair dealing, or other 157 | equivalents. 158 | 159 | 2.7. Conditions 160 | 161 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in 162 | Section 2.1. 163 | 164 | 165 | 3. Responsibilities 166 | 167 | 3.1. Distribution of Source Form 168 | 169 | All distribution of Covered Software in Source Code Form, including any 170 | Modifications that You create or to which You contribute, must be under 171 | the terms of this License. You must inform recipients that the Source 172 | Code Form of the Covered Software is governed by the terms of this 173 | License, and how they can obtain a copy of this License. You may not 174 | attempt to alter or restrict the recipients' rights in the Source Code 175 | Form. 176 | 177 | 3.2. Distribution of Executable Form 178 | 179 | If You distribute Covered Software in Executable Form then: 180 | 181 | a. such Covered Software must also be made available in Source Code Form, 182 | as described in Section 3.1, and You must inform recipients of the 183 | Executable Form how they can obtain a copy of such Source Code Form by 184 | reasonable means in a timely manner, at a charge no more than the cost 185 | of distribution to the recipient; and 186 | 187 | b. You may distribute such Executable Form under the terms of this 188 | License, or sublicense it under different terms, provided that the 189 | license for the Executable Form does not attempt to limit or alter the 190 | recipients' rights in the Source Code Form under this License. 191 | 192 | 3.3. Distribution of a Larger Work 193 | 194 | You may create and distribute a Larger Work under terms of Your choice, 195 | provided that You also comply with the requirements of this License for 196 | the Covered Software. If the Larger Work is a combination of Covered 197 | Software with a work governed by one or more Secondary Licenses, and the 198 | Covered Software is not Incompatible With Secondary Licenses, this 199 | License permits You to additionally distribute such Covered Software 200 | under the terms of such Secondary License(s), so that the recipient of 201 | the Larger Work may, at their option, further distribute the Covered 202 | Software under the terms of either this License or such Secondary 203 | License(s). 204 | 205 | 3.4. Notices 206 | 207 | You may not remove or alter the substance of any license notices 208 | (including copyright notices, patent notices, disclaimers of warranty, or 209 | limitations of liability) contained within the Source Code Form of the 210 | Covered Software, except that You may alter any license notices to the 211 | extent required to remedy known factual inaccuracies. 212 | 213 | 3.5. Application of Additional Terms 214 | 215 | You may choose to offer, and to charge a fee for, warranty, support, 216 | indemnity or liability obligations to one or more recipients of Covered 217 | Software. However, You may do so only on Your own behalf, and not on 218 | behalf of any Contributor. You must make it absolutely clear that any 219 | such warranty, support, indemnity, or liability obligation is offered by 220 | You alone, and You hereby agree to indemnify every Contributor for any 221 | liability incurred by such Contributor as a result of warranty, support, 222 | indemnity or liability terms You offer. You may include additional 223 | disclaimers of warranty and limitations of liability specific to any 224 | jurisdiction. 225 | 226 | 4. Inability to Comply Due to Statute or Regulation 227 | 228 | If it is impossible for You to comply with any of the terms of this License 229 | with respect to some or all of the Covered Software due to statute, 230 | judicial order, or regulation then You must: (a) comply with the terms of 231 | this License to the maximum extent possible; and (b) describe the 232 | limitations and the code they affect. Such description must be placed in a 233 | text file included with all distributions of the Covered Software under 234 | this License. Except to the extent prohibited by statute or regulation, 235 | such description must be sufficiently detailed for a recipient of ordinary 236 | skill to be able to understand it. 237 | 238 | 5. Termination 239 | 240 | 5.1. The rights granted under this License will terminate automatically if You 241 | fail to comply with any of its terms. However, if You become compliant, 242 | then the rights granted under this License from a particular Contributor 243 | are reinstated (a) provisionally, unless and until such Contributor 244 | explicitly and finally terminates Your grants, and (b) on an ongoing 245 | basis, if such Contributor fails to notify You of the non-compliance by 246 | some reasonable means prior to 60 days after You have come back into 247 | compliance. Moreover, Your grants from a particular Contributor are 248 | reinstated on an ongoing basis if such Contributor notifies You of the 249 | non-compliance by some reasonable means, this is the first time You have 250 | received notice of non-compliance with this License from such 251 | Contributor, and You become compliant prior to 30 days after Your receipt 252 | of the notice. 253 | 254 | 5.2. If You initiate litigation against any entity by asserting a patent 255 | infringement claim (excluding declaratory judgment actions, 256 | counter-claims, and cross-claims) alleging that a Contributor Version 257 | directly or indirectly infringes any patent, then the rights granted to 258 | You by any and all Contributors for the Covered Software under Section 259 | 2.1 of this License shall terminate. 260 | 261 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user 262 | license agreements (excluding distributors and resellers) which have been 263 | validly granted by You or Your distributors under this License prior to 264 | termination shall survive termination. 265 | 266 | 6. Disclaimer of Warranty 267 | 268 | Covered Software is provided under this License on an "as is" basis, 269 | without warranty of any kind, either expressed, implied, or statutory, 270 | including, without limitation, warranties that the Covered Software is free 271 | of defects, merchantable, fit for a particular purpose or non-infringing. 272 | The entire risk as to the quality and performance of the Covered Software 273 | is with You. Should any Covered Software prove defective in any respect, 274 | You (not any Contributor) assume the cost of any necessary servicing, 275 | repair, or correction. This disclaimer of warranty constitutes an essential 276 | part of this License. No use of any Covered Software is authorized under 277 | this License except under this disclaimer. 278 | 279 | 7. Limitation of Liability 280 | 281 | Under no circumstances and under no legal theory, whether tort (including 282 | negligence), contract, or otherwise, shall any Contributor, or anyone who 283 | distributes Covered Software as permitted above, be liable to You for any 284 | direct, indirect, special, incidental, or consequential damages of any 285 | character including, without limitation, damages for lost profits, loss of 286 | goodwill, work stoppage, computer failure or malfunction, or any and all 287 | other commercial damages or losses, even if such party shall have been 288 | informed of the possibility of such damages. This limitation of liability 289 | shall not apply to liability for death or personal injury resulting from 290 | such party's negligence to the extent applicable law prohibits such 291 | limitation. Some jurisdictions do not allow the exclusion or limitation of 292 | incidental or consequential damages, so this exclusion and limitation may 293 | not apply to You. 294 | 295 | 8. Litigation 296 | 297 | Any litigation relating to this License may be brought only in the courts 298 | of a jurisdiction where the defendant maintains its principal place of 299 | business and such litigation shall be governed by laws of that 300 | jurisdiction, without reference to its conflict-of-law provisions. Nothing 301 | in this Section shall prevent a party's ability to bring cross-claims or 302 | counter-claims. 303 | 304 | 9. Miscellaneous 305 | 306 | This License represents the complete agreement concerning the subject 307 | matter hereof. If any provision of this License is held to be 308 | unenforceable, such provision shall be reformed only to the extent 309 | necessary to make it enforceable. Any law or regulation which provides that 310 | the language of a contract shall be construed against the drafter shall not 311 | be used to construe this License against a Contributor. 312 | 313 | 314 | 10. Versions of the License 315 | 316 | 10.1. New Versions 317 | 318 | Mozilla Foundation is the license steward. Except as provided in Section 319 | 10.3, no one other than the license steward has the right to modify or 320 | publish new versions of this License. Each version will be given a 321 | distinguishing version number. 322 | 323 | 10.2. Effect of New Versions 324 | 325 | You may distribute the Covered Software under the terms of the version 326 | of the License under which You originally received the Covered Software, 327 | or under the terms of any subsequent version published by the license 328 | steward. 329 | 330 | 10.3. Modified Versions 331 | 332 | If you create software not governed by this License, and you want to 333 | create a new license for such software, you may create and use a 334 | modified version of this License if you rename the license and remove 335 | any references to the name of the license steward (except to note that 336 | such modified license differs from this License). 337 | 338 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 339 | Licenses If You choose to distribute Source Code Form that is 340 | Incompatible With Secondary Licenses under the terms of this version of 341 | the License, the notice described in Exhibit B of this License must be 342 | attached. 343 | 344 | Exhibit A - Source Code Form License Notice 345 | 346 | This Source Code Form is subject to the 347 | terms of the Mozilla Public License, v. 348 | 2.0. If a copy of the MPL was not 349 | distributed with this file, You can 350 | obtain one at 351 | http://mozilla.org/MPL/2.0/. 352 | 353 | If it is not possible or desirable to put the notice in a particular file, 354 | then You may include the notice in a location (such as a LICENSE file in a 355 | relevant directory) where a recipient would be likely to look for such a 356 | notice. 357 | 358 | You may add additional accurate notices of copyright ownership. 359 | 360 | Exhibit B - "Incompatible With Secondary Licenses" Notice 361 | 362 | This Source Code Form is "Incompatible 363 | With Secondary Licenses", as defined by 364 | the Mozilla Public License, v. 2.0. 365 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | golang-lru 2 | ========== 3 | 4 | This provides the `lru` package which implements a fixed-size 5 | thread safe LRU cache. It is based on the cache in Groupcache. 6 | 7 | Documentation 8 | ============= 9 | 10 | Full docs are available on [Go Packages](https://pkg.go.dev/github.com/hashicorp/golang-lru/v2) 11 | 12 | LRU cache example 13 | ================= 14 | 15 | ```go 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | "github.com/hashicorp/golang-lru/v2" 21 | ) 22 | 23 | func main() { 24 | l, _ := lru.New[int, any](128) 25 | for i := 0; i < 256; i++ { 26 | l.Add(i, nil) 27 | } 28 | if l.Len() != 128 { 29 | panic(fmt.Sprintf("bad len: %v", l.Len())) 30 | } 31 | } 32 | ``` 33 | 34 | Expirable LRU cache example 35 | =========================== 36 | 37 | ```go 38 | package main 39 | 40 | import ( 41 | "fmt" 42 | "time" 43 | 44 | "github.com/hashicorp/golang-lru/v2/expirable" 45 | ) 46 | 47 | func main() { 48 | // make cache with 10ms TTL and 5 max keys 49 | cache := expirable.NewLRU[string, string](5, nil, time.Millisecond*10) 50 | 51 | 52 | // set value under key1. 53 | cache.Add("key1", "val1") 54 | 55 | // get value under key1 56 | r, ok := cache.Get("key1") 57 | 58 | // check for OK value 59 | if ok { 60 | fmt.Printf("value before expiration is found: %v, value: %q\n", ok, r) 61 | } 62 | 63 | // wait for cache to expire 64 | time.Sleep(time.Millisecond * 12) 65 | 66 | // get value under key1 after key expiration 67 | r, ok = cache.Get("key1") 68 | fmt.Printf("value after expiration is found: %v, value: %q\n", ok, r) 69 | 70 | // set value under key2, would evict old entry because it is already expired. 71 | cache.Add("key2", "val2") 72 | 73 | fmt.Printf("Cache len: %d\n", cache.Len()) 74 | // Output: 75 | // value before expiration is found: true, value: "val1" 76 | // value after expiration is found: false, value: "" 77 | // Cache len: 1 78 | } 79 | ``` 80 | -------------------------------------------------------------------------------- /arc/arc.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package arc 5 | 6 | import ( 7 | "sync" 8 | 9 | "github.com/hashicorp/golang-lru/v2/simplelru" 10 | ) 11 | 12 | // ARCCache is a thread-safe fixed size Adaptive Replacement Cache (ARC). 13 | // ARC is an enhancement over the standard LRU cache in that tracks both 14 | // frequency and recency of use. This avoids a burst in access to new 15 | // entries from evicting the frequently used older entries. It adds some 16 | // additional tracking overhead to a standard LRU cache, computationally 17 | // it is roughly 2x the cost, and the extra memory overhead is linear 18 | // with the size of the cache. ARC has been patented by IBM, but is 19 | // similar to the TwoQueueCache (2Q) which requires setting parameters. 20 | type ARCCache[K comparable, V any] struct { 21 | size int // Size is the total capacity of the cache 22 | p int // P is the dynamic preference towards T1 or T2 23 | 24 | t1 simplelru.LRUCache[K, V] // T1 is the LRU for recently accessed items 25 | b1 simplelru.LRUCache[K, struct{}] // B1 is the LRU for evictions from t1 26 | 27 | t2 simplelru.LRUCache[K, V] // T2 is the LRU for frequently accessed items 28 | b2 simplelru.LRUCache[K, struct{}] // B2 is the LRU for evictions from t2 29 | 30 | lock sync.RWMutex 31 | } 32 | 33 | // NewARC creates an ARC of the given size 34 | func NewARC[K comparable, V any](size int) (*ARCCache[K, V], error) { 35 | // Create the sub LRUs 36 | b1, err := simplelru.NewLRU[K, struct{}](size, nil) 37 | if err != nil { 38 | return nil, err 39 | } 40 | b2, err := simplelru.NewLRU[K, struct{}](size, nil) 41 | if err != nil { 42 | return nil, err 43 | } 44 | t1, err := simplelru.NewLRU[K, V](size, nil) 45 | if err != nil { 46 | return nil, err 47 | } 48 | t2, err := simplelru.NewLRU[K, V](size, nil) 49 | if err != nil { 50 | return nil, err 51 | } 52 | 53 | // Initialize the ARC 54 | c := &ARCCache[K, V]{ 55 | size: size, 56 | p: 0, 57 | t1: t1, 58 | b1: b1, 59 | t2: t2, 60 | b2: b2, 61 | } 62 | return c, nil 63 | } 64 | 65 | // Get looks up a key's value from the cache. 66 | func (c *ARCCache[K, V]) Get(key K) (value V, ok bool) { 67 | c.lock.Lock() 68 | defer c.lock.Unlock() 69 | 70 | // If the value is contained in T1 (recent), then 71 | // promote it to T2 (frequent) 72 | if val, ok := c.t1.Peek(key); ok { 73 | c.t1.Remove(key) 74 | c.t2.Add(key, val) 75 | return val, ok 76 | } 77 | 78 | // Check if the value is contained in T2 (frequent) 79 | if val, ok := c.t2.Get(key); ok { 80 | return val, ok 81 | } 82 | 83 | // No hit 84 | return 85 | } 86 | 87 | // Add adds a value to the cache. 88 | func (c *ARCCache[K, V]) Add(key K, value V) { 89 | c.lock.Lock() 90 | defer c.lock.Unlock() 91 | 92 | // Check if the value is contained in T1 (recent), and potentially 93 | // promote it to frequent T2 94 | if c.t1.Contains(key) { 95 | c.t1.Remove(key) 96 | c.t2.Add(key, value) 97 | return 98 | } 99 | 100 | // Check if the value is already in T2 (frequent) and update it 101 | if c.t2.Contains(key) { 102 | c.t2.Add(key, value) 103 | return 104 | } 105 | 106 | // Check if this value was recently evicted as part of the 107 | // recently used list 108 | if c.b1.Contains(key) { 109 | // T1 set is too small, increase P appropriately 110 | delta := 1 111 | b1Len := c.b1.Len() 112 | b2Len := c.b2.Len() 113 | if b2Len > b1Len { 114 | delta = b2Len / b1Len 115 | } 116 | if c.p+delta >= c.size { 117 | c.p = c.size 118 | } else { 119 | c.p += delta 120 | } 121 | 122 | // Potentially need to make room in the cache 123 | if c.t1.Len()+c.t2.Len() >= c.size { 124 | c.replace(false) 125 | } 126 | 127 | // Remove from B1 128 | c.b1.Remove(key) 129 | 130 | // Add the key to the frequently used list 131 | c.t2.Add(key, value) 132 | return 133 | } 134 | 135 | // Check if this value was recently evicted as part of the 136 | // frequently used list 137 | if c.b2.Contains(key) { 138 | // T2 set is too small, decrease P appropriately 139 | delta := 1 140 | b1Len := c.b1.Len() 141 | b2Len := c.b2.Len() 142 | if b1Len > b2Len { 143 | delta = b1Len / b2Len 144 | } 145 | if delta >= c.p { 146 | c.p = 0 147 | } else { 148 | c.p -= delta 149 | } 150 | 151 | // Potentially need to make room in the cache 152 | if c.t1.Len()+c.t2.Len() >= c.size { 153 | c.replace(true) 154 | } 155 | 156 | // Remove from B2 157 | c.b2.Remove(key) 158 | 159 | // Add the key to the frequently used list 160 | c.t2.Add(key, value) 161 | return 162 | } 163 | 164 | // Potentially need to make room in the cache 165 | if c.t1.Len()+c.t2.Len() >= c.size { 166 | c.replace(false) 167 | } 168 | 169 | // Keep the size of the ghost buffers trim 170 | if c.b1.Len() > c.size-c.p { 171 | c.b1.RemoveOldest() 172 | } 173 | if c.b2.Len() > c.p { 174 | c.b2.RemoveOldest() 175 | } 176 | 177 | // Add to the recently seen list 178 | c.t1.Add(key, value) 179 | } 180 | 181 | // replace is used to adaptively evict from either T1 or T2 182 | // based on the current learned value of P 183 | func (c *ARCCache[K, V]) replace(b2ContainsKey bool) { 184 | t1Len := c.t1.Len() 185 | if t1Len > 0 && (t1Len > c.p || (t1Len == c.p && b2ContainsKey)) { 186 | k, _, ok := c.t1.RemoveOldest() 187 | if ok { 188 | c.b1.Add(k, struct{}{}) 189 | } 190 | } else { 191 | k, _, ok := c.t2.RemoveOldest() 192 | if ok { 193 | c.b2.Add(k, struct{}{}) 194 | } 195 | } 196 | } 197 | 198 | // Len returns the number of cached entries 199 | func (c *ARCCache[K, V]) Len() int { 200 | c.lock.RLock() 201 | defer c.lock.RUnlock() 202 | return c.t1.Len() + c.t2.Len() 203 | } 204 | 205 | // Cap returns the capacity of the cache 206 | func (c *ARCCache[K, V]) Cap() int { 207 | return c.size 208 | } 209 | 210 | // Keys returns all the cached keys 211 | func (c *ARCCache[K, V]) Keys() []K { 212 | c.lock.RLock() 213 | defer c.lock.RUnlock() 214 | k1 := c.t1.Keys() 215 | k2 := c.t2.Keys() 216 | return append(k1, k2...) 217 | } 218 | 219 | // Values returns all the cached values 220 | func (c *ARCCache[K, V]) Values() []V { 221 | c.lock.RLock() 222 | defer c.lock.RUnlock() 223 | v1 := c.t1.Values() 224 | v2 := c.t2.Values() 225 | return append(v1, v2...) 226 | } 227 | 228 | // Remove is used to purge a key from the cache 229 | func (c *ARCCache[K, V]) Remove(key K) { 230 | c.lock.Lock() 231 | defer c.lock.Unlock() 232 | if c.t1.Remove(key) { 233 | return 234 | } 235 | if c.t2.Remove(key) { 236 | return 237 | } 238 | if c.b1.Remove(key) { 239 | return 240 | } 241 | if c.b2.Remove(key) { 242 | return 243 | } 244 | } 245 | 246 | // Purge is used to clear the cache 247 | func (c *ARCCache[K, V]) Purge() { 248 | c.lock.Lock() 249 | defer c.lock.Unlock() 250 | c.t1.Purge() 251 | c.t2.Purge() 252 | c.b1.Purge() 253 | c.b2.Purge() 254 | } 255 | 256 | // Contains is used to check if the cache contains a key 257 | // without updating recency or frequency. 258 | func (c *ARCCache[K, V]) Contains(key K) bool { 259 | c.lock.RLock() 260 | defer c.lock.RUnlock() 261 | return c.t1.Contains(key) || c.t2.Contains(key) 262 | } 263 | 264 | // Peek is used to inspect the cache value of a key 265 | // without updating recency or frequency. 266 | func (c *ARCCache[K, V]) Peek(key K) (value V, ok bool) { 267 | c.lock.RLock() 268 | defer c.lock.RUnlock() 269 | if val, ok := c.t1.Peek(key); ok { 270 | return val, ok 271 | } 272 | return c.t2.Peek(key) 273 | } 274 | -------------------------------------------------------------------------------- /arc/arc_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package arc 5 | 6 | import ( 7 | "crypto/rand" 8 | "math" 9 | "math/big" 10 | mathrand "math/rand" 11 | "testing" 12 | "time" 13 | ) 14 | 15 | func getRand(tb testing.TB) int64 { 16 | out, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64)) 17 | if err != nil { 18 | tb.Fatal(err) 19 | } 20 | return out.Int64() 21 | } 22 | 23 | func init() { 24 | mathrand.Seed(time.Now().Unix()) 25 | } 26 | 27 | func BenchmarkARC_Rand(b *testing.B) { 28 | l, err := NewARC[int64, int64](8192) 29 | if err != nil { 30 | b.Fatalf("err: %v", err) 31 | } 32 | 33 | trace := make([]int64, b.N*2) 34 | for i := 0; i < b.N*2; i++ { 35 | trace[i] = getRand(b) % 32768 36 | } 37 | 38 | b.ResetTimer() 39 | 40 | var hit, miss int 41 | for i := 0; i < 2*b.N; i++ { 42 | if i%2 == 0 { 43 | l.Add(trace[i], trace[i]) 44 | } else { 45 | if _, ok := l.Get(trace[i]); ok { 46 | hit++ 47 | } else { 48 | miss++ 49 | } 50 | } 51 | } 52 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss)) 53 | } 54 | 55 | func BenchmarkARC_Freq(b *testing.B) { 56 | l, err := NewARC[int64, int64](8192) 57 | if err != nil { 58 | b.Fatalf("err: %v", err) 59 | } 60 | 61 | trace := make([]int64, b.N*2) 62 | for i := 0; i < b.N*2; i++ { 63 | if i%2 == 0 { 64 | trace[i] = getRand(b) % 16384 65 | } else { 66 | trace[i] = getRand(b) % 32768 67 | } 68 | } 69 | 70 | b.ResetTimer() 71 | 72 | for i := 0; i < b.N; i++ { 73 | l.Add(trace[i], trace[i]) 74 | } 75 | var hit, miss int 76 | for i := 0; i < b.N; i++ { 77 | if _, ok := l.Get(trace[i]); ok { 78 | hit++ 79 | } else { 80 | miss++ 81 | } 82 | } 83 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss)) 84 | } 85 | 86 | func TestARC_RandomOps(t *testing.T) { 87 | size := 128 88 | l, err := NewARC[int64, int64](128) 89 | if err != nil { 90 | t.Fatalf("err: %v", err) 91 | } 92 | 93 | n := 200000 94 | for i := 0; i < n; i++ { 95 | key := getRand(t) % 512 96 | r := getRand(t) 97 | switch r % 3 { 98 | case 0: 99 | l.Add(key, key) 100 | case 1: 101 | l.Get(key) 102 | case 2: 103 | l.Remove(key) 104 | } 105 | 106 | if l.t1.Len()+l.t2.Len() > size { 107 | t.Fatalf("bad: t1: %d t2: %d b1: %d b2: %d p: %d", 108 | l.t1.Len(), l.t2.Len(), l.b1.Len(), l.b2.Len(), l.p) 109 | } 110 | if l.b1.Len()+l.b2.Len() > size { 111 | t.Fatalf("bad: t1: %d t2: %d b1: %d b2: %d p: %d", 112 | l.t1.Len(), l.t2.Len(), l.b1.Len(), l.b2.Len(), l.p) 113 | } 114 | } 115 | } 116 | 117 | func TestARC_Get_RecentToFrequent(t *testing.T) { 118 | l, err := NewARC[int, int](128) 119 | if err != nil { 120 | t.Fatalf("err: %v", err) 121 | } 122 | 123 | // Touch all the entries, should be in t1 124 | for i := 0; i < 128; i++ { 125 | l.Add(i, i) 126 | } 127 | if n := l.t1.Len(); n != 128 { 128 | t.Fatalf("bad: %d", n) 129 | } 130 | if n := l.t2.Len(); n != 0 { 131 | t.Fatalf("bad: %d", n) 132 | } 133 | 134 | // Get should upgrade to t2 135 | for i := 0; i < 128; i++ { 136 | if _, ok := l.Get(i); !ok { 137 | t.Fatalf("missing: %d", i) 138 | } 139 | } 140 | if n := l.t1.Len(); n != 0 { 141 | t.Fatalf("bad: %d", n) 142 | } 143 | if n := l.t2.Len(); n != 128 { 144 | t.Fatalf("bad: %d", n) 145 | } 146 | 147 | // Get be from t2 148 | for i := 0; i < 128; i++ { 149 | if _, ok := l.Get(i); !ok { 150 | t.Fatalf("missing: %d", i) 151 | } 152 | } 153 | if n := l.t1.Len(); n != 0 { 154 | t.Fatalf("bad: %d", n) 155 | } 156 | if n := l.t2.Len(); n != 128 { 157 | t.Fatalf("bad: %d", n) 158 | } 159 | } 160 | 161 | func TestARC_Add_RecentToFrequent(t *testing.T) { 162 | l, err := NewARC[int, int](128) 163 | if err != nil { 164 | t.Fatalf("err: %v", err) 165 | } 166 | 167 | // Add initially to t1 168 | l.Add(1, 1) 169 | if n := l.t1.Len(); n != 1 { 170 | t.Fatalf("bad: %d", n) 171 | } 172 | if n := l.t2.Len(); n != 0 { 173 | t.Fatalf("bad: %d", n) 174 | } 175 | 176 | // Add should upgrade to t2 177 | l.Add(1, 1) 178 | if n := l.t1.Len(); n != 0 { 179 | t.Fatalf("bad: %d", n) 180 | } 181 | if n := l.t2.Len(); n != 1 { 182 | t.Fatalf("bad: %d", n) 183 | } 184 | 185 | // Add should remain in t2 186 | l.Add(1, 1) 187 | if n := l.t1.Len(); n != 0 { 188 | t.Fatalf("bad: %d", n) 189 | } 190 | if n := l.t2.Len(); n != 1 { 191 | t.Fatalf("bad: %d", n) 192 | } 193 | } 194 | 195 | func TestARC_Adaptive(t *testing.T) { 196 | l, err := NewARC[int, int](4) 197 | if err != nil { 198 | t.Fatalf("err: %v", err) 199 | } 200 | 201 | // Fill t1 202 | for i := 0; i < 4; i++ { 203 | l.Add(i, i) 204 | } 205 | if n := l.t1.Len(); n != 4 { 206 | t.Fatalf("bad: %d", n) 207 | } 208 | 209 | // Move to t2 210 | l.Get(0) 211 | l.Get(1) 212 | if n := l.t2.Len(); n != 2 { 213 | t.Fatalf("bad: %d", n) 214 | } 215 | 216 | // Evict from t1 217 | l.Add(4, 4) 218 | if n := l.b1.Len(); n != 1 { 219 | t.Fatalf("bad: %d", n) 220 | } 221 | 222 | // Current state 223 | // t1 : (MRU) [4, 3] (LRU) 224 | // t2 : (MRU) [1, 0] (LRU) 225 | // b1 : (MRU) [2] (LRU) 226 | // b2 : (MRU) [] (LRU) 227 | 228 | // Add 2, should cause hit on b1 229 | l.Add(2, 2) 230 | if n := l.b1.Len(); n != 1 { 231 | t.Fatalf("bad: %d", n) 232 | } 233 | if l.p != 1 { 234 | t.Fatalf("bad: %d", l.p) 235 | } 236 | if n := l.t2.Len(); n != 3 { 237 | t.Fatalf("bad: %d", n) 238 | } 239 | 240 | // Current state 241 | // t1 : (MRU) [4] (LRU) 242 | // t2 : (MRU) [2, 1, 0] (LRU) 243 | // b1 : (MRU) [3] (LRU) 244 | // b2 : (MRU) [] (LRU) 245 | 246 | // Add 4, should migrate to t2 247 | l.Add(4, 4) 248 | if n := l.t1.Len(); n != 0 { 249 | t.Fatalf("bad: %d", n) 250 | } 251 | if n := l.t2.Len(); n != 4 { 252 | t.Fatalf("bad: %d", n) 253 | } 254 | 255 | // Current state 256 | // t1 : (MRU) [] (LRU) 257 | // t2 : (MRU) [4, 2, 1, 0] (LRU) 258 | // b1 : (MRU) [3] (LRU) 259 | // b2 : (MRU) [] (LRU) 260 | 261 | // Add 4, should evict to b2 262 | l.Add(5, 5) 263 | if n := l.t1.Len(); n != 1 { 264 | t.Fatalf("bad: %d", n) 265 | } 266 | if n := l.t2.Len(); n != 3 { 267 | t.Fatalf("bad: %d", n) 268 | } 269 | if n := l.b2.Len(); n != 1 { 270 | t.Fatalf("bad: %d", n) 271 | } 272 | 273 | // Current state 274 | // t1 : (MRU) [5] (LRU) 275 | // t2 : (MRU) [4, 2, 1] (LRU) 276 | // b1 : (MRU) [3] (LRU) 277 | // b2 : (MRU) [0] (LRU) 278 | 279 | // Add 0, should decrease p 280 | l.Add(0, 0) 281 | if n := l.t1.Len(); n != 0 { 282 | t.Fatalf("bad: %d", n) 283 | } 284 | if n := l.t2.Len(); n != 4 { 285 | t.Fatalf("bad: %d", n) 286 | } 287 | if n := l.b1.Len(); n != 2 { 288 | t.Fatalf("bad: %d", n) 289 | } 290 | if n := l.b2.Len(); n != 0 { 291 | t.Fatalf("bad: %d", n) 292 | } 293 | if l.p != 0 { 294 | t.Fatalf("bad: %d", l.p) 295 | } 296 | 297 | // Current state 298 | // t1 : (MRU) [] (LRU) 299 | // t2 : (MRU) [0, 4, 2, 1] (LRU) 300 | // b1 : (MRU) [5, 3] (LRU) 301 | // b2 : (MRU) [0] (LRU) 302 | } 303 | 304 | func TestARC(t *testing.T) { 305 | l, err := NewARC[int, int](128) 306 | if err != nil { 307 | t.Fatalf("err: %v", err) 308 | } 309 | 310 | for i := 0; i < 256; i++ { 311 | l.Add(i, i) 312 | } 313 | if l.Len() != 128 { 314 | t.Fatalf("bad len: %v", l.Len()) 315 | } 316 | if l.Cap() != 128 { 317 | t.Fatalf("expect %d, but %d", 128, l.Cap()) 318 | } 319 | 320 | for i, k := range l.Keys() { 321 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 322 | t.Fatalf("bad key: %v", k) 323 | } 324 | } 325 | for i, v := range l.Values() { 326 | if v != i+128 { 327 | t.Fatalf("bad value: %v", v) 328 | } 329 | } 330 | for i := 0; i < 128; i++ { 331 | if _, ok := l.Get(i); ok { 332 | t.Fatalf("should be evicted") 333 | } 334 | } 335 | for i := 128; i < 256; i++ { 336 | if _, ok := l.Get(i); !ok { 337 | t.Fatalf("should not be evicted") 338 | } 339 | } 340 | for i := 128; i < 192; i++ { 341 | l.Remove(i) 342 | if _, ok := l.Get(i); ok { 343 | t.Fatalf("should be deleted") 344 | } 345 | } 346 | if l.Cap() != 128 { 347 | t.Fatalf("expect %d, but %d", 128, l.Cap()) 348 | } 349 | 350 | l.Purge() 351 | if l.Len() != 0 { 352 | t.Fatalf("bad len: %v", l.Len()) 353 | } 354 | if _, ok := l.Get(200); ok { 355 | t.Fatalf("should contain nothing") 356 | } 357 | if l.Cap() != 128 { 358 | t.Fatalf("expect %d, but %d", 128, l.Cap()) 359 | } 360 | } 361 | 362 | // Test that Contains doesn't update recent-ness 363 | func TestARC_Contains(t *testing.T) { 364 | l, err := NewARC[int, int](2) 365 | if err != nil { 366 | t.Fatalf("err: %v", err) 367 | } 368 | 369 | l.Add(1, 1) 370 | l.Add(2, 2) 371 | if !l.Contains(1) { 372 | t.Errorf("1 should be contained") 373 | } 374 | 375 | l.Add(3, 3) 376 | if l.Contains(1) { 377 | t.Errorf("Contains should not have updated recent-ness of 1") 378 | } 379 | } 380 | 381 | // Test that Peek doesn't update recent-ness 382 | func TestARC_Peek(t *testing.T) { 383 | l, err := NewARC[int, int](2) 384 | if err != nil { 385 | t.Fatalf("err: %v", err) 386 | } 387 | 388 | l.Add(1, 1) 389 | l.Add(2, 2) 390 | if v, ok := l.Peek(1); !ok || v != 1 { 391 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 392 | } 393 | 394 | l.Add(3, 3) 395 | if l.Contains(1) { 396 | t.Errorf("should not have updated recent-ness of 1") 397 | } 398 | } 399 | -------------------------------------------------------------------------------- /arc/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/hashicorp/golang-lru/arc/v2 2 | 3 | go 1.18 4 | 5 | require github.com/hashicorp/golang-lru/v2 v2.0.7 6 | -------------------------------------------------------------------------------- /arc/go.sum: -------------------------------------------------------------------------------- 1 | github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= 2 | github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= 3 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | // Package lru provides three different LRU caches of varying sophistication. 5 | // 6 | // Cache is a simple LRU cache. It is based on the LRU implementation in 7 | // groupcache: https://github.com/golang/groupcache/tree/master/lru 8 | // 9 | // TwoQueueCache tracks frequently used and recently used entries separately. 10 | // This avoids a burst of accesses from taking out frequently used entries, at 11 | // the cost of about 2x computational overhead and some extra bookkeeping. 12 | // 13 | // ARCCache is an adaptive replacement cache. It tracks recent evictions as well 14 | // as recent usage in both the frequent and recent caches. Its computational 15 | // overhead is comparable to TwoQueueCache, but the memory overhead is linear 16 | // with the size of the cache. 17 | // 18 | // ARC has been patented by IBM, so do not use it if that is problematic for 19 | // your program. For this reason, it is in a separate go module contained within 20 | // this repository. 21 | // 22 | // All caches in this package take locks while operating, and are therefore 23 | // thread-safe for consumers. 24 | package lru 25 | -------------------------------------------------------------------------------- /expirable/expirable_lru.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package expirable 5 | 6 | import ( 7 | "sync" 8 | "time" 9 | 10 | "github.com/hashicorp/golang-lru/v2/internal" 11 | ) 12 | 13 | // EvictCallback is used to get a callback when a cache entry is evicted 14 | type EvictCallback[K comparable, V any] func(key K, value V) 15 | 16 | // LRU implements a thread-safe LRU with expirable entries. 17 | type LRU[K comparable, V any] struct { 18 | size int 19 | evictList *internal.LruList[K, V] 20 | items map[K]*internal.Entry[K, V] 21 | onEvict EvictCallback[K, V] 22 | 23 | // expirable options 24 | mu sync.Mutex 25 | ttl time.Duration 26 | done chan struct{} 27 | 28 | // buckets for expiration 29 | buckets []bucket[K, V] 30 | // uint8 because it's number between 0 and numBuckets 31 | nextCleanupBucket uint8 32 | } 33 | 34 | // bucket is a container for holding entries to be expired 35 | type bucket[K comparable, V any] struct { 36 | entries map[K]*internal.Entry[K, V] 37 | newestEntry time.Time 38 | } 39 | 40 | // noEvictionTTL - very long ttl to prevent eviction 41 | const noEvictionTTL = time.Hour * 24 * 365 * 10 42 | 43 | // because of uint8 usage for nextCleanupBucket, should not exceed 256. 44 | // casting it as uint8 explicitly requires type conversions in multiple places 45 | const numBuckets = 100 46 | 47 | // NewLRU returns a new thread-safe cache with expirable entries. 48 | // 49 | // Size parameter set to 0 makes cache of unlimited size, e.g. turns LRU mechanism off. 50 | // 51 | // Providing 0 TTL turns expiring off. 52 | // 53 | // Delete expired entries every 1/100th of ttl value. Goroutine which deletes expired entries runs indefinitely. 54 | func NewLRU[K comparable, V any](size int, onEvict EvictCallback[K, V], ttl time.Duration) *LRU[K, V] { 55 | if size < 0 { 56 | size = 0 57 | } 58 | if ttl <= 0 { 59 | ttl = noEvictionTTL 60 | } 61 | 62 | res := LRU[K, V]{ 63 | ttl: ttl, 64 | size: size, 65 | evictList: internal.NewList[K, V](), 66 | items: make(map[K]*internal.Entry[K, V]), 67 | onEvict: onEvict, 68 | done: make(chan struct{}), 69 | } 70 | 71 | // initialize the buckets 72 | res.buckets = make([]bucket[K, V], numBuckets) 73 | for i := 0; i < numBuckets; i++ { 74 | res.buckets[i] = bucket[K, V]{entries: make(map[K]*internal.Entry[K, V])} 75 | } 76 | 77 | // enable deleteExpired() running in separate goroutine for cache with non-zero TTL 78 | // 79 | // Important: done channel is never closed, so deleteExpired() goroutine will never exit, 80 | // it's decided to add functionality to close it in the version later than v2. 81 | if res.ttl != noEvictionTTL { 82 | go func(done <-chan struct{}) { 83 | ticker := time.NewTicker(res.ttl / numBuckets) 84 | defer ticker.Stop() 85 | for { 86 | select { 87 | case <-done: 88 | return 89 | case <-ticker.C: 90 | res.deleteExpired() 91 | } 92 | } 93 | }(res.done) 94 | } 95 | return &res 96 | } 97 | 98 | // Purge clears the cache completely. 99 | // onEvict is called for each evicted key. 100 | func (c *LRU[K, V]) Purge() { 101 | c.mu.Lock() 102 | defer c.mu.Unlock() 103 | for k, v := range c.items { 104 | if c.onEvict != nil { 105 | c.onEvict(k, v.Value) 106 | } 107 | delete(c.items, k) 108 | } 109 | for _, b := range c.buckets { 110 | for _, ent := range b.entries { 111 | delete(b.entries, ent.Key) 112 | } 113 | } 114 | c.evictList.Init() 115 | } 116 | 117 | // Add adds a value to the cache. Returns true if an eviction occurred. 118 | // Returns false if there was no eviction: the item was already in the cache, 119 | // or the size was not exceeded. 120 | func (c *LRU[K, V]) Add(key K, value V) (evicted bool) { 121 | c.mu.Lock() 122 | defer c.mu.Unlock() 123 | now := time.Now() 124 | 125 | // Check for existing item 126 | if ent, ok := c.items[key]; ok { 127 | c.evictList.MoveToFront(ent) 128 | c.removeFromBucket(ent) // remove the entry from its current bucket as expiresAt is renewed 129 | ent.Value = value 130 | ent.ExpiresAt = now.Add(c.ttl) 131 | c.addToBucket(ent) 132 | return false 133 | } 134 | 135 | // Add new item 136 | ent := c.evictList.PushFrontExpirable(key, value, now.Add(c.ttl)) 137 | c.items[key] = ent 138 | c.addToBucket(ent) // adds the entry to the appropriate bucket and sets entry.expireBucket 139 | 140 | evict := c.size > 0 && c.evictList.Length() > c.size 141 | // Verify size not exceeded 142 | if evict { 143 | c.removeOldest() 144 | } 145 | return evict 146 | } 147 | 148 | // Get looks up a key's value from the cache. 149 | func (c *LRU[K, V]) Get(key K) (value V, ok bool) { 150 | c.mu.Lock() 151 | defer c.mu.Unlock() 152 | var ent *internal.Entry[K, V] 153 | if ent, ok = c.items[key]; ok { 154 | // Expired item check 155 | if time.Now().After(ent.ExpiresAt) { 156 | return value, false 157 | } 158 | c.evictList.MoveToFront(ent) 159 | return ent.Value, true 160 | } 161 | return 162 | } 163 | 164 | // Contains checks if a key is in the cache, without updating the recent-ness 165 | // or deleting it for being stale. 166 | func (c *LRU[K, V]) Contains(key K) (ok bool) { 167 | c.mu.Lock() 168 | defer c.mu.Unlock() 169 | _, ok = c.items[key] 170 | return ok 171 | } 172 | 173 | // Peek returns the key value (or undefined if not found) without updating 174 | // the "recently used"-ness of the key. 175 | func (c *LRU[K, V]) Peek(key K) (value V, ok bool) { 176 | c.mu.Lock() 177 | defer c.mu.Unlock() 178 | var ent *internal.Entry[K, V] 179 | if ent, ok = c.items[key]; ok { 180 | // Expired item check 181 | if time.Now().After(ent.ExpiresAt) { 182 | return value, false 183 | } 184 | return ent.Value, true 185 | } 186 | return 187 | } 188 | 189 | // Remove removes the provided key from the cache, returning if the 190 | // key was contained. 191 | func (c *LRU[K, V]) Remove(key K) bool { 192 | c.mu.Lock() 193 | defer c.mu.Unlock() 194 | if ent, ok := c.items[key]; ok { 195 | c.removeElement(ent) 196 | return true 197 | } 198 | return false 199 | } 200 | 201 | // RemoveOldest removes the oldest item from the cache. 202 | func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool) { 203 | c.mu.Lock() 204 | defer c.mu.Unlock() 205 | if ent := c.evictList.Back(); ent != nil { 206 | c.removeElement(ent) 207 | return ent.Key, ent.Value, true 208 | } 209 | return 210 | } 211 | 212 | // GetOldest returns the oldest entry 213 | func (c *LRU[K, V]) GetOldest() (key K, value V, ok bool) { 214 | c.mu.Lock() 215 | defer c.mu.Unlock() 216 | if ent := c.evictList.Back(); ent != nil { 217 | return ent.Key, ent.Value, true 218 | } 219 | return 220 | } 221 | 222 | // Keys returns a slice of the keys in the cache, from oldest to newest. 223 | // Expired entries are filtered out. 224 | func (c *LRU[K, V]) Keys() []K { 225 | c.mu.Lock() 226 | defer c.mu.Unlock() 227 | keys := make([]K, 0, len(c.items)) 228 | now := time.Now() 229 | for ent := c.evictList.Back(); ent != nil; ent = ent.PrevEntry() { 230 | if now.After(ent.ExpiresAt) { 231 | continue 232 | } 233 | keys = append(keys, ent.Key) 234 | } 235 | return keys 236 | } 237 | 238 | // Values returns a slice of the values in the cache, from oldest to newest. 239 | // Expired entries are filtered out. 240 | func (c *LRU[K, V]) Values() []V { 241 | c.mu.Lock() 242 | defer c.mu.Unlock() 243 | values := make([]V, 0, len(c.items)) 244 | now := time.Now() 245 | for ent := c.evictList.Back(); ent != nil; ent = ent.PrevEntry() { 246 | if now.After(ent.ExpiresAt) { 247 | continue 248 | } 249 | values = append(values, ent.Value) 250 | } 251 | return values 252 | } 253 | 254 | // Len returns the number of items in the cache. 255 | func (c *LRU[K, V]) Len() int { 256 | c.mu.Lock() 257 | defer c.mu.Unlock() 258 | return c.evictList.Length() 259 | } 260 | 261 | // Resize changes the cache size. Size of 0 means unlimited. 262 | func (c *LRU[K, V]) Resize(size int) (evicted int) { 263 | c.mu.Lock() 264 | defer c.mu.Unlock() 265 | if size <= 0 { 266 | c.size = 0 267 | return 0 268 | } 269 | diff := c.evictList.Length() - size 270 | if diff < 0 { 271 | diff = 0 272 | } 273 | for i := 0; i < diff; i++ { 274 | c.removeOldest() 275 | } 276 | c.size = size 277 | return diff 278 | } 279 | 280 | // Close destroys cleanup goroutine. To clean up the cache, run Purge() before Close(). 281 | // func (c *LRU[K, V]) Close() { 282 | // c.mu.Lock() 283 | // defer c.mu.Unlock() 284 | // select { 285 | // case <-c.done: 286 | // return 287 | // default: 288 | // } 289 | // close(c.done) 290 | // } 291 | 292 | // removeOldest removes the oldest item from the cache. Has to be called with lock! 293 | func (c *LRU[K, V]) removeOldest() { 294 | if ent := c.evictList.Back(); ent != nil { 295 | c.removeElement(ent) 296 | } 297 | } 298 | 299 | // removeElement is used to remove a given list element from the cache. Has to be called with lock! 300 | func (c *LRU[K, V]) removeElement(e *internal.Entry[K, V]) { 301 | c.evictList.Remove(e) 302 | delete(c.items, e.Key) 303 | c.removeFromBucket(e) 304 | if c.onEvict != nil { 305 | c.onEvict(e.Key, e.Value) 306 | } 307 | } 308 | 309 | // deleteExpired deletes expired records from the oldest bucket, waiting for the newest entry 310 | // in it to expire first. 311 | func (c *LRU[K, V]) deleteExpired() { 312 | c.mu.Lock() 313 | bucketIdx := c.nextCleanupBucket 314 | timeToExpire := time.Until(c.buckets[bucketIdx].newestEntry) 315 | // wait for newest entry to expire before cleanup without holding lock 316 | if timeToExpire > 0 { 317 | c.mu.Unlock() 318 | time.Sleep(timeToExpire) 319 | c.mu.Lock() 320 | } 321 | for _, ent := range c.buckets[bucketIdx].entries { 322 | c.removeElement(ent) 323 | } 324 | c.nextCleanupBucket = (c.nextCleanupBucket + 1) % numBuckets 325 | c.mu.Unlock() 326 | } 327 | 328 | // addToBucket adds entry to expire bucket so that it will be cleaned up when the time comes. Has to be called with lock! 329 | func (c *LRU[K, V]) addToBucket(e *internal.Entry[K, V]) { 330 | bucketID := (numBuckets + c.nextCleanupBucket - 1) % numBuckets 331 | e.ExpireBucket = bucketID 332 | c.buckets[bucketID].entries[e.Key] = e 333 | if c.buckets[bucketID].newestEntry.Before(e.ExpiresAt) { 334 | c.buckets[bucketID].newestEntry = e.ExpiresAt 335 | } 336 | } 337 | 338 | // removeFromBucket removes the entry from its corresponding bucket. Has to be called with lock! 339 | func (c *LRU[K, V]) removeFromBucket(e *internal.Entry[K, V]) { 340 | delete(c.buckets[e.ExpireBucket].entries, e.Key) 341 | } 342 | 343 | // Cap returns the capacity of the cache 344 | func (c *LRU[K, V]) Cap() int { 345 | return c.size 346 | } -------------------------------------------------------------------------------- /expirable/expirable_lru_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package expirable 5 | 6 | import ( 7 | "crypto/rand" 8 | "fmt" 9 | "math" 10 | "math/big" 11 | "reflect" 12 | "sync" 13 | "testing" 14 | "time" 15 | 16 | "github.com/hashicorp/golang-lru/v2/simplelru" 17 | ) 18 | 19 | func BenchmarkLRU_Rand_NoExpire(b *testing.B) { 20 | l := NewLRU[int64, int64](8192, nil, 0) 21 | 22 | trace := make([]int64, b.N*2) 23 | for i := 0; i < b.N*2; i++ { 24 | trace[i] = getRand(b) % 32768 25 | } 26 | 27 | b.ResetTimer() 28 | 29 | var hit, miss int 30 | for i := 0; i < 2*b.N; i++ { 31 | if i%2 == 0 { 32 | l.Add(trace[i], trace[i]) 33 | } else { 34 | if _, ok := l.Get(trace[i]); ok { 35 | hit++ 36 | } else { 37 | miss++ 38 | } 39 | } 40 | } 41 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss)) 42 | } 43 | 44 | func BenchmarkLRU_Freq_NoExpire(b *testing.B) { 45 | l := NewLRU[int64, int64](8192, nil, 0) 46 | 47 | trace := make([]int64, b.N*2) 48 | for i := 0; i < b.N*2; i++ { 49 | if i%2 == 0 { 50 | trace[i] = getRand(b) % 16384 51 | } else { 52 | trace[i] = getRand(b) % 32768 53 | } 54 | } 55 | 56 | b.ResetTimer() 57 | 58 | for i := 0; i < b.N; i++ { 59 | l.Add(trace[i], trace[i]) 60 | } 61 | var hit, miss int 62 | for i := 0; i < b.N; i++ { 63 | if _, ok := l.Get(trace[i]); ok { 64 | hit++ 65 | } else { 66 | miss++ 67 | } 68 | } 69 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss)) 70 | } 71 | 72 | func BenchmarkLRU_Rand_WithExpire(b *testing.B) { 73 | l := NewLRU[int64, int64](8192, nil, time.Millisecond*10) 74 | 75 | trace := make([]int64, b.N*2) 76 | for i := 0; i < b.N*2; i++ { 77 | trace[i] = getRand(b) % 32768 78 | } 79 | 80 | b.ResetTimer() 81 | 82 | var hit, miss int 83 | for i := 0; i < 2*b.N; i++ { 84 | if i%2 == 0 { 85 | l.Add(trace[i], trace[i]) 86 | } else { 87 | if _, ok := l.Get(trace[i]); ok { 88 | hit++ 89 | } else { 90 | miss++ 91 | } 92 | } 93 | } 94 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss)) 95 | } 96 | 97 | func BenchmarkLRU_Freq_WithExpire(b *testing.B) { 98 | l := NewLRU[int64, int64](8192, nil, time.Millisecond*10) 99 | 100 | trace := make([]int64, b.N*2) 101 | for i := 0; i < b.N*2; i++ { 102 | if i%2 == 0 { 103 | trace[i] = getRand(b) % 16384 104 | } else { 105 | trace[i] = getRand(b) % 32768 106 | } 107 | } 108 | 109 | b.ResetTimer() 110 | 111 | for i := 0; i < b.N; i++ { 112 | l.Add(trace[i], trace[i]) 113 | } 114 | var hit, miss int 115 | for i := 0; i < b.N; i++ { 116 | if _, ok := l.Get(trace[i]); ok { 117 | hit++ 118 | } else { 119 | miss++ 120 | } 121 | } 122 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss)) 123 | } 124 | 125 | func TestLRUInterface(_ *testing.T) { 126 | var _ simplelru.LRUCache[int, int] = &LRU[int, int]{} 127 | } 128 | 129 | func TestLRUNoPurge(t *testing.T) { 130 | lc := NewLRU[string, string](10, nil, 0) 131 | 132 | lc.Add("key1", "val1") 133 | if lc.Len() != 1 { 134 | t.Fatalf("length differs from expected") 135 | } 136 | 137 | v, ok := lc.Peek("key1") 138 | if v != "val1" { 139 | t.Fatalf("value differs from expected") 140 | } 141 | if !ok { 142 | t.Fatalf("should be true") 143 | } 144 | 145 | if !lc.Contains("key1") { 146 | t.Fatalf("should contain key1") 147 | } 148 | if lc.Contains("key2") { 149 | t.Fatalf("should not contain key2") 150 | } 151 | 152 | v, ok = lc.Peek("key2") 153 | if v != "" { 154 | t.Fatalf("should be empty") 155 | } 156 | if ok { 157 | t.Fatalf("should be false") 158 | } 159 | 160 | if !reflect.DeepEqual(lc.Keys(), []string{"key1"}) { 161 | t.Fatalf("value differs from expected") 162 | } 163 | 164 | if lc.Resize(0) != 0 { 165 | t.Fatalf("evicted count differs from expected") 166 | } 167 | if lc.Resize(2) != 0 { 168 | t.Fatalf("evicted count differs from expected") 169 | } 170 | lc.Add("key2", "val2") 171 | if lc.Resize(1) != 1 { 172 | t.Fatalf("evicted count differs from expected") 173 | } 174 | } 175 | 176 | func TestLRUEdgeCases(t *testing.T) { 177 | lc := NewLRU[string, *string](2, nil, 0) 178 | 179 | // Adding a nil value 180 | lc.Add("key1", nil) 181 | 182 | value, exists := lc.Get("key1") 183 | if value != nil || !exists { 184 | t.Fatalf("unexpected value or existence flag for key1: value=%v, exists=%v", value, exists) 185 | } 186 | 187 | // Adding an entry with the same key but different value 188 | newVal := "val1" 189 | lc.Add("key1", &newVal) 190 | 191 | value, exists = lc.Get("key1") 192 | if value != &newVal || !exists { 193 | t.Fatalf("unexpected value or existence flag for key1: value=%v, exists=%v", value, exists) 194 | } 195 | } 196 | 197 | func TestLRU_Values(t *testing.T) { 198 | lc := NewLRU[string, string](3, nil, 0) 199 | 200 | lc.Add("key1", "val1") 201 | lc.Add("key2", "val2") 202 | lc.Add("key3", "val3") 203 | 204 | values := lc.Values() 205 | if !reflect.DeepEqual(values, []string{"val1", "val2", "val3"}) { 206 | t.Fatalf("values differs from expected") 207 | } 208 | } 209 | 210 | // func TestExpirableMultipleClose(_ *testing.T) { 211 | // lc := NewLRU[string, string](10, nil, 0) 212 | // lc.Close() 213 | // // should not panic 214 | // lc.Close() 215 | // } 216 | 217 | func TestLRUWithPurge(t *testing.T) { 218 | var evicted []string 219 | lc := NewLRU(10, func(key string, value string) { evicted = append(evicted, key, value) }, 150*time.Millisecond) 220 | 221 | k, v, ok := lc.GetOldest() 222 | if k != "" { 223 | t.Fatalf("should be empty") 224 | } 225 | if v != "" { 226 | t.Fatalf("should be empty") 227 | } 228 | if ok { 229 | t.Fatalf("should be false") 230 | } 231 | 232 | lc.Add("key1", "val1") 233 | 234 | time.Sleep(100 * time.Millisecond) // not enough to expire 235 | if lc.Len() != 1 { 236 | t.Fatalf("length differs from expected") 237 | } 238 | 239 | v, ok = lc.Get("key1") 240 | if v != "val1" { 241 | t.Fatalf("value differs from expected") 242 | } 243 | if !ok { 244 | t.Fatalf("should be true") 245 | } 246 | 247 | time.Sleep(200 * time.Millisecond) // expire 248 | v, ok = lc.Get("key1") 249 | if ok { 250 | t.Fatalf("should be false") 251 | } 252 | if v != "" { 253 | t.Fatalf("should be nil") 254 | } 255 | 256 | if lc.Len() != 0 { 257 | t.Fatalf("length differs from expected") 258 | } 259 | if !reflect.DeepEqual(evicted, []string{"key1", "val1"}) { 260 | t.Fatalf("value differs from expected") 261 | } 262 | 263 | // add new entry 264 | lc.Add("key2", "val2") 265 | if lc.Len() != 1 { 266 | t.Fatalf("length differs from expected") 267 | } 268 | 269 | k, v, ok = lc.GetOldest() 270 | if k != "key2" { 271 | t.Fatalf("value differs from expected") 272 | } 273 | if v != "val2" { 274 | t.Fatalf("value differs from expected") 275 | } 276 | if !ok { 277 | t.Fatalf("should be true") 278 | } 279 | 280 | // DeleteExpired, nothing deleted 281 | lc.deleteExpired() 282 | if lc.Len() != 1 { 283 | t.Fatalf("length differs from expected") 284 | } 285 | if !reflect.DeepEqual(evicted, []string{"key1", "val1"}) { 286 | t.Fatalf("value differs from expected") 287 | } 288 | 289 | // Purge, cache should be clean 290 | lc.Purge() 291 | if lc.Len() != 0 { 292 | t.Fatalf("length differs from expected") 293 | } 294 | if !reflect.DeepEqual(evicted, []string{"key1", "val1", "key2", "val2"}) { 295 | t.Fatalf("value differs from expected") 296 | } 297 | } 298 | 299 | func TestLRUWithPurgeEnforcedBySize(t *testing.T) { 300 | lc := NewLRU[string, string](10, nil, time.Hour) 301 | 302 | for i := 0; i < 100; i++ { 303 | i := i 304 | lc.Add(fmt.Sprintf("key%d", i), fmt.Sprintf("val%d", i)) 305 | v, ok := lc.Get(fmt.Sprintf("key%d", i)) 306 | if v != fmt.Sprintf("val%d", i) { 307 | t.Fatalf("value differs from expected") 308 | } 309 | if !ok { 310 | t.Fatalf("should be true") 311 | } 312 | if lc.Len() > 20 { 313 | t.Fatalf("length should be less than 20") 314 | } 315 | } 316 | 317 | if lc.Len() != 10 { 318 | t.Fatalf("length differs from expected") 319 | } 320 | } 321 | 322 | func TestLRUConcurrency(t *testing.T) { 323 | lc := NewLRU[string, string](0, nil, 0) 324 | wg := sync.WaitGroup{} 325 | wg.Add(1000) 326 | for i := 0; i < 1000; i++ { 327 | go func(i int) { 328 | lc.Add(fmt.Sprintf("key-%d", i/10), fmt.Sprintf("val-%d", i/10)) 329 | wg.Done() 330 | }(i) 331 | } 332 | wg.Wait() 333 | if lc.Len() != 100 { 334 | t.Fatalf("length differs from expected") 335 | } 336 | } 337 | 338 | func TestLRUInvalidateAndEvict(t *testing.T) { 339 | var evicted int 340 | lc := NewLRU(-1, func(_, _ string) { evicted++ }, 0) 341 | 342 | lc.Add("key1", "val1") 343 | lc.Add("key2", "val2") 344 | 345 | val, ok := lc.Get("key1") 346 | if !ok { 347 | t.Fatalf("should be true") 348 | } 349 | if val != "val1" { 350 | t.Fatalf("value differs from expected") 351 | } 352 | if evicted != 0 { 353 | t.Fatalf("value differs from expected") 354 | } 355 | 356 | lc.Remove("key1") 357 | if evicted != 1 { 358 | t.Fatalf("value differs from expected") 359 | } 360 | val, ok = lc.Get("key1") 361 | if val != "" { 362 | t.Fatalf("should be empty") 363 | } 364 | if ok { 365 | t.Fatalf("should be false") 366 | } 367 | } 368 | 369 | func TestLoadingExpired(t *testing.T) { 370 | lc := NewLRU[string, string](0, nil, time.Millisecond*5) 371 | 372 | lc.Add("key1", "val1") 373 | if lc.Len() != 1 { 374 | t.Fatalf("length differs from expected") 375 | } 376 | 377 | v, ok := lc.Peek("key1") 378 | if v != "val1" { 379 | t.Fatalf("value differs from expected") 380 | } 381 | if !ok { 382 | t.Fatalf("should be true") 383 | } 384 | 385 | v, ok = lc.Get("key1") 386 | if v != "val1" { 387 | t.Fatalf("value differs from expected") 388 | } 389 | if !ok { 390 | t.Fatalf("should be true") 391 | } 392 | 393 | for { 394 | result, ok := lc.Get("key1") 395 | if ok && result == "" { 396 | t.Fatalf("ok should return a result") 397 | } 398 | if !ok { 399 | break 400 | } 401 | } 402 | 403 | time.Sleep(time.Millisecond * 100) // wait for expiration reaper 404 | if lc.Len() != 0 { 405 | t.Fatalf("length differs from expected") 406 | } 407 | 408 | v, ok = lc.Peek("key1") 409 | if v != "" { 410 | t.Fatalf("should be empty") 411 | } 412 | if ok { 413 | t.Fatalf("should be false") 414 | } 415 | 416 | v, ok = lc.Get("key1") 417 | if v != "" { 418 | t.Fatalf("should be empty") 419 | } 420 | if ok { 421 | t.Fatalf("should be false") 422 | } 423 | } 424 | 425 | func TestLRURemoveOldest(t *testing.T) { 426 | lc := NewLRU[string, string](2, nil, 0) 427 | 428 | if lc.Cap() != 2 { 429 | t.Fatalf("expect cap is 2") 430 | } 431 | 432 | k, v, ok := lc.RemoveOldest() 433 | if k != "" { 434 | t.Fatalf("should be empty") 435 | } 436 | if v != "" { 437 | t.Fatalf("should be empty") 438 | } 439 | if ok { 440 | t.Fatalf("should be false") 441 | } 442 | 443 | ok = lc.Remove("non_existent") 444 | if ok { 445 | t.Fatalf("should be false") 446 | } 447 | 448 | lc.Add("key1", "val1") 449 | if lc.Len() != 1 { 450 | t.Fatalf("length differs from expected") 451 | } 452 | 453 | v, ok = lc.Get("key1") 454 | if !ok { 455 | t.Fatalf("should be true") 456 | } 457 | if v != "val1" { 458 | t.Fatalf("value differs from expected") 459 | } 460 | 461 | if !reflect.DeepEqual(lc.Keys(), []string{"key1"}) { 462 | t.Fatalf("value differs from expected") 463 | } 464 | if lc.Len() != 1 { 465 | t.Fatalf("length differs from expected") 466 | } 467 | 468 | lc.Add("key2", "val2") 469 | if !reflect.DeepEqual(lc.Keys(), []string{"key1", "key2"}) { 470 | t.Fatalf("value differs from expected") 471 | } 472 | if lc.Len() != 2 { 473 | t.Fatalf("length differs from expected") 474 | } 475 | 476 | k, v, ok = lc.RemoveOldest() 477 | if k != "key1" { 478 | t.Fatalf("value differs from expected") 479 | } 480 | if v != "val1" { 481 | t.Fatalf("value differs from expected") 482 | } 483 | if !ok { 484 | t.Fatalf("should be true") 485 | } 486 | 487 | if !reflect.DeepEqual(lc.Keys(), []string{"key2"}) { 488 | t.Fatalf("value differs from expected") 489 | } 490 | if lc.Len() != 1 { 491 | t.Fatalf("length differs from expected") 492 | } 493 | } 494 | 495 | func ExampleLRU() { 496 | // make cache with 10ms TTL and 5 max keys 497 | cache := NewLRU[string, string](5, nil, time.Millisecond*10) 498 | 499 | // set value under key1. 500 | cache.Add("key1", "val1") 501 | 502 | // get value under key1 503 | r, ok := cache.Get("key1") 504 | 505 | // check for OK value 506 | if ok { 507 | fmt.Printf("value before expiration is found: %v, value: %q\n", ok, r) 508 | } 509 | 510 | // wait for cache to expire 511 | time.Sleep(time.Millisecond * 100) 512 | 513 | // get value under key1 after key expiration 514 | r, ok = cache.Get("key1") 515 | fmt.Printf("value after expiration is found: %v, value: %q\n", ok, r) 516 | 517 | // set value under key2, would evict old entry because it is already expired. 518 | cache.Add("key2", "val2") 519 | 520 | fmt.Printf("Cache len: %d\n", cache.Len()) 521 | // Output: 522 | // value before expiration is found: true, value: "val1" 523 | // value after expiration is found: false, value: "" 524 | // Cache len: 1 525 | } 526 | 527 | func getRand(tb testing.TB) int64 { 528 | out, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64)) 529 | if err != nil { 530 | tb.Fatal(err) 531 | } 532 | return out.Int64() 533 | } 534 | 535 | func (c *LRU[K, V]) wantKeys(t *testing.T, want []K) { 536 | t.Helper() 537 | got := c.Keys() 538 | if !reflect.DeepEqual(got, want) { 539 | t.Errorf("wrong keys got: %v, want: %v ", got, want) 540 | } 541 | } 542 | 543 | func TestCache_EvictionSameKey(t *testing.T) { 544 | var evictedKeys []int 545 | 546 | cache := NewLRU[int, struct{}]( 547 | 2, 548 | func(key int, _ struct{}) { 549 | evictedKeys = append(evictedKeys, key) 550 | }, 551 | 0) 552 | 553 | if evicted := cache.Add(1, struct{}{}); evicted { 554 | t.Error("First 1: got unexpected eviction") 555 | } 556 | cache.wantKeys(t, []int{1}) 557 | 558 | if evicted := cache.Add(2, struct{}{}); evicted { 559 | t.Error("2: got unexpected eviction") 560 | } 561 | cache.wantKeys(t, []int{1, 2}) 562 | 563 | if evicted := cache.Add(1, struct{}{}); evicted { 564 | t.Error("Second 1: got unexpected eviction") 565 | } 566 | cache.wantKeys(t, []int{2, 1}) 567 | 568 | if evicted := cache.Add(3, struct{}{}); !evicted { 569 | t.Error("3: did not get expected eviction") 570 | } 571 | cache.wantKeys(t, []int{1, 3}) 572 | 573 | want := []int{2} 574 | if !reflect.DeepEqual(evictedKeys, want) { 575 | t.Errorf("evictedKeys got: %v want: %v", evictedKeys, want) 576 | } 577 | } 578 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/hashicorp/golang-lru/v2 2 | 3 | go 1.19 4 | -------------------------------------------------------------------------------- /internal/list.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE_list file. 4 | 5 | package internal 6 | 7 | import "time" 8 | 9 | // Entry is an LRU Entry 10 | type Entry[K comparable, V any] struct { 11 | // Next and previous pointers in the doubly-linked list of elements. 12 | // To simplify the implementation, internally a list l is implemented 13 | // as a ring, such that &l.root is both the next element of the last 14 | // list element (l.Back()) and the previous element of the first list 15 | // element (l.Front()). 16 | next, prev *Entry[K, V] 17 | 18 | // The list to which this element belongs. 19 | list *LruList[K, V] 20 | 21 | // The LRU Key of this element. 22 | Key K 23 | 24 | // The Value stored with this element. 25 | Value V 26 | 27 | // The time this element would be cleaned up, optional 28 | ExpiresAt time.Time 29 | 30 | // The expiry bucket item was put in, optional 31 | ExpireBucket uint8 32 | } 33 | 34 | // PrevEntry returns the previous list element or nil. 35 | func (e *Entry[K, V]) PrevEntry() *Entry[K, V] { 36 | if p := e.prev; e.list != nil && p != &e.list.root { 37 | return p 38 | } 39 | return nil 40 | } 41 | 42 | // LruList represents a doubly linked list. 43 | // The zero Value for LruList is an empty list ready to use. 44 | type LruList[K comparable, V any] struct { 45 | root Entry[K, V] // sentinel list element, only &root, root.prev, and root.next are used 46 | len int // current list Length excluding (this) sentinel element 47 | } 48 | 49 | // Init initializes or clears list l. 50 | func (l *LruList[K, V]) Init() *LruList[K, V] { 51 | l.root.next = &l.root 52 | l.root.prev = &l.root 53 | l.len = 0 54 | return l 55 | } 56 | 57 | // NewList returns an initialized list. 58 | func NewList[K comparable, V any]() *LruList[K, V] { return new(LruList[K, V]).Init() } 59 | 60 | // Length returns the number of elements of list l. 61 | // The complexity is O(1). 62 | func (l *LruList[K, V]) Length() int { return l.len } 63 | 64 | // Back returns the last element of list l or nil if the list is empty. 65 | func (l *LruList[K, V]) Back() *Entry[K, V] { 66 | if l.len == 0 { 67 | return nil 68 | } 69 | return l.root.prev 70 | } 71 | 72 | // lazyInit lazily initializes a zero List Value. 73 | func (l *LruList[K, V]) lazyInit() { 74 | if l.root.next == nil { 75 | l.Init() 76 | } 77 | } 78 | 79 | // insert inserts e after at, increments l.len, and returns e. 80 | func (l *LruList[K, V]) insert(e, at *Entry[K, V]) *Entry[K, V] { 81 | e.prev = at 82 | e.next = at.next 83 | e.prev.next = e 84 | e.next.prev = e 85 | e.list = l 86 | l.len++ 87 | return e 88 | } 89 | 90 | // insertValue is a convenience wrapper for insert(&Entry{Value: v, ExpiresAt: ExpiresAt}, at). 91 | func (l *LruList[K, V]) insertValue(k K, v V, expiresAt time.Time, at *Entry[K, V]) *Entry[K, V] { 92 | return l.insert(&Entry[K, V]{Value: v, Key: k, ExpiresAt: expiresAt}, at) 93 | } 94 | 95 | // Remove removes e from its list, decrements l.len 96 | func (l *LruList[K, V]) Remove(e *Entry[K, V]) V { 97 | e.prev.next = e.next 98 | e.next.prev = e.prev 99 | e.next = nil // avoid memory leaks 100 | e.prev = nil // avoid memory leaks 101 | e.list = nil 102 | l.len-- 103 | 104 | return e.Value 105 | } 106 | 107 | // move moves e to next to at. 108 | func (l *LruList[K, V]) move(e, at *Entry[K, V]) { 109 | if e == at { 110 | return 111 | } 112 | e.prev.next = e.next 113 | e.next.prev = e.prev 114 | 115 | e.prev = at 116 | e.next = at.next 117 | e.prev.next = e 118 | e.next.prev = e 119 | } 120 | 121 | // PushFront inserts a new element e with value v at the front of list l and returns e. 122 | func (l *LruList[K, V]) PushFront(k K, v V) *Entry[K, V] { 123 | l.lazyInit() 124 | return l.insertValue(k, v, time.Time{}, &l.root) 125 | } 126 | 127 | // PushFrontExpirable inserts a new expirable element e with Value v at the front of list l and returns e. 128 | func (l *LruList[K, V]) PushFrontExpirable(k K, v V, expiresAt time.Time) *Entry[K, V] { 129 | l.lazyInit() 130 | return l.insertValue(k, v, expiresAt, &l.root) 131 | } 132 | 133 | // MoveToFront moves element e to the front of list l. 134 | // If e is not an element of l, the list is not modified. 135 | // The element must not be nil. 136 | func (l *LruList[K, V]) MoveToFront(e *Entry[K, V]) { 137 | if e.list != l || l.root.next == e { 138 | return 139 | } 140 | // see comment in List.Remove about initialization of l 141 | l.move(e, &l.root) 142 | } 143 | -------------------------------------------------------------------------------- /lru.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package lru 5 | 6 | import ( 7 | "sync" 8 | 9 | "github.com/hashicorp/golang-lru/v2/simplelru" 10 | ) 11 | 12 | const ( 13 | // DefaultEvictedBufferSize defines the default buffer size to store evicted key/val 14 | DefaultEvictedBufferSize = 16 15 | ) 16 | 17 | // Cache is a thread-safe fixed size LRU cache. 18 | type Cache[K comparable, V any] struct { 19 | lru *simplelru.LRU[K, V] 20 | evictedKeys []K 21 | evictedVals []V 22 | onEvictedCB func(k K, v V) 23 | lock sync.RWMutex 24 | } 25 | 26 | // New creates an LRU of the given size. 27 | func New[K comparable, V any](size int) (*Cache[K, V], error) { 28 | return NewWithEvict[K, V](size, nil) 29 | } 30 | 31 | // NewWithEvict constructs a fixed size cache with the given eviction 32 | // callback. 33 | func NewWithEvict[K comparable, V any](size int, onEvicted func(key K, value V)) (c *Cache[K, V], err error) { 34 | // create a cache with default settings 35 | c = &Cache[K, V]{ 36 | onEvictedCB: onEvicted, 37 | } 38 | if onEvicted != nil { 39 | c.initEvictBuffers() 40 | onEvicted = c.onEvicted 41 | } 42 | c.lru, err = simplelru.NewLRU(size, onEvicted) 43 | return 44 | } 45 | 46 | func (c *Cache[K, V]) initEvictBuffers() { 47 | c.evictedKeys = make([]K, 0, DefaultEvictedBufferSize) 48 | c.evictedVals = make([]V, 0, DefaultEvictedBufferSize) 49 | } 50 | 51 | // onEvicted save evicted key/val and sent in externally registered callback 52 | // outside of critical section 53 | func (c *Cache[K, V]) onEvicted(k K, v V) { 54 | c.evictedKeys = append(c.evictedKeys, k) 55 | c.evictedVals = append(c.evictedVals, v) 56 | } 57 | 58 | // Purge is used to completely clear the cache. 59 | func (c *Cache[K, V]) Purge() { 60 | var ks []K 61 | var vs []V 62 | c.lock.Lock() 63 | c.lru.Purge() 64 | if c.onEvictedCB != nil && len(c.evictedKeys) > 0 { 65 | ks, vs = c.evictedKeys, c.evictedVals 66 | c.initEvictBuffers() 67 | } 68 | c.lock.Unlock() 69 | // invoke callback outside of critical section 70 | if c.onEvictedCB != nil { 71 | for i := 0; i < len(ks); i++ { 72 | c.onEvictedCB(ks[i], vs[i]) 73 | } 74 | } 75 | } 76 | 77 | // Add adds a value to the cache. Returns true if an eviction occurred. 78 | func (c *Cache[K, V]) Add(key K, value V) (evicted bool) { 79 | var k K 80 | var v V 81 | c.lock.Lock() 82 | evicted = c.lru.Add(key, value) 83 | if c.onEvictedCB != nil && evicted { 84 | k, v = c.evictedKeys[0], c.evictedVals[0] 85 | c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] 86 | } 87 | c.lock.Unlock() 88 | if c.onEvictedCB != nil && evicted { 89 | c.onEvictedCB(k, v) 90 | } 91 | return 92 | } 93 | 94 | // Get looks up a key's value from the cache. 95 | func (c *Cache[K, V]) Get(key K) (value V, ok bool) { 96 | c.lock.Lock() 97 | value, ok = c.lru.Get(key) 98 | c.lock.Unlock() 99 | return value, ok 100 | } 101 | 102 | // Contains checks if a key is in the cache, without updating the 103 | // recent-ness or deleting it for being stale. 104 | func (c *Cache[K, V]) Contains(key K) bool { 105 | c.lock.RLock() 106 | containKey := c.lru.Contains(key) 107 | c.lock.RUnlock() 108 | return containKey 109 | } 110 | 111 | // Peek returns the key value (or undefined if not found) without updating 112 | // the "recently used"-ness of the key. 113 | func (c *Cache[K, V]) Peek(key K) (value V, ok bool) { 114 | c.lock.RLock() 115 | value, ok = c.lru.Peek(key) 116 | c.lock.RUnlock() 117 | return value, ok 118 | } 119 | 120 | // ContainsOrAdd checks if a key is in the cache without updating the 121 | // recent-ness or deleting it for being stale, and if not, adds the value. 122 | // Returns whether found and whether an eviction occurred. 123 | func (c *Cache[K, V]) ContainsOrAdd(key K, value V) (ok, evicted bool) { 124 | var k K 125 | var v V 126 | c.lock.Lock() 127 | if c.lru.Contains(key) { 128 | c.lock.Unlock() 129 | return true, false 130 | } 131 | evicted = c.lru.Add(key, value) 132 | if c.onEvictedCB != nil && evicted { 133 | k, v = c.evictedKeys[0], c.evictedVals[0] 134 | c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] 135 | } 136 | c.lock.Unlock() 137 | if c.onEvictedCB != nil && evicted { 138 | c.onEvictedCB(k, v) 139 | } 140 | return false, evicted 141 | } 142 | 143 | // PeekOrAdd checks if a key is in the cache without updating the 144 | // recent-ness or deleting it for being stale, and if not, adds the value. 145 | // Returns whether found and whether an eviction occurred. 146 | func (c *Cache[K, V]) PeekOrAdd(key K, value V) (previous V, ok, evicted bool) { 147 | var k K 148 | var v V 149 | c.lock.Lock() 150 | previous, ok = c.lru.Peek(key) 151 | if ok { 152 | c.lock.Unlock() 153 | return previous, true, false 154 | } 155 | evicted = c.lru.Add(key, value) 156 | if c.onEvictedCB != nil && evicted { 157 | k, v = c.evictedKeys[0], c.evictedVals[0] 158 | c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] 159 | } 160 | c.lock.Unlock() 161 | if c.onEvictedCB != nil && evicted { 162 | c.onEvictedCB(k, v) 163 | } 164 | return 165 | } 166 | 167 | // Remove removes the provided key from the cache. 168 | func (c *Cache[K, V]) Remove(key K) (present bool) { 169 | var k K 170 | var v V 171 | c.lock.Lock() 172 | present = c.lru.Remove(key) 173 | if c.onEvictedCB != nil && present { 174 | k, v = c.evictedKeys[0], c.evictedVals[0] 175 | c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] 176 | } 177 | c.lock.Unlock() 178 | if c.onEvictedCB != nil && present { 179 | c.onEvictedCB(k, v) 180 | } 181 | return 182 | } 183 | 184 | // Resize changes the cache size. 185 | func (c *Cache[K, V]) Resize(size int) (evicted int) { 186 | var ks []K 187 | var vs []V 188 | c.lock.Lock() 189 | evicted = c.lru.Resize(size) 190 | if c.onEvictedCB != nil && evicted > 0 { 191 | ks, vs = c.evictedKeys, c.evictedVals 192 | c.initEvictBuffers() 193 | } 194 | c.lock.Unlock() 195 | if c.onEvictedCB != nil && evicted > 0 { 196 | for i := 0; i < len(ks); i++ { 197 | c.onEvictedCB(ks[i], vs[i]) 198 | } 199 | } 200 | return evicted 201 | } 202 | 203 | // RemoveOldest removes the oldest item from the cache. 204 | func (c *Cache[K, V]) RemoveOldest() (key K, value V, ok bool) { 205 | var k K 206 | var v V 207 | c.lock.Lock() 208 | key, value, ok = c.lru.RemoveOldest() 209 | if c.onEvictedCB != nil && ok { 210 | k, v = c.evictedKeys[0], c.evictedVals[0] 211 | c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] 212 | } 213 | c.lock.Unlock() 214 | if c.onEvictedCB != nil && ok { 215 | c.onEvictedCB(k, v) 216 | } 217 | return 218 | } 219 | 220 | // GetOldest returns the oldest entry 221 | func (c *Cache[K, V]) GetOldest() (key K, value V, ok bool) { 222 | c.lock.RLock() 223 | key, value, ok = c.lru.GetOldest() 224 | c.lock.RUnlock() 225 | return 226 | } 227 | 228 | // Keys returns a slice of the keys in the cache, from oldest to newest. 229 | func (c *Cache[K, V]) Keys() []K { 230 | c.lock.RLock() 231 | keys := c.lru.Keys() 232 | c.lock.RUnlock() 233 | return keys 234 | } 235 | 236 | // Values returns a slice of the values in the cache, from oldest to newest. 237 | func (c *Cache[K, V]) Values() []V { 238 | c.lock.RLock() 239 | values := c.lru.Values() 240 | c.lock.RUnlock() 241 | return values 242 | } 243 | 244 | // Len returns the number of items in the cache. 245 | func (c *Cache[K, V]) Len() int { 246 | c.lock.RLock() 247 | length := c.lru.Len() 248 | c.lock.RUnlock() 249 | return length 250 | } 251 | 252 | // Cap returns the capacity of the cache 253 | func (c *Cache[K, V]) Cap() int { 254 | return c.lru.Cap() 255 | } 256 | -------------------------------------------------------------------------------- /lru_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package lru 5 | 6 | import ( 7 | "reflect" 8 | "testing" 9 | ) 10 | 11 | func BenchmarkLRU_Rand(b *testing.B) { 12 | l, err := New[int64, int64](8192) 13 | if err != nil { 14 | b.Fatalf("err: %v", err) 15 | } 16 | 17 | trace := make([]int64, b.N*2) 18 | for i := 0; i < b.N*2; i++ { 19 | trace[i] = getRand(b) % 32768 20 | } 21 | 22 | b.ResetTimer() 23 | 24 | var hit, miss int 25 | for i := 0; i < 2*b.N; i++ { 26 | if i%2 == 0 { 27 | l.Add(trace[i], trace[i]) 28 | } else { 29 | if _, ok := l.Get(trace[i]); ok { 30 | hit++ 31 | } else { 32 | miss++ 33 | } 34 | } 35 | } 36 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss)) 37 | } 38 | 39 | func BenchmarkLRU_Freq(b *testing.B) { 40 | l, err := New[int64, int64](8192) 41 | if err != nil { 42 | b.Fatalf("err: %v", err) 43 | } 44 | 45 | trace := make([]int64, b.N*2) 46 | for i := 0; i < b.N*2; i++ { 47 | if i%2 == 0 { 48 | trace[i] = getRand(b) % 16384 49 | } else { 50 | trace[i] = getRand(b) % 32768 51 | } 52 | } 53 | 54 | b.ResetTimer() 55 | 56 | for i := 0; i < b.N; i++ { 57 | l.Add(trace[i], trace[i]) 58 | } 59 | var hit, miss int 60 | for i := 0; i < b.N; i++ { 61 | if _, ok := l.Get(trace[i]); ok { 62 | hit++ 63 | } else { 64 | miss++ 65 | } 66 | } 67 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss)) 68 | } 69 | 70 | func TestLRU(t *testing.T) { 71 | evictCounter := 0 72 | onEvicted := func(k int, v int) { 73 | if k != v { 74 | t.Fatalf("Evict values not equal (%v!=%v)", k, v) 75 | } 76 | evictCounter++ 77 | } 78 | l, err := NewWithEvict(128, onEvicted) 79 | if err != nil { 80 | t.Fatalf("err: %v", err) 81 | } 82 | 83 | for i := 0; i < 256; i++ { 84 | l.Add(i, i) 85 | } 86 | if l.Len() != 128 { 87 | t.Fatalf("bad len: %v", l.Len()) 88 | } 89 | if l.Cap() != 128 { 90 | t.Fatalf("expect %d, but %d", 128, l.Cap()) 91 | } 92 | 93 | if evictCounter != 128 { 94 | t.Fatalf("bad evict count: %v", evictCounter) 95 | } 96 | 97 | for i, k := range l.Keys() { 98 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 99 | t.Fatalf("bad key: %v", k) 100 | } 101 | } 102 | for i, v := range l.Values() { 103 | if v != i+128 { 104 | t.Fatalf("bad value: %v", v) 105 | } 106 | } 107 | for i := 0; i < 128; i++ { 108 | if _, ok := l.Get(i); ok { 109 | t.Fatalf("should be evicted") 110 | } 111 | } 112 | for i := 128; i < 256; i++ { 113 | if _, ok := l.Get(i); !ok { 114 | t.Fatalf("should not be evicted") 115 | } 116 | } 117 | for i := 128; i < 192; i++ { 118 | l.Remove(i) 119 | if _, ok := l.Get(i); ok { 120 | t.Fatalf("should be deleted") 121 | } 122 | } 123 | 124 | l.Get(192) // expect 192 to be last key in l.Keys() 125 | 126 | for i, k := range l.Keys() { 127 | if (i < 63 && k != i+193) || (i == 63 && k != 192) { 128 | t.Fatalf("out of order key: %v", k) 129 | } 130 | } 131 | 132 | l.Purge() 133 | if l.Len() != 0 { 134 | t.Fatalf("bad len: %v", l.Len()) 135 | } 136 | if _, ok := l.Get(200); ok { 137 | t.Fatalf("should contain nothing") 138 | } 139 | } 140 | 141 | // test that Add returns true/false if an eviction occurred 142 | func TestLRUAdd(t *testing.T) { 143 | evictCounter := 0 144 | onEvicted := func(k int, v int) { 145 | evictCounter++ 146 | } 147 | 148 | l, err := NewWithEvict(1, onEvicted) 149 | if err != nil { 150 | t.Fatalf("err: %v", err) 151 | } 152 | 153 | if l.Add(1, 1) == true || evictCounter != 0 { 154 | t.Errorf("should not have an eviction") 155 | } 156 | if l.Add(2, 2) == false || evictCounter != 1 { 157 | t.Errorf("should have an eviction") 158 | } 159 | } 160 | 161 | // test that Contains doesn't update recent-ness 162 | func TestLRUContains(t *testing.T) { 163 | l, err := New[int, int](2) 164 | if err != nil { 165 | t.Fatalf("err: %v", err) 166 | } 167 | 168 | l.Add(1, 1) 169 | l.Add(2, 2) 170 | if !l.Contains(1) { 171 | t.Errorf("1 should be contained") 172 | } 173 | 174 | l.Add(3, 3) 175 | if l.Contains(1) { 176 | t.Errorf("Contains should not have updated recent-ness of 1") 177 | } 178 | } 179 | 180 | // test that ContainsOrAdd doesn't update recent-ness 181 | func TestLRUContainsOrAdd(t *testing.T) { 182 | l, err := New[int, int](2) 183 | if err != nil { 184 | t.Fatalf("err: %v", err) 185 | } 186 | 187 | l.Add(1, 1) 188 | l.Add(2, 2) 189 | contains, evict := l.ContainsOrAdd(1, 1) 190 | if !contains { 191 | t.Errorf("1 should be contained") 192 | } 193 | if evict { 194 | t.Errorf("nothing should be evicted here") 195 | } 196 | 197 | l.Add(3, 3) 198 | contains, evict = l.ContainsOrAdd(1, 1) 199 | if contains { 200 | t.Errorf("1 should not have been contained") 201 | } 202 | if !evict { 203 | t.Errorf("an eviction should have occurred") 204 | } 205 | if !l.Contains(1) { 206 | t.Errorf("now 1 should be contained") 207 | } 208 | } 209 | 210 | // test that PeekOrAdd doesn't update recent-ness 211 | func TestLRUPeekOrAdd(t *testing.T) { 212 | l, err := New[int, int](2) 213 | if err != nil { 214 | t.Fatalf("err: %v", err) 215 | } 216 | 217 | l.Add(1, 1) 218 | l.Add(2, 2) 219 | previous, contains, evict := l.PeekOrAdd(1, 1) 220 | if !contains { 221 | t.Errorf("1 should be contained") 222 | } 223 | if evict { 224 | t.Errorf("nothing should be evicted here") 225 | } 226 | if previous != 1 { 227 | t.Errorf("previous is not equal to 1") 228 | } 229 | 230 | l.Add(3, 3) 231 | contains, evict = l.ContainsOrAdd(1, 1) 232 | if contains { 233 | t.Errorf("1 should not have been contained") 234 | } 235 | if !evict { 236 | t.Errorf("an eviction should have occurred") 237 | } 238 | if !l.Contains(1) { 239 | t.Errorf("now 1 should be contained") 240 | } 241 | } 242 | 243 | // test that Peek doesn't update recent-ness 244 | func TestLRUPeek(t *testing.T) { 245 | l, err := New[int, int](2) 246 | if err != nil { 247 | t.Fatalf("err: %v", err) 248 | } 249 | 250 | l.Add(1, 1) 251 | l.Add(2, 2) 252 | if v, ok := l.Peek(1); !ok || v != 1 { 253 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 254 | } 255 | 256 | l.Add(3, 3) 257 | if l.Contains(1) { 258 | t.Errorf("should not have updated recent-ness of 1") 259 | } 260 | } 261 | 262 | // test that Resize can upsize and downsize 263 | func TestLRUResize(t *testing.T) { 264 | onEvictCounter := 0 265 | onEvicted := func(k int, v int) { 266 | onEvictCounter++ 267 | } 268 | l, err := NewWithEvict(2, onEvicted) 269 | if err != nil { 270 | t.Fatalf("err: %v", err) 271 | } 272 | 273 | // Downsize 274 | l.Add(1, 1) 275 | l.Add(2, 2) 276 | evicted := l.Resize(1) 277 | if evicted != 1 { 278 | t.Errorf("1 element should have been evicted: %v", evicted) 279 | } 280 | if onEvictCounter != 1 { 281 | t.Errorf("onEvicted should have been called 1 time: %v", onEvictCounter) 282 | } 283 | 284 | l.Add(3, 3) 285 | if l.Contains(1) { 286 | t.Errorf("Element 1 should have been evicted") 287 | } 288 | 289 | // Upsize 290 | evicted = l.Resize(2) 291 | if evicted != 0 { 292 | t.Errorf("0 elements should have been evicted: %v", evicted) 293 | } 294 | 295 | l.Add(4, 4) 296 | if !l.Contains(3) || !l.Contains(4) { 297 | t.Errorf("Cache should have contained 2 elements") 298 | } 299 | } 300 | 301 | func (c *Cache[K, V]) wantKeys(t *testing.T, want []K) { 302 | t.Helper() 303 | got := c.Keys() 304 | if !reflect.DeepEqual(got, want) { 305 | t.Errorf("wrong keys got: %v, want: %v ", got, want) 306 | } 307 | } 308 | 309 | func TestCache_EvictionSameKey(t *testing.T) { 310 | t.Run("Add", func(t *testing.T) { 311 | var evictedKeys []int 312 | 313 | cache, _ := NewWithEvict( 314 | 2, 315 | func(key int, _ struct{}) { 316 | evictedKeys = append(evictedKeys, key) 317 | }) 318 | 319 | if evicted := cache.Add(1, struct{}{}); evicted { 320 | t.Error("First 1: got unexpected eviction") 321 | } 322 | cache.wantKeys(t, []int{1}) 323 | 324 | if evicted := cache.Add(2, struct{}{}); evicted { 325 | t.Error("2: got unexpected eviction") 326 | } 327 | cache.wantKeys(t, []int{1, 2}) 328 | 329 | if evicted := cache.Add(1, struct{}{}); evicted { 330 | t.Error("Second 1: got unexpected eviction") 331 | } 332 | cache.wantKeys(t, []int{2, 1}) 333 | 334 | if evicted := cache.Add(3, struct{}{}); !evicted { 335 | t.Error("3: did not get expected eviction") 336 | } 337 | cache.wantKeys(t, []int{1, 3}) 338 | 339 | want := []int{2} 340 | if !reflect.DeepEqual(evictedKeys, want) { 341 | t.Errorf("evictedKeys got: %v want: %v", evictedKeys, want) 342 | } 343 | }) 344 | 345 | t.Run("ContainsOrAdd", func(t *testing.T) { 346 | var evictedKeys []int 347 | 348 | cache, _ := NewWithEvict( 349 | 2, 350 | func(key int, _ struct{}) { 351 | evictedKeys = append(evictedKeys, key) 352 | }) 353 | 354 | contained, evicted := cache.ContainsOrAdd(1, struct{}{}) 355 | if contained { 356 | t.Error("First 1: got unexpected contained") 357 | } 358 | if evicted { 359 | t.Error("First 1: got unexpected eviction") 360 | } 361 | cache.wantKeys(t, []int{1}) 362 | 363 | contained, evicted = cache.ContainsOrAdd(2, struct{}{}) 364 | if contained { 365 | t.Error("2: got unexpected contained") 366 | } 367 | if evicted { 368 | t.Error("2: got unexpected eviction") 369 | } 370 | cache.wantKeys(t, []int{1, 2}) 371 | 372 | contained, evicted = cache.ContainsOrAdd(1, struct{}{}) 373 | if !contained { 374 | t.Error("Second 1: did not get expected contained") 375 | } 376 | if evicted { 377 | t.Error("Second 1: got unexpected eviction") 378 | } 379 | cache.wantKeys(t, []int{1, 2}) 380 | 381 | contained, evicted = cache.ContainsOrAdd(3, struct{}{}) 382 | if contained { 383 | t.Error("3: got unexpected contained") 384 | } 385 | if !evicted { 386 | t.Error("3: did not get expected eviction") 387 | } 388 | cache.wantKeys(t, []int{2, 3}) 389 | 390 | want := []int{1} 391 | if !reflect.DeepEqual(evictedKeys, want) { 392 | t.Errorf("evictedKeys got: %v want: %v", evictedKeys, want) 393 | } 394 | }) 395 | 396 | t.Run("PeekOrAdd", func(t *testing.T) { 397 | var evictedKeys []int 398 | 399 | cache, _ := NewWithEvict( 400 | 2, 401 | func(key int, _ struct{}) { 402 | evictedKeys = append(evictedKeys, key) 403 | }) 404 | 405 | _, contained, evicted := cache.PeekOrAdd(1, struct{}{}) 406 | if contained { 407 | t.Error("First 1: got unexpected contained") 408 | } 409 | if evicted { 410 | t.Error("First 1: got unexpected eviction") 411 | } 412 | cache.wantKeys(t, []int{1}) 413 | 414 | _, contained, evicted = cache.PeekOrAdd(2, struct{}{}) 415 | if contained { 416 | t.Error("2: got unexpected contained") 417 | } 418 | if evicted { 419 | t.Error("2: got unexpected eviction") 420 | } 421 | cache.wantKeys(t, []int{1, 2}) 422 | 423 | _, contained, evicted = cache.PeekOrAdd(1, struct{}{}) 424 | if !contained { 425 | t.Error("Second 1: did not get expected contained") 426 | } 427 | if evicted { 428 | t.Error("Second 1: got unexpected eviction") 429 | } 430 | cache.wantKeys(t, []int{1, 2}) 431 | 432 | _, contained, evicted = cache.PeekOrAdd(3, struct{}{}) 433 | if contained { 434 | t.Error("3: got unexpected contained") 435 | } 436 | if !evicted { 437 | t.Error("3: did not get expected eviction") 438 | } 439 | cache.wantKeys(t, []int{2, 3}) 440 | 441 | want := []int{1} 442 | if !reflect.DeepEqual(evictedKeys, want) { 443 | t.Errorf("evictedKeys got: %v want: %v", evictedKeys, want) 444 | } 445 | }) 446 | } 447 | -------------------------------------------------------------------------------- /simplelru/LICENSE_list: -------------------------------------------------------------------------------- 1 | This license applies to simplelru/list.go 2 | 3 | Copyright (c) 2009 The Go Authors. All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are 7 | met: 8 | 9 | * Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above 12 | copyright notice, this list of conditions and the following disclaimer 13 | in the documentation and/or other materials provided with the 14 | distribution. 15 | * Neither the name of Google Inc. nor the names of its 16 | contributors may be used to endorse or promote products derived from 17 | this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /simplelru/lru.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package simplelru 5 | 6 | import ( 7 | "errors" 8 | 9 | "github.com/hashicorp/golang-lru/v2/internal" 10 | ) 11 | 12 | // EvictCallback is used to get a callback when a cache entry is evicted 13 | type EvictCallback[K comparable, V any] func(key K, value V) 14 | 15 | // LRU implements a non-thread safe fixed size LRU cache 16 | type LRU[K comparable, V any] struct { 17 | size int 18 | evictList *internal.LruList[K, V] 19 | items map[K]*internal.Entry[K, V] 20 | onEvict EvictCallback[K, V] 21 | } 22 | 23 | // NewLRU constructs an LRU of the given size 24 | func NewLRU[K comparable, V any](size int, onEvict EvictCallback[K, V]) (*LRU[K, V], error) { 25 | if size <= 0 { 26 | return nil, errors.New("must provide a positive size") 27 | } 28 | 29 | c := &LRU[K, V]{ 30 | size: size, 31 | evictList: internal.NewList[K, V](), 32 | items: make(map[K]*internal.Entry[K, V]), 33 | onEvict: onEvict, 34 | } 35 | return c, nil 36 | } 37 | 38 | // Purge is used to completely clear the cache. 39 | func (c *LRU[K, V]) Purge() { 40 | for k, v := range c.items { 41 | if c.onEvict != nil { 42 | c.onEvict(k, v.Value) 43 | } 44 | delete(c.items, k) 45 | } 46 | c.evictList.Init() 47 | } 48 | 49 | // Add adds a value to the cache. Returns true if an eviction occurred. 50 | func (c *LRU[K, V]) Add(key K, value V) (evicted bool) { 51 | // Check for existing item 52 | if ent, ok := c.items[key]; ok { 53 | c.evictList.MoveToFront(ent) 54 | ent.Value = value 55 | return false 56 | } 57 | 58 | // Add new item 59 | ent := c.evictList.PushFront(key, value) 60 | c.items[key] = ent 61 | 62 | evict := c.evictList.Length() > c.size 63 | // Verify size not exceeded 64 | if evict { 65 | c.removeOldest() 66 | } 67 | return evict 68 | } 69 | 70 | // Get looks up a key's value from the cache. 71 | func (c *LRU[K, V]) Get(key K) (value V, ok bool) { 72 | if ent, ok := c.items[key]; ok { 73 | c.evictList.MoveToFront(ent) 74 | return ent.Value, true 75 | } 76 | return 77 | } 78 | 79 | // Contains checks if a key is in the cache, without updating the recent-ness 80 | // or deleting it for being stale. 81 | func (c *LRU[K, V]) Contains(key K) (ok bool) { 82 | _, ok = c.items[key] 83 | return ok 84 | } 85 | 86 | // Peek returns the key value (or undefined if not found) without updating 87 | // the "recently used"-ness of the key. 88 | func (c *LRU[K, V]) Peek(key K) (value V, ok bool) { 89 | var ent *internal.Entry[K, V] 90 | if ent, ok = c.items[key]; ok { 91 | return ent.Value, true 92 | } 93 | return 94 | } 95 | 96 | // Remove removes the provided key from the cache, returning if the 97 | // key was contained. 98 | func (c *LRU[K, V]) Remove(key K) (present bool) { 99 | if ent, ok := c.items[key]; ok { 100 | c.removeElement(ent) 101 | return true 102 | } 103 | return false 104 | } 105 | 106 | // RemoveOldest removes the oldest item from the cache. 107 | func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool) { 108 | if ent := c.evictList.Back(); ent != nil { 109 | c.removeElement(ent) 110 | return ent.Key, ent.Value, true 111 | } 112 | return 113 | } 114 | 115 | // GetOldest returns the oldest entry 116 | func (c *LRU[K, V]) GetOldest() (key K, value V, ok bool) { 117 | if ent := c.evictList.Back(); ent != nil { 118 | return ent.Key, ent.Value, true 119 | } 120 | return 121 | } 122 | 123 | // Keys returns a slice of the keys in the cache, from oldest to newest. 124 | func (c *LRU[K, V]) Keys() []K { 125 | keys := make([]K, c.evictList.Length()) 126 | i := 0 127 | for ent := c.evictList.Back(); ent != nil; ent = ent.PrevEntry() { 128 | keys[i] = ent.Key 129 | i++ 130 | } 131 | return keys 132 | } 133 | 134 | // Values returns a slice of the values in the cache, from oldest to newest. 135 | func (c *LRU[K, V]) Values() []V { 136 | values := make([]V, len(c.items)) 137 | i := 0 138 | for ent := c.evictList.Back(); ent != nil; ent = ent.PrevEntry() { 139 | values[i] = ent.Value 140 | i++ 141 | } 142 | return values 143 | } 144 | 145 | // Len returns the number of items in the cache. 146 | func (c *LRU[K, V]) Len() int { 147 | return c.evictList.Length() 148 | } 149 | 150 | // Cap returns the capacity of the cache 151 | func (c *LRU[K, V]) Cap() int { 152 | return c.size 153 | } 154 | 155 | // Resize changes the cache size. 156 | func (c *LRU[K, V]) Resize(size int) (evicted int) { 157 | diff := c.Len() - size 158 | if diff < 0 { 159 | diff = 0 160 | } 161 | for i := 0; i < diff; i++ { 162 | c.removeOldest() 163 | } 164 | c.size = size 165 | return diff 166 | } 167 | 168 | // removeOldest removes the oldest item from the cache. 169 | func (c *LRU[K, V]) removeOldest() { 170 | if ent := c.evictList.Back(); ent != nil { 171 | c.removeElement(ent) 172 | } 173 | } 174 | 175 | // removeElement is used to remove a given list element from the cache 176 | func (c *LRU[K, V]) removeElement(e *internal.Entry[K, V]) { 177 | c.evictList.Remove(e) 178 | delete(c.items, e.Key) 179 | if c.onEvict != nil { 180 | c.onEvict(e.Key, e.Value) 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /simplelru/lru_interface.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | // Package simplelru provides simple LRU implementation based on build-in container/list. 5 | package simplelru 6 | 7 | // LRUCache is the interface for simple LRU cache. 8 | type LRUCache[K comparable, V any] interface { 9 | // Adds a value to the cache, returns true if an eviction occurred and 10 | // updates the "recently used"-ness of the key. 11 | Add(key K, value V) bool 12 | 13 | // Returns key's value from the cache and 14 | // updates the "recently used"-ness of the key. #value, isFound 15 | Get(key K) (value V, ok bool) 16 | 17 | // Checks if a key exists in cache without updating the recent-ness. 18 | Contains(key K) (ok bool) 19 | 20 | // Returns key's value without updating the "recently used"-ness of the key. 21 | Peek(key K) (value V, ok bool) 22 | 23 | // Removes a key from the cache. 24 | Remove(key K) bool 25 | 26 | // Removes the oldest entry from cache. 27 | RemoveOldest() (K, V, bool) 28 | 29 | // Returns the oldest entry from the cache. #key, value, isFound 30 | GetOldest() (K, V, bool) 31 | 32 | // Returns a slice of the keys in the cache, from oldest to newest. 33 | Keys() []K 34 | 35 | // Values returns a slice of the values in the cache, from oldest to newest. 36 | Values() []V 37 | 38 | // Returns the number of items in the cache. 39 | Len() int 40 | 41 | // Returns the capacity of the cache. 42 | Cap() int 43 | 44 | // Clears all cache entries. 45 | Purge() 46 | 47 | // Resizes cache, returning number evicted 48 | Resize(int) int 49 | } 50 | -------------------------------------------------------------------------------- /simplelru/lru_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package simplelru 5 | 6 | import ( 7 | "reflect" 8 | "testing" 9 | ) 10 | 11 | func TestLRU(t *testing.T) { 12 | evictCounter := 0 13 | onEvicted := func(k int, v int) { 14 | if k != v { 15 | t.Fatalf("Evict values not equal (%v!=%v)", k, v) 16 | } 17 | evictCounter++ 18 | } 19 | l, err := NewLRU(128, onEvicted) 20 | if err != nil { 21 | t.Fatalf("err: %v", err) 22 | } 23 | 24 | for i := 0; i < 256; i++ { 25 | l.Add(i, i) 26 | } 27 | if l.Len() != 128 { 28 | t.Fatalf("bad len: %v", l.Len()) 29 | } 30 | if l.Cap() != 128 { 31 | t.Fatalf("expect %d, but %d", 128, l.Cap()) 32 | } 33 | 34 | if evictCounter != 128 { 35 | t.Fatalf("bad evict count: %v", evictCounter) 36 | } 37 | 38 | for i, k := range l.Keys() { 39 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 40 | t.Fatalf("bad key: %v", k) 41 | } 42 | } 43 | for i, v := range l.Values() { 44 | if v != i+128 { 45 | t.Fatalf("bad value: %v", v) 46 | } 47 | } 48 | for i := 0; i < 128; i++ { 49 | if _, ok := l.Get(i); ok { 50 | t.Fatalf("should be evicted") 51 | } 52 | } 53 | for i := 128; i < 256; i++ { 54 | if _, ok := l.Get(i); !ok { 55 | t.Fatalf("should not be evicted") 56 | } 57 | } 58 | for i := 128; i < 192; i++ { 59 | if ok := l.Remove(i); !ok { 60 | t.Fatalf("should be contained") 61 | } 62 | if ok := l.Remove(i); ok { 63 | t.Fatalf("should not be contained") 64 | } 65 | if _, ok := l.Get(i); ok { 66 | t.Fatalf("should be deleted") 67 | } 68 | } 69 | 70 | l.Get(192) // expect 192 to be last key in l.Keys() 71 | 72 | for i, k := range l.Keys() { 73 | if (i < 63 && k != i+193) || (i == 63 && k != 192) { 74 | t.Fatalf("out of order key: %v", k) 75 | } 76 | } 77 | 78 | l.Purge() 79 | if l.Len() != 0 { 80 | t.Fatalf("bad len: %v", l.Len()) 81 | } 82 | if _, ok := l.Get(200); ok { 83 | t.Fatalf("should contain nothing") 84 | } 85 | } 86 | 87 | func TestLRU_GetOldest_RemoveOldest(t *testing.T) { 88 | l, err := NewLRU[int, int](128, nil) 89 | if err != nil { 90 | t.Fatalf("err: %v", err) 91 | } 92 | for i := 0; i < 256; i++ { 93 | l.Add(i, i) 94 | } 95 | k, _, ok := l.GetOldest() 96 | if !ok { 97 | t.Fatalf("missing") 98 | } 99 | if k != 128 { 100 | t.Fatalf("bad: %v", k) 101 | } 102 | 103 | k, _, ok = l.RemoveOldest() 104 | if !ok { 105 | t.Fatalf("missing") 106 | } 107 | if k != 128 { 108 | t.Fatalf("bad: %v", k) 109 | } 110 | 111 | k, _, ok = l.RemoveOldest() 112 | if !ok { 113 | t.Fatalf("missing") 114 | } 115 | if k != 129 { 116 | t.Fatalf("bad: %v", k) 117 | } 118 | } 119 | 120 | // Test that Add returns true/false if an eviction occurred 121 | func TestLRU_Add(t *testing.T) { 122 | evictCounter := 0 123 | onEvicted := func(k int, v int) { 124 | evictCounter++ 125 | } 126 | 127 | l, err := NewLRU(1, onEvicted) 128 | if err != nil { 129 | t.Fatalf("err: %v", err) 130 | } 131 | 132 | if l.Add(1, 1) == true || evictCounter != 0 { 133 | t.Errorf("should not have an eviction") 134 | } 135 | if l.Add(2, 2) == false || evictCounter != 1 { 136 | t.Errorf("should have an eviction") 137 | } 138 | } 139 | 140 | // Test that Contains doesn't update recent-ness 141 | func TestLRU_Contains(t *testing.T) { 142 | l, err := NewLRU[int, int](2, nil) 143 | if err != nil { 144 | t.Fatalf("err: %v", err) 145 | } 146 | 147 | l.Add(1, 1) 148 | l.Add(2, 2) 149 | if !l.Contains(1) { 150 | t.Errorf("1 should be contained") 151 | } 152 | 153 | l.Add(3, 3) 154 | if l.Contains(1) { 155 | t.Errorf("Contains should not have updated recent-ness of 1") 156 | } 157 | } 158 | 159 | // Test that Peek doesn't update recent-ness 160 | func TestLRU_Peek(t *testing.T) { 161 | l, err := NewLRU[int, int](2, nil) 162 | if err != nil { 163 | t.Fatalf("err: %v", err) 164 | } 165 | 166 | l.Add(1, 1) 167 | l.Add(2, 2) 168 | if v, ok := l.Peek(1); !ok || v != 1 { 169 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 170 | } 171 | 172 | l.Add(3, 3) 173 | if l.Contains(1) { 174 | t.Errorf("should not have updated recent-ness of 1") 175 | } 176 | } 177 | 178 | // Test that Resize can upsize and downsize 179 | func TestLRU_Resize(t *testing.T) { 180 | onEvictCounter := 0 181 | onEvicted := func(k int, v int) { 182 | onEvictCounter++ 183 | } 184 | l, err := NewLRU(2, onEvicted) 185 | if err != nil { 186 | t.Fatalf("err: %v", err) 187 | } 188 | 189 | // Downsize 190 | l.Add(1, 1) 191 | l.Add(2, 2) 192 | evicted := l.Resize(1) 193 | if evicted != 1 { 194 | t.Errorf("1 element should have been evicted: %v", evicted) 195 | } 196 | if onEvictCounter != 1 { 197 | t.Errorf("onEvicted should have been called 1 time: %v", onEvictCounter) 198 | } 199 | 200 | l.Add(3, 3) 201 | if l.Contains(1) { 202 | t.Errorf("Element 1 should have been evicted") 203 | } 204 | 205 | // Upsize 206 | evicted = l.Resize(2) 207 | if evicted != 0 { 208 | t.Errorf("0 elements should have been evicted: %v", evicted) 209 | } 210 | 211 | l.Add(4, 4) 212 | if !l.Contains(3) || !l.Contains(4) { 213 | t.Errorf("Cache should have contained 2 elements") 214 | } 215 | } 216 | 217 | func (c *LRU[K, V]) wantKeys(t *testing.T, want []K) { 218 | t.Helper() 219 | got := c.Keys() 220 | if !reflect.DeepEqual(got, want) { 221 | t.Errorf("wrong keys got: %v, want: %v ", got, want) 222 | } 223 | } 224 | 225 | func TestCache_EvictionSameKey(t *testing.T) { 226 | var evictedKeys []int 227 | 228 | cache, _ := NewLRU( 229 | 2, 230 | func(key int, _ struct{}) { 231 | evictedKeys = append(evictedKeys, key) 232 | }) 233 | 234 | if evicted := cache.Add(1, struct{}{}); evicted { 235 | t.Error("First 1: got unexpected eviction") 236 | } 237 | cache.wantKeys(t, []int{1}) 238 | 239 | if evicted := cache.Add(2, struct{}{}); evicted { 240 | t.Error("2: got unexpected eviction") 241 | } 242 | cache.wantKeys(t, []int{1, 2}) 243 | 244 | if evicted := cache.Add(1, struct{}{}); evicted { 245 | t.Error("Second 1: got unexpected eviction") 246 | } 247 | cache.wantKeys(t, []int{2, 1}) 248 | 249 | if evicted := cache.Add(3, struct{}{}); !evicted { 250 | t.Error("3: did not get expected eviction") 251 | } 252 | cache.wantKeys(t, []int{1, 3}) 253 | 254 | want := []int{2} 255 | if !reflect.DeepEqual(evictedKeys, want) { 256 | t.Errorf("evictedKeys got: %v want: %v", evictedKeys, want) 257 | } 258 | } 259 | -------------------------------------------------------------------------------- /testing_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) HashiCorp, Inc. 2 | // SPDX-License-Identifier: MPL-2.0 3 | 4 | package lru 5 | 6 | import ( 7 | "crypto/rand" 8 | "math" 9 | "math/big" 10 | "testing" 11 | ) 12 | 13 | func getRand(tb testing.TB) int64 { 14 | out, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64)) 15 | if err != nil { 16 | tb.Fatal(err) 17 | } 18 | return out.Int64() 19 | } 20 | --------------------------------------------------------------------------------