├── go.mod ├── .gitignore ├── README.md ├── .golangci.yml ├── .github └── workflows │ └── ci.yml ├── doc.go ├── simplelru ├── lru_interface.go ├── lru_test.go └── lru.go ├── 2q.go ├── 2q_test.go ├── arc.go ├── lru_test.go ├── lru.go ├── arc_test.go └── LICENSE /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/descope/golang-lru 2 | 3 | go 1.18 4 | 5 | require github.com/bahlo/generic-list-go v0.2.0 // indirect 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Mac OS X files 2 | .DS_Store 3 | 4 | # IDE files 5 | .idea/ 6 | .vscode/ 7 | .history/ 8 | *.vsix 9 | .ionide 10 | 11 | # Binaries for programs and plugins 12 | *.exe 13 | *.exe~ 14 | *.dll 15 | *.so 16 | *.dylib 17 | 18 | # Test binary, build with `go test -c` 19 | *.test 20 | 21 | # Output of the go coverage tool, specifically when used with LiteIDE 22 | *.out 23 | coverage.html 24 | 25 | # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 26 | .glide/ 27 | 28 | # Dependency directories 29 | vendor/ 30 | 31 | __debug_bin 32 | -------------------------------------------------------------------------------- /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 [Godoc](http://godoc.org/github.com/hashicorp/golang-lru) 11 | 12 | Example 13 | ======= 14 | 15 | Using the LRU is very simple: 16 | 17 | ```go 18 | l, _ := New(128) 19 | for i := 0; i < 256; i++ { 20 | l.Add(i, nil) 21 | } 22 | if l.Len() != 128 { 23 | panic(fmt.Sprintf("bad len: %v", l.Len())) 24 | } 25 | ``` 26 | -------------------------------------------------------------------------------- /.golangci.yml: -------------------------------------------------------------------------------- 1 | linters: 2 | enable: 3 | - megacheck 4 | - golint 5 | - govet 6 | - unconvert 7 | - megacheck 8 | - structcheck 9 | - gas 10 | - gocyclo 11 | - dupl 12 | - misspell 13 | - unparam 14 | - varcheck 15 | - deadcode 16 | - typecheck 17 | - ineffassign 18 | - varcheck 19 | - stylecheck 20 | - scopelint 21 | - gocritic 22 | - nakedret 23 | - gosimple 24 | - prealloc 25 | fast: false 26 | disable-all: true 27 | 28 | issues: 29 | exclude-rules: 30 | - path: _test\.go 31 | linters: 32 | - dupl 33 | exclude-use-default: false 34 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: 6 | tags: 7 | pull_request: 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - name: set up go 1.14 15 | uses: actions/setup-go@v1 16 | with: 17 | go-version: 1.14 18 | id: go 19 | 20 | - name: checkout 21 | uses: actions/checkout@v2 22 | 23 | - name: build and test 24 | run: | 25 | go test -timeout=60s -race 26 | go build -race 27 | 28 | - name: install golangci-lint 29 | run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $GITHUB_WORKSPACE v1.26.0 30 | 31 | - name: run golangci-lint 32 | run: $GITHUB_WORKSPACE/golangci-lint run --out-format=github-actions 33 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | // Package lru provides three different LRU caches of varying sophistication. 2 | // 3 | // Cache is a simple LRU cache. It is based on the 4 | // LRU implementation in groupcache: 5 | // https://github.com/golang/groupcache/tree/master/lru 6 | // 7 | // TwoQueueCache tracks frequently used and recently used entries separately. 8 | // This avoids a burst of accesses from taking out frequently used entries, 9 | // at the cost of about 2x computational overhead and some extra bookkeeping. 10 | // 11 | // ARCCache is an adaptive replacement cache. It tracks recent evictions as 12 | // well as recent usage in both the frequent and recent caches. Its 13 | // computational overhead is comparable to TwoQueueCache, but the memory 14 | // overhead is linear with the size of the cache. 15 | // 16 | // ARC has been patented by IBM, so do not use it if that is problematic for 17 | // your program. 18 | // 19 | // All caches in this package take locks while operating, and are therefore 20 | // thread-safe for consumers. 21 | package lru 22 | -------------------------------------------------------------------------------- /simplelru/lru_interface.go: -------------------------------------------------------------------------------- 1 | // Package simplelru provides simple LRU implementation based on build-in container/list. 2 | package simplelru 3 | 4 | // LRUCache is the interface for simple LRU cache. 5 | type LRUCache[K comparable, T any] interface { 6 | // Adds a value to the cache, returns true if an eviction occurred and 7 | // updates the "recently used"-ness of the key. 8 | Add(key K, value T) bool 9 | 10 | // Returns key's value from the cache and 11 | // updates the "recently used"-ness of the key. #value, isFound 12 | Get(key K) (value T, ok bool) 13 | 14 | // Checks if a key exists in cache without updating the recent-ness. 15 | Contains(key K) (ok bool) 16 | 17 | // Returns key's value without updating the "recently used"-ness of the key. 18 | Peek(key K) (value T, ok bool) 19 | 20 | // Removes a key from the cache. 21 | Remove(key K) bool 22 | 23 | // Removes the oldest entry from cache. 24 | RemoveOldest() (K, T, bool) 25 | 26 | // Returns the oldest entry from the cache. #key, value, isFound 27 | GetOldest() (K, T, bool) 28 | 29 | // Returns a slice of the keys in the cache, from oldest to newest. 30 | Keys() []K 31 | 32 | // Returns the number of items in the cache. 33 | Len() int 34 | 35 | // Clears all cache entries. 36 | Purge() 37 | 38 | // Resizes cache, returning number evicted 39 | Resize(int) int 40 | } 41 | -------------------------------------------------------------------------------- /simplelru/lru_test.go: -------------------------------------------------------------------------------- 1 | package simplelru 2 | 3 | import "testing" 4 | 5 | func TestLRU(t *testing.T) { 6 | evictCounter := 0 7 | onEvicted := func(k int, v int) { 8 | if k != v { 9 | t.Fatalf("Evict values not equal (%v!=%v)", k, v) 10 | } 11 | evictCounter++ 12 | } 13 | l, err := NewLRU[int, int](128, onEvicted) 14 | if err != nil { 15 | t.Fatalf("err: %v", err) 16 | } 17 | 18 | for i := 0; i < 256; i++ { 19 | l.Add(i, i) 20 | } 21 | if l.Len() != 128 { 22 | t.Fatalf("bad len: %v", l.Len()) 23 | } 24 | 25 | if evictCounter != 128 { 26 | t.Fatalf("bad evict count: %v", evictCounter) 27 | } 28 | 29 | for i, k := range l.Keys() { 30 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 31 | t.Fatalf("bad key: %v", k) 32 | } 33 | } 34 | for i := 0; i < 128; i++ { 35 | _, ok := l.Get(i) 36 | if ok { 37 | t.Fatalf("should be evicted") 38 | } 39 | } 40 | for i := 128; i < 256; i++ { 41 | _, ok := l.Get(i) 42 | if !ok { 43 | t.Fatalf("should not be evicted") 44 | } 45 | } 46 | for i := 128; i < 192; i++ { 47 | ok := l.Remove(i) 48 | if !ok { 49 | t.Fatalf("should be contained") 50 | } 51 | ok = l.Remove(i) 52 | if ok { 53 | t.Fatalf("should not be contained") 54 | } 55 | _, ok = l.Get(i) 56 | if ok { 57 | t.Fatalf("should be deleted") 58 | } 59 | } 60 | 61 | l.Get(192) // expect 192 to be last key in l.Keys() 62 | 63 | for i, k := range l.Keys() { 64 | if (i < 63 && k != i+193) || (i == 63 && k != 192) { 65 | t.Fatalf("out of order key: %v", k) 66 | } 67 | } 68 | 69 | l.Purge() 70 | if l.Len() != 0 { 71 | t.Fatalf("bad len: %v", l.Len()) 72 | } 73 | if _, ok := l.Get(200); ok { 74 | t.Fatalf("should contain nothing") 75 | } 76 | } 77 | 78 | func TestLRU_GetOldest_RemoveOldest(t *testing.T) { 79 | l, err := NewLRU[int, int](128, nil) 80 | if err != nil { 81 | t.Fatalf("err: %v", err) 82 | } 83 | for i := 0; i < 256; i++ { 84 | l.Add(i, i) 85 | } 86 | k, _, ok := l.GetOldest() 87 | if !ok { 88 | t.Fatalf("missing") 89 | } 90 | if k != 128 { 91 | t.Fatalf("bad: %v", k) 92 | } 93 | 94 | k, _, ok = l.RemoveOldest() 95 | if !ok { 96 | t.Fatalf("missing") 97 | } 98 | if k != 128 { 99 | t.Fatalf("bad: %v", k) 100 | } 101 | 102 | k, _, ok = l.RemoveOldest() 103 | if !ok { 104 | t.Fatalf("missing") 105 | } 106 | if k != 129 { 107 | t.Fatalf("bad: %v", k) 108 | } 109 | } 110 | 111 | // Test that Add returns true/false if an eviction occurred 112 | func TestLRU_Add(t *testing.T) { 113 | evictCounter := 0 114 | onEvicted := func(k int, v int) { 115 | evictCounter++ 116 | } 117 | 118 | l, err := NewLRU[int, int](1, onEvicted) 119 | if err != nil { 120 | t.Fatalf("err: %v", err) 121 | } 122 | 123 | if l.Add(1, 1) == true || evictCounter != 0 { 124 | t.Errorf("should not have an eviction") 125 | } 126 | if l.Add(2, 2) == false || evictCounter != 1 { 127 | t.Errorf("should have an eviction") 128 | } 129 | } 130 | 131 | // Test that Contains doesn't update recent-ness 132 | func TestLRU_Contains(t *testing.T) { 133 | l, err := NewLRU[int, int](2, nil) 134 | if err != nil { 135 | t.Fatalf("err: %v", err) 136 | } 137 | 138 | l.Add(1, 1) 139 | l.Add(2, 2) 140 | if !l.Contains(1) { 141 | t.Errorf("1 should be contained") 142 | } 143 | 144 | l.Add(3, 3) 145 | if l.Contains(1) { 146 | t.Errorf("Contains should not have updated recent-ness of 1") 147 | } 148 | } 149 | 150 | // Test that Peek doesn't update recent-ness 151 | func TestLRU_Peek(t *testing.T) { 152 | l, err := NewLRU[int, int](2, nil) 153 | if err != nil { 154 | t.Fatalf("err: %v", err) 155 | } 156 | 157 | l.Add(1, 1) 158 | l.Add(2, 2) 159 | if v, ok := l.Peek(1); !ok || v != 1 { 160 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 161 | } 162 | 163 | l.Add(3, 3) 164 | if l.Contains(1) { 165 | t.Errorf("should not have updated recent-ness of 1") 166 | } 167 | } 168 | 169 | // Test that Resize can upsize and downsize 170 | func TestLRU_Resize(t *testing.T) { 171 | onEvictCounter := 0 172 | onEvicted := func(k int, v int) { 173 | onEvictCounter++ 174 | } 175 | l, err := NewLRU[int, int](2, onEvicted) 176 | if err != nil { 177 | t.Fatalf("err: %v", err) 178 | } 179 | 180 | // Downsize 181 | l.Add(1, 1) 182 | l.Add(2, 2) 183 | evicted := l.Resize(1) 184 | if evicted != 1 { 185 | t.Errorf("1 element should have been evicted: %v", evicted) 186 | } 187 | if onEvictCounter != 1 { 188 | t.Errorf("onEvicted should have been called 1 time: %v", onEvictCounter) 189 | } 190 | 191 | l.Add(3, 3) 192 | if l.Contains(1) { 193 | t.Errorf("Element 1 should have been evicted") 194 | } 195 | 196 | // Upsize 197 | evicted = l.Resize(2) 198 | if evicted != 0 { 199 | t.Errorf("0 elements should have been evicted: %v", evicted) 200 | } 201 | 202 | l.Add(4, 4) 203 | if !l.Contains(3) || !l.Contains(4) { 204 | t.Errorf("Cache should have contained 2 elements") 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /simplelru/lru.go: -------------------------------------------------------------------------------- 1 | package simplelru 2 | 3 | import ( 4 | "errors" 5 | 6 | list "github.com/bahlo/generic-list-go" 7 | ) 8 | 9 | // LRU implements a non-thread safe fixed size LRU cache 10 | type LRU[K comparable, T any] struct { 11 | size int 12 | evictList *list.List[*entry[K, T]] 13 | items map[K]*list.Element[*entry[K, T]] 14 | onEvict func(key K, value T) 15 | } 16 | 17 | // entry is used to hold a value in the evictList 18 | type entry[K comparable, T any] struct { 19 | key K 20 | value T 21 | } 22 | 23 | // NewLRU constructs an LRU of the given size 24 | func NewLRU[K comparable, T any](size int, onEvict func(key K, value T)) (*LRU[K, T], error) { 25 | if size <= 0 { 26 | return nil, errors.New("must provide a positive size") 27 | } 28 | c := &LRU[K, T]{ 29 | size: size, 30 | evictList: list.New[*entry[K, T]](), 31 | items: make(map[K]*list.Element[*entry[K, T]]), 32 | onEvict: onEvict, 33 | } 34 | return c, nil 35 | } 36 | 37 | // Purge is used to completely clear the cache. 38 | func (c *LRU[K, T]) Purge() { 39 | for k, v := range c.items { 40 | if c.onEvict != nil { 41 | c.onEvict(k, v.Value.value) 42 | } 43 | delete(c.items, k) 44 | } 45 | c.evictList.Init() 46 | } 47 | 48 | // Add adds a value to the cache. Returns true if an eviction occurred. 49 | func (c *LRU[K, T]) Add(key K, value T) (evicted bool) { 50 | // Check for existing item 51 | if ent, ok := c.items[key]; ok { 52 | c.evictList.MoveToFront(ent) 53 | ent.Value.value = value 54 | return false 55 | } 56 | 57 | // Add new item 58 | ent := &entry[K, T]{key, value} 59 | entry := c.evictList.PushFront(ent) 60 | c.items[key] = entry 61 | 62 | evict := c.evictList.Len() > 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, T]) Get(key K) (value T, ok bool) { 72 | if ent, ok := c.items[key]; ok { 73 | c.evictList.MoveToFront(ent) 74 | if ent.Value == nil { 75 | var empty T 76 | return empty, false 77 | } 78 | return ent.Value.value, true 79 | } 80 | return 81 | } 82 | 83 | // Contains checks if a key is in the cache, without updating the recent-ness 84 | // or deleting it for being stale. 85 | func (c *LRU[K, T]) Contains(key K) (ok bool) { 86 | _, ok = c.items[key] 87 | return ok 88 | } 89 | 90 | // Peek returns the key value (or undefined if not found) without updating 91 | // the "recently used"-ness of the key. 92 | func (c *LRU[K, T]) Peek(key K) (value T, ok bool) { 93 | var ent *list.Element[*entry[K, T]] 94 | if ent, ok = c.items[key]; ok { 95 | return ent.Value.value, true 96 | } 97 | var empty T 98 | return empty, ok 99 | } 100 | 101 | // Remove removes the provided key from the cache, returning if the 102 | // key was contained. 103 | func (c *LRU[K, T]) Remove(key K) (present bool) { 104 | if ent, ok := c.items[key]; ok { 105 | c.removeElement(ent) 106 | return true 107 | } 108 | return false 109 | } 110 | 111 | // RemoveOldest removes the oldest item from the cache. 112 | func (c *LRU[K, T]) RemoveOldest() (key K, value T, ok bool) { 113 | ent := c.evictList.Back() 114 | if ent != nil { 115 | c.removeElement(ent) 116 | kv := ent.Value 117 | return kv.key, kv.value, true 118 | } 119 | var emptyK K 120 | var emptyV T 121 | return emptyK, emptyV, false 122 | } 123 | 124 | // GetOldest returns the oldest entry 125 | func (c *LRU[K, T]) GetOldest() (key K, value T, ok bool) { 126 | ent := c.evictList.Back() 127 | if ent != nil { 128 | kv := ent.Value 129 | return kv.key, kv.value, true 130 | } 131 | var emptyK K 132 | var emptyV T 133 | return emptyK, emptyV, false 134 | } 135 | 136 | // Keys returns a slice of the keys in the cache, from oldest to newest. 137 | func (c *LRU[K, T]) Keys() []K { 138 | keys := make([]K, len(c.items)) 139 | i := 0 140 | for ent := c.evictList.Back(); ent != nil; ent = ent.Prev() { 141 | keys[i] = ent.Value.key 142 | i++ 143 | } 144 | return keys 145 | } 146 | 147 | // Len returns the number of items in the cache. 148 | func (c *LRU[K, T]) Len() int { 149 | return c.evictList.Len() 150 | } 151 | 152 | // Resize changes the cache size. 153 | func (c *LRU[K, T]) Resize(size int) (evicted int) { 154 | diff := c.Len() - size 155 | if diff < 0 { 156 | diff = 0 157 | } 158 | for i := 0; i < diff; i++ { 159 | c.removeOldest() 160 | } 161 | c.size = size 162 | return diff 163 | } 164 | 165 | // removeOldest removes the oldest item from the cache. 166 | func (c *LRU[K, T]) removeOldest() { 167 | ent := c.evictList.Back() 168 | if ent != nil { 169 | c.removeElement(ent) 170 | } 171 | } 172 | 173 | // removeElement is used to remove a given list element from the cache 174 | func (c *LRU[K, T]) removeElement(e *list.Element[*entry[K, T]]) { 175 | c.evictList.Remove(e) 176 | kv := e.Value 177 | delete(c.items, kv.key) 178 | if c.onEvict != nil { 179 | c.onEvict(kv.key, kv.value) 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /2q.go: -------------------------------------------------------------------------------- 1 | package lru 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | 7 | "github.com/descope/golang-lru/simplelru" 8 | ) 9 | 10 | const ( 11 | // Default2QRecentRatio is the ratio of the 2Q cache dedicated 12 | // to recently added entries that have only been accessed once. 13 | Default2QRecentRatio = 0.25 14 | 15 | // Default2QGhostEntries is the default ratio of ghost 16 | // entries kept to track entries recently evicted 17 | Default2QGhostEntries = 0.50 18 | ) 19 | 20 | // TwoQueueCache is a thread-safe fixed size 2Q cache. 21 | // 2Q is an enhancement over the standard LRU cache 22 | // in that it tracks both frequently and recently used 23 | // entries separately. This avoids a burst in access to new 24 | // entries from evicting frequently used entries. It adds some 25 | // additional tracking overhead to the standard LRU cache, and is 26 | // computationally about 2x the cost, and adds some metadata over 27 | // head. The ARCCache is similar, but does not require setting any 28 | // parameters. 29 | type TwoQueueCache[K comparable, T any] struct { 30 | size int 31 | recentSize int 32 | 33 | recent simplelru.LRUCache[K, T] 34 | frequent simplelru.LRUCache[K, T] 35 | recentEvict simplelru.LRUCache[K, T] 36 | lock sync.RWMutex 37 | } 38 | 39 | // New2Q creates a new TwoQueueCache using the default 40 | // values for the parameters. 41 | func New2Q[K comparable, T any](size int) (*TwoQueueCache[K, T], error) { 42 | return New2QParams[K, T](size, Default2QRecentRatio, Default2QGhostEntries) 43 | } 44 | 45 | // New2QParams creates a new TwoQueueCache using the provided 46 | // parameter values. 47 | func New2QParams[K comparable, T any](size int, recentRatio, ghostRatio float64) (*TwoQueueCache[K, T], error) { 48 | if size <= 0 { 49 | return nil, fmt.Errorf("invalid size") 50 | } 51 | if recentRatio < 0.0 || recentRatio > 1.0 { 52 | return nil, fmt.Errorf("invalid recent ratio") 53 | } 54 | if ghostRatio < 0.0 || ghostRatio > 1.0 { 55 | return nil, fmt.Errorf("invalid ghost ratio") 56 | } 57 | 58 | // Determine the sub-sizes 59 | recentSize := int(float64(size) * recentRatio) 60 | evictSize := int(float64(size) * ghostRatio) 61 | 62 | // Allocate the LRUs 63 | recent, err := simplelru.NewLRU[K, T](size, nil) 64 | if err != nil { 65 | return nil, err 66 | } 67 | frequent, err := simplelru.NewLRU[K, T](size, nil) 68 | if err != nil { 69 | return nil, err 70 | } 71 | recentEvict, err := simplelru.NewLRU[K, T](evictSize, nil) 72 | if err != nil { 73 | return nil, err 74 | } 75 | 76 | // Initialize the cache 77 | c := &TwoQueueCache[K, T]{ 78 | size: size, 79 | recentSize: recentSize, 80 | recent: recent, 81 | frequent: frequent, 82 | recentEvict: recentEvict, 83 | } 84 | return c, nil 85 | } 86 | 87 | // Get looks up a key's value from the cache. 88 | func (c *TwoQueueCache[K, T]) Get(key K) (value T, ok bool) { 89 | c.lock.Lock() 90 | defer c.lock.Unlock() 91 | 92 | // Check if this is a frequent value 93 | if val, ok := c.frequent.Get(key); ok { 94 | return val, ok 95 | } 96 | 97 | // If the value is contained in recent, then we 98 | // promote it to frequent 99 | if val, ok := c.recent.Peek(key); ok { 100 | c.recent.Remove(key) 101 | c.frequent.Add(key, val) 102 | return val, ok 103 | } 104 | 105 | // No hit 106 | var emptyV T 107 | return emptyV, false 108 | } 109 | 110 | // Add adds a value to the cache. 111 | func (c *TwoQueueCache[K, T]) Add(key K, value T) { 112 | c.lock.Lock() 113 | defer c.lock.Unlock() 114 | 115 | // Check if the value is frequently used already, 116 | // and just update the value 117 | if c.frequent.Contains(key) { 118 | c.frequent.Add(key, value) 119 | return 120 | } 121 | 122 | // Check if the value is recently used, and promote 123 | // the value into the frequent list 124 | if c.recent.Contains(key) { 125 | c.recent.Remove(key) 126 | c.frequent.Add(key, value) 127 | return 128 | } 129 | 130 | // If the value was recently evicted, add it to the 131 | // frequently used list 132 | if c.recentEvict.Contains(key) { 133 | c.ensureSpace(true) 134 | c.recentEvict.Remove(key) 135 | c.frequent.Add(key, value) 136 | return 137 | } 138 | 139 | // Add to the recently seen list 140 | c.ensureSpace(false) 141 | c.recent.Add(key, value) 142 | } 143 | 144 | // ensureSpace is used to ensure we have space in the cache 145 | func (c *TwoQueueCache[K, T]) ensureSpace(recentEvict bool) { 146 | // If we have space, nothing to do 147 | recentLen := c.recent.Len() 148 | freqLen := c.frequent.Len() 149 | if recentLen+freqLen < c.size { 150 | return 151 | } 152 | 153 | // If the recent buffer is larger than 154 | // the target, evict from there 155 | if recentLen > 0 && (recentLen > c.recentSize || (recentLen == c.recentSize && !recentEvict)) { 156 | k, _, _ := c.recent.RemoveOldest() 157 | var emptyV T 158 | c.recentEvict.Add(k, emptyV) 159 | return 160 | } 161 | 162 | // Remove from the frequent list otherwise 163 | c.frequent.RemoveOldest() 164 | } 165 | 166 | // Len returns the number of items in the cache. 167 | func (c *TwoQueueCache[K, T]) Len() int { 168 | c.lock.RLock() 169 | defer c.lock.RUnlock() 170 | return c.recent.Len() + c.frequent.Len() 171 | } 172 | 173 | // Keys returns a slice of the keys in the cache. 174 | // The frequently used keys are first in the returned slice. 175 | func (c *TwoQueueCache[K, T]) Keys() []K { 176 | c.lock.RLock() 177 | defer c.lock.RUnlock() 178 | k1 := c.frequent.Keys() 179 | k2 := c.recent.Keys() 180 | return append(k1, k2...) 181 | } 182 | 183 | // Remove removes the provided key from the cache. 184 | func (c *TwoQueueCache[K, T]) Remove(key K) { 185 | c.lock.Lock() 186 | defer c.lock.Unlock() 187 | if c.frequent.Remove(key) { 188 | return 189 | } 190 | if c.recent.Remove(key) { 191 | return 192 | } 193 | if c.recentEvict.Remove(key) { 194 | return 195 | } 196 | } 197 | 198 | // Purge is used to completely clear the cache. 199 | func (c *TwoQueueCache[K, T]) Purge() { 200 | c.lock.Lock() 201 | defer c.lock.Unlock() 202 | c.recent.Purge() 203 | c.frequent.Purge() 204 | c.recentEvict.Purge() 205 | } 206 | 207 | // Contains is used to check if the cache contains a key 208 | // without updating recency or frequency. 209 | func (c *TwoQueueCache[K, T]) Contains(key K) bool { 210 | c.lock.RLock() 211 | defer c.lock.RUnlock() 212 | return c.frequent.Contains(key) || c.recent.Contains(key) 213 | } 214 | 215 | // Peek is used to inspect the cache value of a key 216 | // without updating recency or frequency. 217 | func (c *TwoQueueCache[K, T]) Peek(key K) (value T, ok bool) { 218 | c.lock.RLock() 219 | defer c.lock.RUnlock() 220 | if val, ok := c.frequent.Peek(key); ok { 221 | return val, ok 222 | } 223 | return c.recent.Peek(key) 224 | } 225 | -------------------------------------------------------------------------------- /2q_test.go: -------------------------------------------------------------------------------- 1 | package lru 2 | 3 | import ( 4 | "math/rand" 5 | "testing" 6 | ) 7 | 8 | func Benchmark2Q_Rand(b *testing.B) { 9 | l, err := New2Q[int64, int64](8192) 10 | if err != nil { 11 | b.Fatalf("err: %v", err) 12 | } 13 | 14 | trace := make([]int64, b.N*2) 15 | for i := 0; i < b.N*2; i++ { 16 | trace[i] = rand.Int63() % 32768 17 | } 18 | 19 | b.ResetTimer() 20 | 21 | var hit, miss int 22 | for i := 0; i < 2*b.N; i++ { 23 | if i%2 == 0 { 24 | l.Add(trace[i], trace[i]) 25 | } else { 26 | _, ok := l.Get(trace[i]) 27 | if ok { 28 | hit++ 29 | } else { 30 | miss++ 31 | } 32 | } 33 | } 34 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss)) 35 | } 36 | 37 | func Benchmark2Q_Freq(b *testing.B) { 38 | l, err := New2Q[int64, int64](8192) 39 | if err != nil { 40 | b.Fatalf("err: %v", err) 41 | } 42 | 43 | trace := make([]int64, b.N*2) 44 | for i := 0; i < b.N*2; i++ { 45 | if i%2 == 0 { 46 | trace[i] = rand.Int63() % 16384 47 | } else { 48 | trace[i] = rand.Int63() % 32768 49 | } 50 | } 51 | 52 | b.ResetTimer() 53 | 54 | for i := 0; i < b.N; i++ { 55 | l.Add(trace[i], trace[i]) 56 | } 57 | var hit, miss int 58 | for i := 0; i < b.N; i++ { 59 | _, ok := l.Get(trace[i]) 60 | if ok { 61 | hit++ 62 | } else { 63 | miss++ 64 | } 65 | } 66 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(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 := rand.Int63() % 512 79 | r := rand.Int63() 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 | _, ok := l.Get(i) 116 | if !ok { 117 | t.Fatalf("missing: %d", i) 118 | } 119 | } 120 | if n := l.recent.Len(); n != 0 { 121 | t.Fatalf("bad: %d", n) 122 | } 123 | if n := l.frequent.Len(); n != 128 { 124 | t.Fatalf("bad: %d", n) 125 | } 126 | 127 | // Get be from t2 128 | for i := 0; i < 128; i++ { 129 | _, ok := l.Get(i) 130 | if !ok { 131 | t.Fatalf("missing: %d", i) 132 | } 133 | } 134 | if n := l.recent.Len(); n != 0 { 135 | t.Fatalf("bad: %d", n) 136 | } 137 | if n := l.frequent.Len(); n != 128 { 138 | t.Fatalf("bad: %d", n) 139 | } 140 | } 141 | 142 | func Test2Q_Add_RecentToFrequent(t *testing.T) { 143 | l, err := New2Q[int, int](128) 144 | if err != nil { 145 | t.Fatalf("err: %v", err) 146 | } 147 | 148 | // Add initially to recent 149 | l.Add(1, 1) 150 | if n := l.recent.Len(); n != 1 { 151 | t.Fatalf("bad: %d", n) 152 | } 153 | if n := l.frequent.Len(); n != 0 { 154 | t.Fatalf("bad: %d", n) 155 | } 156 | 157 | // Add should upgrade to frequent 158 | l.Add(1, 1) 159 | if n := l.recent.Len(); n != 0 { 160 | t.Fatalf("bad: %d", n) 161 | } 162 | if n := l.frequent.Len(); n != 1 { 163 | t.Fatalf("bad: %d", n) 164 | } 165 | 166 | // Add should remain in frequent 167 | l.Add(1, 1) 168 | if n := l.recent.Len(); n != 0 { 169 | t.Fatalf("bad: %d", n) 170 | } 171 | if n := l.frequent.Len(); n != 1 { 172 | t.Fatalf("bad: %d", n) 173 | } 174 | } 175 | 176 | func Test2Q_Add_RecentEvict(t *testing.T) { 177 | l, err := New2Q[int, int](4) 178 | if err != nil { 179 | t.Fatalf("err: %v", err) 180 | } 181 | 182 | // Add 1,2,3,4,5 -> Evict 1 183 | l.Add(1, 1) 184 | l.Add(2, 2) 185 | l.Add(3, 3) 186 | l.Add(4, 4) 187 | l.Add(5, 5) 188 | if n := l.recent.Len(); n != 4 { 189 | t.Fatalf("bad: %d", n) 190 | } 191 | if n := l.recentEvict.Len(); n != 1 { 192 | t.Fatalf("bad: %d", n) 193 | } 194 | if n := l.frequent.Len(); n != 0 { 195 | t.Fatalf("bad: %d", n) 196 | } 197 | 198 | // Pull in the recently evicted 199 | l.Add(1, 1) 200 | if n := l.recent.Len(); n != 3 { 201 | t.Fatalf("bad: %d", n) 202 | } 203 | if n := l.recentEvict.Len(); n != 1 { 204 | t.Fatalf("bad: %d", n) 205 | } 206 | if n := l.frequent.Len(); n != 1 { 207 | t.Fatalf("bad: %d", n) 208 | } 209 | 210 | // Add 6, should cause another recent evict 211 | l.Add(6, 6) 212 | if n := l.recent.Len(); n != 3 { 213 | t.Fatalf("bad: %d", n) 214 | } 215 | if n := l.recentEvict.Len(); n != 2 { 216 | t.Fatalf("bad: %d", n) 217 | } 218 | if n := l.frequent.Len(); n != 1 { 219 | t.Fatalf("bad: %d", n) 220 | } 221 | } 222 | 223 | func Test2Q(t *testing.T) { 224 | l, err := New2Q[int, int](128) 225 | if err != nil { 226 | t.Fatalf("err: %v", err) 227 | } 228 | 229 | for i := 0; i < 256; i++ { 230 | l.Add(i, i) 231 | } 232 | if l.Len() != 128 { 233 | t.Fatalf("bad len: %v", l.Len()) 234 | } 235 | 236 | for i, k := range l.Keys() { 237 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 238 | t.Fatalf("bad key: %v", k) 239 | } 240 | } 241 | for i := 0; i < 128; i++ { 242 | _, ok := l.Get(i) 243 | if ok { 244 | t.Fatalf("should be evicted") 245 | } 246 | } 247 | for i := 128; i < 256; i++ { 248 | _, ok := l.Get(i) 249 | if !ok { 250 | t.Fatalf("should not be evicted") 251 | } 252 | } 253 | for i := 128; i < 192; i++ { 254 | l.Remove(i) 255 | _, ok := l.Get(i) 256 | if ok { 257 | t.Fatalf("should be deleted") 258 | } 259 | } 260 | 261 | l.Purge() 262 | if l.Len() != 0 { 263 | t.Fatalf("bad len: %v", l.Len()) 264 | } 265 | if _, ok := l.Get(200); ok { 266 | t.Fatalf("should contain nothing") 267 | } 268 | } 269 | 270 | // Test that Contains doesn't update recent-ness 271 | func Test2Q_Contains(t *testing.T) { 272 | l, err := New2Q[int, int](2) 273 | if err != nil { 274 | t.Fatalf("err: %v", err) 275 | } 276 | 277 | l.Add(1, 1) 278 | l.Add(2, 2) 279 | if !l.Contains(1) { 280 | t.Errorf("1 should be contained") 281 | } 282 | 283 | l.Add(3, 3) 284 | if l.Contains(1) { 285 | t.Errorf("Contains should not have updated recent-ness of 1") 286 | } 287 | } 288 | 289 | // Test that Peek doesn't update recent-ness 290 | func Test2Q_Peek(t *testing.T) { 291 | l, err := New2Q[int, int](2) 292 | if err != nil { 293 | t.Fatalf("err: %v", err) 294 | } 295 | 296 | l.Add(1, 1) 297 | l.Add(2, 2) 298 | if v, ok := l.Peek(1); !ok || v != 1 { 299 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 300 | } 301 | 302 | l.Add(3, 3) 303 | if l.Contains(1) { 304 | t.Errorf("should not have updated recent-ness of 1") 305 | } 306 | } 307 | -------------------------------------------------------------------------------- /arc.go: -------------------------------------------------------------------------------- 1 | package lru 2 | 3 | import ( 4 | "sync" 5 | 6 | "github.com/descope/golang-lru/simplelru" 7 | ) 8 | 9 | // ARCCache is a thread-safe fixed size Adaptive Replacement Cache (ARC). 10 | // ARC is an enhancement over the standard LRU cache in that tracks both 11 | // frequency and recency of use. This avoids a burst in access to new 12 | // entries from evicting the frequently used older entries. It adds some 13 | // additional tracking overhead to a standard LRU cache, computationally 14 | // it is roughly 2x the cost, and the extra memory overhead is linear 15 | // with the size of the cache. ARC has been patented by IBM, but is 16 | // similar to the TwoQueueCache (2Q) which requires setting parameters. 17 | type ARCCache[K comparable, T any] struct { 18 | size int // Size is the total capacity of the cache 19 | p int // P is the dynamic preference towards T1 or T2 20 | 21 | t1 simplelru.LRUCache[K, T] // T1 is the LRU for recently accessed items 22 | b1 simplelru.LRUCache[K, T] // B1 is the LRU for evictions from t1 23 | 24 | t2 simplelru.LRUCache[K, T] // T2 is the LRU for frequently accessed items 25 | b2 simplelru.LRUCache[K, T] // B2 is the LRU for evictions from t2 26 | 27 | lock sync.RWMutex 28 | } 29 | 30 | // NewARC creates an ARC of the given size 31 | func NewARC[K comparable, T any](size int) (*ARCCache[K, T], error) { 32 | // Create the sub LRUs 33 | b1, err := simplelru.NewLRU[K, T](size, nil) 34 | if err != nil { 35 | return nil, err 36 | } 37 | b2, err := simplelru.NewLRU[K, T](size, nil) 38 | if err != nil { 39 | return nil, err 40 | } 41 | t1, err := simplelru.NewLRU[K, T](size, nil) 42 | if err != nil { 43 | return nil, err 44 | } 45 | t2, err := simplelru.NewLRU[K, T](size, nil) 46 | if err != nil { 47 | return nil, err 48 | } 49 | 50 | // Initialize the ARC 51 | c := &ARCCache[K, T]{ 52 | size: size, 53 | p: 0, 54 | t1: t1, 55 | b1: b1, 56 | t2: t2, 57 | b2: b2, 58 | } 59 | return c, nil 60 | } 61 | 62 | // Get looks up a key's value from the cache. 63 | func (c *ARCCache[K, T]) Get(key K) (value T, ok bool) { 64 | c.lock.Lock() 65 | defer c.lock.Unlock() 66 | 67 | // If the value is contained in T1 (recent), then 68 | // promote it to T2 (frequent) 69 | if val, ok := c.t1.Peek(key); ok { 70 | c.t1.Remove(key) 71 | c.t2.Add(key, val) 72 | return val, ok 73 | } 74 | 75 | // Check if the value is contained in T2 (frequent) 76 | if val, ok := c.t2.Get(key); ok { 77 | return val, ok 78 | } 79 | 80 | // No hit 81 | var emptyV T 82 | return emptyV, false 83 | } 84 | 85 | // Add adds a value to the cache. 86 | func (c *ARCCache[K, T]) Add(key K, value T) { 87 | c.lock.Lock() 88 | defer c.lock.Unlock() 89 | 90 | // Check if the value is contained in T1 (recent), and potentially 91 | // promote it to frequent T2 92 | if c.t1.Contains(key) { 93 | c.t1.Remove(key) 94 | c.t2.Add(key, value) 95 | return 96 | } 97 | 98 | // Check if the value is already in T2 (frequent) and update it 99 | if c.t2.Contains(key) { 100 | c.t2.Add(key, value) 101 | return 102 | } 103 | 104 | // Check if this value was recently evicted as part of the 105 | // recently used list 106 | if c.b1.Contains(key) { 107 | // T1 set is too small, increase P appropriately 108 | delta := 1 109 | b1Len := c.b1.Len() 110 | b2Len := c.b2.Len() 111 | if b2Len > b1Len { 112 | delta = b2Len / b1Len 113 | } 114 | if c.p+delta >= c.size { 115 | c.p = c.size 116 | } else { 117 | c.p += delta 118 | } 119 | 120 | // Potentially need to make room in the cache 121 | if c.t1.Len()+c.t2.Len() >= c.size { 122 | c.replace(false) 123 | } 124 | 125 | // Remove from B1 126 | c.b1.Remove(key) 127 | 128 | // Add the key to the frequently used list 129 | c.t2.Add(key, value) 130 | return 131 | } 132 | 133 | // Check if this value was recently evicted as part of the 134 | // frequently used list 135 | if c.b2.Contains(key) { 136 | // T2 set is too small, decrease P appropriately 137 | delta := 1 138 | b1Len := c.b1.Len() 139 | b2Len := c.b2.Len() 140 | if b1Len > b2Len { 141 | delta = b1Len / b2Len 142 | } 143 | if delta >= c.p { 144 | c.p = 0 145 | } else { 146 | c.p -= delta 147 | } 148 | 149 | // Potentially need to make room in the cache 150 | if c.t1.Len()+c.t2.Len() >= c.size { 151 | c.replace(true) 152 | } 153 | 154 | // Remove from B2 155 | c.b2.Remove(key) 156 | 157 | // Add the key to the frequently used list 158 | c.t2.Add(key, value) 159 | return 160 | } 161 | 162 | // Potentially need to make room in the cache 163 | if c.t1.Len()+c.t2.Len() >= c.size { 164 | c.replace(false) 165 | } 166 | 167 | // Keep the size of the ghost buffers trim 168 | if c.b1.Len() > c.size-c.p { 169 | c.b1.RemoveOldest() 170 | } 171 | if c.b2.Len() > c.p { 172 | c.b2.RemoveOldest() 173 | } 174 | 175 | // Add to the recently seen list 176 | c.t1.Add(key, value) 177 | } 178 | 179 | // replace is used to adaptively evict from either T1 or T2 180 | // based on the current learned value of P 181 | func (c *ARCCache[K, T]) replace(b2ContainsKey bool) { 182 | t1Len := c.t1.Len() 183 | if t1Len > 0 && (t1Len > c.p || (t1Len == c.p && b2ContainsKey)) { 184 | k, _, ok := c.t1.RemoveOldest() 185 | if ok { 186 | var emptyV T 187 | c.b1.Add(k, emptyV) 188 | } 189 | } else { 190 | k, _, ok := c.t2.RemoveOldest() 191 | if ok { 192 | var emptyV T 193 | c.b2.Add(k, emptyV) 194 | } 195 | } 196 | } 197 | 198 | // Len returns the number of cached entries 199 | func (c *ARCCache[K, T]) Len() int { 200 | c.lock.RLock() 201 | defer c.lock.RUnlock() 202 | return c.t1.Len() + c.t2.Len() 203 | } 204 | 205 | // Keys returns all the cached keys 206 | func (c *ARCCache[K, T]) Keys() []K { 207 | c.lock.RLock() 208 | defer c.lock.RUnlock() 209 | k1 := c.t1.Keys() 210 | k2 := c.t2.Keys() 211 | return append(k1, k2...) 212 | } 213 | 214 | // Remove is used to purge a key from the cache 215 | func (c *ARCCache[K, T]) Remove(key K) { 216 | c.lock.Lock() 217 | defer c.lock.Unlock() 218 | if c.t1.Remove(key) { 219 | return 220 | } 221 | if c.t2.Remove(key) { 222 | return 223 | } 224 | if c.b1.Remove(key) { 225 | return 226 | } 227 | if c.b2.Remove(key) { 228 | return 229 | } 230 | } 231 | 232 | // Purge is used to clear the cache 233 | func (c *ARCCache[K, T]) Purge() { 234 | c.lock.Lock() 235 | defer c.lock.Unlock() 236 | c.t1.Purge() 237 | c.t2.Purge() 238 | c.b1.Purge() 239 | c.b2.Purge() 240 | } 241 | 242 | // Contains is used to check if the cache contains a key 243 | // without updating recency or frequency. 244 | func (c *ARCCache[K, T]) Contains(key K) bool { 245 | c.lock.RLock() 246 | defer c.lock.RUnlock() 247 | return c.t1.Contains(key) || c.t2.Contains(key) 248 | } 249 | 250 | // Peek is used to inspect the cache value of a key 251 | // without updating recency or frequency. 252 | func (c *ARCCache[K, T]) Peek(key K) (value T, ok bool) { 253 | c.lock.RLock() 254 | defer c.lock.RUnlock() 255 | if val, ok := c.t1.Peek(key); ok { 256 | return val, ok 257 | } 258 | return c.t2.Peek(key) 259 | } 260 | -------------------------------------------------------------------------------- /lru_test.go: -------------------------------------------------------------------------------- 1 | package lru 2 | 3 | import ( 4 | "math/rand" 5 | "testing" 6 | ) 7 | 8 | func BenchmarkLRU_Rand(b *testing.B) { 9 | l, err := New[int64, int64](8192) 10 | if err != nil { 11 | b.Fatalf("err: %v", err) 12 | } 13 | 14 | trace := make([]int64, b.N*2) 15 | for i := 0; i < b.N*2; i++ { 16 | trace[i] = rand.Int63() % 32768 17 | } 18 | 19 | b.ResetTimer() 20 | 21 | var hit, miss int 22 | for i := 0; i < 2*b.N; i++ { 23 | if i%2 == 0 { 24 | l.Add(trace[i], trace[i]) 25 | } else { 26 | _, ok := l.Get(trace[i]) 27 | if ok { 28 | hit++ 29 | } else { 30 | miss++ 31 | } 32 | } 33 | } 34 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss)) 35 | } 36 | 37 | func BenchmarkLRU_Freq(b *testing.B) { 38 | l, err := New[int64, int64](8192) 39 | if err != nil { 40 | b.Fatalf("err: %v", err) 41 | } 42 | 43 | trace := make([]int64, b.N*2) 44 | for i := 0; i < b.N*2; i++ { 45 | if i%2 == 0 { 46 | trace[i] = rand.Int63() % 16384 47 | } else { 48 | trace[i] = rand.Int63() % 32768 49 | } 50 | } 51 | 52 | b.ResetTimer() 53 | 54 | for i := 0; i < b.N; i++ { 55 | l.Add(trace[i], trace[i]) 56 | } 57 | var hit, miss int 58 | for i := 0; i < b.N; i++ { 59 | _, ok := l.Get(trace[i]) 60 | if ok { 61 | hit++ 62 | } else { 63 | miss++ 64 | } 65 | } 66 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss)) 67 | } 68 | 69 | func TestLRU(t *testing.T) { 70 | evictCounter := 0 71 | onEvicted := func(k int, v int) { 72 | if k != v { 73 | t.Fatalf("Evict values not equal (%v!=%v)", k, v) 74 | } 75 | evictCounter++ 76 | } 77 | l, err := NewWithEvict[int, int](128, onEvicted) 78 | if err != nil { 79 | t.Fatalf("err: %v", err) 80 | } 81 | 82 | for i := 0; i < 256; i++ { 83 | l.Add(i, i) 84 | } 85 | if l.Len() != 128 { 86 | t.Fatalf("bad len: %v", l.Len()) 87 | } 88 | 89 | if evictCounter != 128 { 90 | t.Fatalf("bad evict count: %v", evictCounter) 91 | } 92 | 93 | for i, k := range l.Keys() { 94 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 95 | t.Fatalf("bad key: %v", k) 96 | } 97 | } 98 | for i := 0; i < 128; i++ { 99 | _, ok := l.Get(i) 100 | if ok { 101 | t.Fatalf("should be evicted") 102 | } 103 | } 104 | for i := 128; i < 256; i++ { 105 | _, ok := l.Get(i) 106 | if !ok { 107 | t.Fatalf("should not be evicted") 108 | } 109 | } 110 | for i := 128; i < 192; i++ { 111 | l.Remove(i) 112 | _, ok := l.Get(i) 113 | if ok { 114 | t.Fatalf("should be deleted") 115 | } 116 | } 117 | 118 | l.Get(192) // expect 192 to be last key in l.Keys() 119 | 120 | for i, k := range l.Keys() { 121 | if (i < 63 && k != i+193) || (i == 63 && k != 192) { 122 | t.Fatalf("out of order key: %v", k) 123 | } 124 | } 125 | 126 | l.Purge() 127 | if l.Len() != 0 { 128 | t.Fatalf("bad len: %v", l.Len()) 129 | } 130 | if _, ok := l.Get(200); ok { 131 | t.Fatalf("should contain nothing") 132 | } 133 | } 134 | 135 | // test that Add returns true/false if an eviction occurred 136 | func TestLRUAdd(t *testing.T) { 137 | evictCounter := 0 138 | onEvicted := func(k int, v int) { 139 | evictCounter++ 140 | } 141 | 142 | l, err := NewWithEvict[int, int](1, onEvicted) 143 | if err != nil { 144 | t.Fatalf("err: %v", err) 145 | } 146 | 147 | if l.Add(1, 1) == true || evictCounter != 0 { 148 | t.Errorf("should not have an eviction") 149 | } 150 | if l.Add(2, 2) == false || evictCounter != 1 { 151 | t.Errorf("should have an eviction") 152 | } 153 | } 154 | 155 | // test that Contains doesn't update recent-ness 156 | func TestLRUContains(t *testing.T) { 157 | l, err := New[int, int](2) 158 | if err != nil { 159 | t.Fatalf("err: %v", err) 160 | } 161 | 162 | l.Add(1, 1) 163 | l.Add(2, 2) 164 | if !l.Contains(1) { 165 | t.Errorf("1 should be contained") 166 | } 167 | 168 | l.Add(3, 3) 169 | if l.Contains(1) { 170 | t.Errorf("Contains should not have updated recent-ness of 1") 171 | } 172 | } 173 | 174 | // test that ContainsOrAdd doesn't update recent-ness 175 | func TestLRUContainsOrAdd(t *testing.T) { 176 | l, err := New[int, int](2) 177 | if err != nil { 178 | t.Fatalf("err: %v", err) 179 | } 180 | 181 | l.Add(1, 1) 182 | l.Add(2, 2) 183 | contains, evict := l.ContainsOrAdd(1, 1) 184 | if !contains { 185 | t.Errorf("1 should be contained") 186 | } 187 | if evict { 188 | t.Errorf("nothing should be evicted here") 189 | } 190 | 191 | l.Add(3, 3) 192 | contains, evict = l.ContainsOrAdd(1, 1) 193 | if contains { 194 | t.Errorf("1 should not have been contained") 195 | } 196 | if !evict { 197 | t.Errorf("an eviction should have occurred") 198 | } 199 | if !l.Contains(1) { 200 | t.Errorf("now 1 should be contained") 201 | } 202 | } 203 | 204 | // test that PeekOrAdd doesn't update recent-ness 205 | func TestLRUPeekOrAdd(t *testing.T) { 206 | l, err := New[int, int](2) 207 | if err != nil { 208 | t.Fatalf("err: %v", err) 209 | } 210 | 211 | l.Add(1, 1) 212 | l.Add(2, 2) 213 | previous, contains, evict := l.PeekOrAdd(1, 1) 214 | if !contains { 215 | t.Errorf("1 should be contained") 216 | } 217 | if evict { 218 | t.Errorf("nothing should be evicted here") 219 | } 220 | if previous != 1 { 221 | t.Errorf("previous is not equal to 1") 222 | } 223 | 224 | l.Add(3, 3) 225 | contains, evict = l.ContainsOrAdd(1, 1) 226 | if contains { 227 | t.Errorf("1 should not have been contained") 228 | } 229 | if !evict { 230 | t.Errorf("an eviction should have occurred") 231 | } 232 | if !l.Contains(1) { 233 | t.Errorf("now 1 should be contained") 234 | } 235 | } 236 | 237 | // test that Peek doesn't update recent-ness 238 | func TestLRUPeek(t *testing.T) { 239 | l, err := New[int, int](2) 240 | if err != nil { 241 | t.Fatalf("err: %v", err) 242 | } 243 | 244 | l.Add(1, 1) 245 | l.Add(2, 2) 246 | if v, ok := l.Peek(1); !ok || v != 1 { 247 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 248 | } 249 | 250 | l.Add(3, 3) 251 | if l.Contains(1) { 252 | t.Errorf("should not have updated recent-ness of 1") 253 | } 254 | } 255 | 256 | // test that Resize can upsize and downsize 257 | func TestLRUResize(t *testing.T) { 258 | onEvictCounter := 0 259 | onEvicted := func(k int, v int) { 260 | onEvictCounter++ 261 | } 262 | l, err := NewWithEvict[int, int](2, onEvicted) 263 | if err != nil { 264 | t.Fatalf("err: %v", err) 265 | } 266 | 267 | // Downsize 268 | l.Add(1, 1) 269 | l.Add(2, 2) 270 | evicted := l.Resize(1) 271 | if evicted != 1 { 272 | t.Errorf("1 element should have been evicted: %v", evicted) 273 | } 274 | if onEvictCounter != 1 { 275 | t.Errorf("onEvicted should have been called 1 time: %v", onEvictCounter) 276 | } 277 | 278 | l.Add(3, 3) 279 | if l.Contains(1) { 280 | t.Errorf("Element 1 should have been evicted") 281 | } 282 | 283 | // Upsize 284 | evicted = l.Resize(2) 285 | if evicted != 0 { 286 | t.Errorf("0 elements should have been evicted: %v", evicted) 287 | } 288 | 289 | l.Add(4, 4) 290 | if !l.Contains(3) || !l.Contains(4) { 291 | t.Errorf("Cache should have contained 2 elements") 292 | } 293 | } 294 | -------------------------------------------------------------------------------- /lru.go: -------------------------------------------------------------------------------- 1 | package lru 2 | 3 | import ( 4 | "sync" 5 | 6 | "github.com/descope/golang-lru/simplelru" 7 | ) 8 | 9 | const ( 10 | // DefaultEvictedBufferSize defines the default buffer size to store evicted key/val 11 | DefaultEvictedBufferSize = 16 12 | ) 13 | 14 | // Cache is a thread-safe fixed size LRU cache. 15 | type Cache[K comparable, T any] struct { 16 | lru *simplelru.LRU[K, T] 17 | evictedKeys []K 18 | evictedVals []T 19 | onEvictedCB func(k K, v T) 20 | lock sync.RWMutex 21 | } 22 | 23 | // New creates an LRU of the given size. 24 | func New[K comparable, T any](size int) (*Cache[K, T], error) { 25 | return NewWithEvict[K, T](size, nil) 26 | } 27 | 28 | // NewWithEvict constructs a fixed size cache with the given eviction 29 | // callback. 30 | func NewWithEvict[K comparable, T any](size int, onEvicted func(key K, value T)) (c *Cache[K, T], err error) { 31 | // create a cache with default settings 32 | c = &Cache[K, T]{ 33 | onEvictedCB: onEvicted, 34 | } 35 | if onEvicted != nil { 36 | c.initEvictBuffers() 37 | onEvicted = c.onEvicted 38 | } 39 | c.lru, err = simplelru.NewLRU[K, T](size, onEvicted) 40 | return 41 | } 42 | 43 | func (c *Cache[K, T]) initEvictBuffers() { 44 | c.evictedKeys = make([]K, 0, DefaultEvictedBufferSize) 45 | c.evictedVals = make([]T, 0, DefaultEvictedBufferSize) 46 | } 47 | 48 | // onEvicted save evicted key/val and sent in externally registered callback 49 | // outside of critical section 50 | func (c *Cache[K, T]) onEvicted(k K, v T) { 51 | c.evictedKeys = append(c.evictedKeys, k) 52 | c.evictedVals = append(c.evictedVals, v) 53 | } 54 | 55 | // Purge is used to completely clear the cache. 56 | func (c *Cache[K, T]) Purge() { 57 | var ks []K 58 | var vs []T 59 | c.lock.Lock() 60 | c.lru.Purge() 61 | if c.onEvictedCB != nil && len(c.evictedKeys) > 0 { 62 | ks, vs = c.evictedKeys, c.evictedVals 63 | c.initEvictBuffers() 64 | } 65 | c.lock.Unlock() 66 | // invoke callback outside of critical section 67 | if c.onEvictedCB != nil { 68 | for i := 0; i < len(ks); i++ { 69 | c.onEvictedCB(ks[i], vs[i]) 70 | } 71 | } 72 | } 73 | 74 | // Add adds a value to the cache. Returns true if an eviction occurred. 75 | func (c *Cache[K, T]) Add(key K, value T) (evicted bool) { 76 | var k K 77 | var v T 78 | c.lock.Lock() 79 | evicted = c.lru.Add(key, value) 80 | if c.onEvictedCB != nil && evicted { 81 | k, v = c.evictedKeys[0], c.evictedVals[0] 82 | c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] 83 | } 84 | c.lock.Unlock() 85 | if c.onEvictedCB != nil && evicted { 86 | c.onEvictedCB(k, v) 87 | } 88 | return 89 | } 90 | 91 | // Get looks up a key's value from the cache. 92 | func (c *Cache[K, T]) Get(key K) (value T, ok bool) { 93 | c.lock.Lock() 94 | value, ok = c.lru.Get(key) 95 | c.lock.Unlock() 96 | return value, ok 97 | } 98 | 99 | // Contains checks if a key is in the cache, without updating the 100 | // recent-ness or deleting it for being stale. 101 | func (c *Cache[K, T]) Contains(key K) bool { 102 | c.lock.RLock() 103 | containKey := c.lru.Contains(key) 104 | c.lock.RUnlock() 105 | return containKey 106 | } 107 | 108 | // Peek returns the key value (or undefined if not found) without updating 109 | // the "recently used"-ness of the key. 110 | func (c *Cache[K, T]) Peek(key K) (value T, ok bool) { 111 | c.lock.RLock() 112 | value, ok = c.lru.Peek(key) 113 | c.lock.RUnlock() 114 | return value, ok 115 | } 116 | 117 | // ContainsOrAdd checks if a key is in the cache without updating the 118 | // recent-ness or deleting it for being stale, and if not, adds the value. 119 | // Returns whether found and whether an eviction occurred. 120 | func (c *Cache[K, T]) ContainsOrAdd(key K, value T) (ok, evicted bool) { 121 | var k K 122 | var v T 123 | c.lock.Lock() 124 | if c.lru.Contains(key) { 125 | c.lock.Unlock() 126 | return true, false 127 | } 128 | evicted = c.lru.Add(key, value) 129 | if c.onEvictedCB != nil && evicted { 130 | k, v = c.evictedKeys[0], c.evictedVals[0] 131 | c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] 132 | } 133 | c.lock.Unlock() 134 | if c.onEvictedCB != nil && evicted { 135 | c.onEvictedCB(k, v) 136 | } 137 | return false, evicted 138 | } 139 | 140 | // PeekOrAdd checks if a key is in the cache without updating the 141 | // recent-ness or deleting it for being stale, and if not, adds the value. 142 | // Returns whether found and whether an eviction occurred. 143 | func (c *Cache[K, T]) PeekOrAdd(key K, value T) (previous T, ok, evicted bool) { 144 | var k K 145 | var v T 146 | c.lock.Lock() 147 | previous, ok = c.lru.Peek(key) 148 | if ok { 149 | c.lock.Unlock() 150 | return previous, true, false 151 | } 152 | evicted = c.lru.Add(key, value) 153 | if c.onEvictedCB != nil && evicted { 154 | k, v = c.evictedKeys[0], c.evictedVals[0] 155 | c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] 156 | } 157 | c.lock.Unlock() 158 | if c.onEvictedCB != nil && evicted { 159 | c.onEvictedCB(k, v) 160 | } 161 | var empty T 162 | return empty, false, evicted 163 | } 164 | 165 | // Remove removes the provided key from the cache. 166 | func (c *Cache[K, T]) Remove(key K) (present bool) { 167 | var k K 168 | var v T 169 | c.lock.Lock() 170 | present = c.lru.Remove(key) 171 | if c.onEvictedCB != nil && present { 172 | k, v = c.evictedKeys[0], c.evictedVals[0] 173 | c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] 174 | } 175 | c.lock.Unlock() 176 | if c.onEvictedCB != nil && present { 177 | c.onEvicted(k, v) 178 | } 179 | return 180 | } 181 | 182 | // Resize changes the cache size. 183 | func (c *Cache[K, T]) Resize(size int) (evicted int) { 184 | var ks []K 185 | var vs []T 186 | c.lock.Lock() 187 | evicted = c.lru.Resize(size) 188 | if c.onEvictedCB != nil && evicted > 0 { 189 | ks, vs = c.evictedKeys, c.evictedVals 190 | c.initEvictBuffers() 191 | } 192 | c.lock.Unlock() 193 | if c.onEvictedCB != nil && evicted > 0 { 194 | for i := 0; i < len(ks); i++ { 195 | c.onEvictedCB(ks[i], vs[i]) 196 | } 197 | } 198 | return evicted 199 | } 200 | 201 | // RemoveOldest removes the oldest item from the cache. 202 | func (c *Cache[K, T]) RemoveOldest() (key K, value T, ok bool) { 203 | var k K 204 | var v T 205 | c.lock.Lock() 206 | key, value, ok = c.lru.RemoveOldest() 207 | if c.onEvictedCB != nil && ok { 208 | k, v = c.evictedKeys[0], c.evictedVals[0] 209 | c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] 210 | } 211 | c.lock.Unlock() 212 | if c.onEvictedCB != nil && ok { 213 | c.onEvictedCB(k, v) 214 | } 215 | return 216 | } 217 | 218 | // GetOldest returns the oldest entry 219 | func (c *Cache[K, T]) GetOldest() (key K, value T, ok bool) { 220 | c.lock.RLock() 221 | key, value, ok = c.lru.GetOldest() 222 | c.lock.RUnlock() 223 | return 224 | } 225 | 226 | // Keys returns a slice of the keys in the cache, from oldest to newest. 227 | func (c *Cache[K, T]) Keys() []K { 228 | c.lock.RLock() 229 | keys := c.lru.Keys() 230 | c.lock.RUnlock() 231 | return keys 232 | } 233 | 234 | // Len returns the number of items in the cache. 235 | func (c *Cache[K, T]) Len() int { 236 | c.lock.RLock() 237 | length := c.lru.Len() 238 | c.lock.RUnlock() 239 | return length 240 | } 241 | -------------------------------------------------------------------------------- /arc_test.go: -------------------------------------------------------------------------------- 1 | package lru 2 | 3 | import ( 4 | "math/rand" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | func init() { 10 | rand.Seed(time.Now().Unix()) 11 | } 12 | 13 | func BenchmarkARC_Rand(b *testing.B) { 14 | l, err := NewARC[int64, int64](8192) 15 | if err != nil { 16 | b.Fatalf("err: %v", err) 17 | } 18 | 19 | trace := make([]int64, b.N*2) 20 | for i := 0; i < b.N*2; i++ { 21 | trace[i] = rand.Int63() % 32768 22 | } 23 | 24 | b.ResetTimer() 25 | 26 | var hit, miss int 27 | for i := 0; i < 2*b.N; i++ { 28 | if i%2 == 0 { 29 | l.Add(trace[i], trace[i]) 30 | } else { 31 | _, ok := l.Get(trace[i]) 32 | if ok { 33 | hit++ 34 | } else { 35 | miss++ 36 | } 37 | } 38 | } 39 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss)) 40 | } 41 | 42 | func BenchmarkARC_Freq(b *testing.B) { 43 | l, err := NewARC[int64, int64](8192) 44 | if err != nil { 45 | b.Fatalf("err: %v", err) 46 | } 47 | 48 | trace := make([]int64, b.N*2) 49 | for i := 0; i < b.N*2; i++ { 50 | if i%2 == 0 { 51 | trace[i] = rand.Int63() % 16384 52 | } else { 53 | trace[i] = rand.Int63() % 32768 54 | } 55 | } 56 | 57 | b.ResetTimer() 58 | 59 | for i := 0; i < b.N; i++ { 60 | l.Add(trace[i], trace[i]) 61 | } 62 | var hit, miss int 63 | for i := 0; i < b.N; i++ { 64 | _, ok := l.Get(trace[i]) 65 | if ok { 66 | hit++ 67 | } else { 68 | miss++ 69 | } 70 | } 71 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss)) 72 | } 73 | 74 | func TestARC_RandomOps(t *testing.T) { 75 | size := 128 76 | l, err := NewARC[int64, int64](128) 77 | if err != nil { 78 | t.Fatalf("err: %v", err) 79 | } 80 | 81 | n := 200000 82 | for i := 0; i < n; i++ { 83 | key := rand.Int63() % 512 84 | r := rand.Int63() 85 | switch r % 3 { 86 | case 0: 87 | l.Add(key, key) 88 | case 1: 89 | l.Get(key) 90 | case 2: 91 | l.Remove(key) 92 | } 93 | 94 | if l.t1.Len()+l.t2.Len() > size { 95 | t.Fatalf("bad: t1: %d t2: %d b1: %d b2: %d p: %d", 96 | l.t1.Len(), l.t2.Len(), l.b1.Len(), l.b2.Len(), l.p) 97 | } 98 | if l.b1.Len()+l.b2.Len() > size { 99 | t.Fatalf("bad: t1: %d t2: %d b1: %d b2: %d p: %d", 100 | l.t1.Len(), l.t2.Len(), l.b1.Len(), l.b2.Len(), l.p) 101 | } 102 | } 103 | } 104 | 105 | func TestARC_Get_RecentToFrequent(t *testing.T) { 106 | l, err := NewARC[int, int](128) 107 | if err != nil { 108 | t.Fatalf("err: %v", err) 109 | } 110 | 111 | // Touch all the entries, should be in t1 112 | for i := 0; i < 128; i++ { 113 | l.Add(i, i) 114 | } 115 | if n := l.t1.Len(); n != 128 { 116 | t.Fatalf("bad: %d", n) 117 | } 118 | if n := l.t2.Len(); n != 0 { 119 | t.Fatalf("bad: %d", n) 120 | } 121 | 122 | // Get should upgrade to t2 123 | for i := 0; i < 128; i++ { 124 | _, ok := l.Get(i) 125 | if !ok { 126 | t.Fatalf("missing: %d", i) 127 | } 128 | } 129 | if n := l.t1.Len(); n != 0 { 130 | t.Fatalf("bad: %d", n) 131 | } 132 | if n := l.t2.Len(); n != 128 { 133 | t.Fatalf("bad: %d", n) 134 | } 135 | 136 | // Get be from t2 137 | for i := 0; i < 128; i++ { 138 | _, ok := l.Get(i) 139 | if !ok { 140 | t.Fatalf("missing: %d", i) 141 | } 142 | } 143 | if n := l.t1.Len(); n != 0 { 144 | t.Fatalf("bad: %d", n) 145 | } 146 | if n := l.t2.Len(); n != 128 { 147 | t.Fatalf("bad: %d", n) 148 | } 149 | } 150 | 151 | func TestARC_Add_RecentToFrequent(t *testing.T) { 152 | l, err := NewARC[int64, int64](128) 153 | if err != nil { 154 | t.Fatalf("err: %v", err) 155 | } 156 | 157 | // Add initially to t1 158 | l.Add(1, 1) 159 | if n := l.t1.Len(); n != 1 { 160 | t.Fatalf("bad: %d", n) 161 | } 162 | if n := l.t2.Len(); n != 0 { 163 | t.Fatalf("bad: %d", n) 164 | } 165 | 166 | // Add should upgrade to t2 167 | l.Add(1, 1) 168 | if n := l.t1.Len(); n != 0 { 169 | t.Fatalf("bad: %d", n) 170 | } 171 | if n := l.t2.Len(); n != 1 { 172 | t.Fatalf("bad: %d", n) 173 | } 174 | 175 | // Add should remain in t2 176 | l.Add(1, 1) 177 | if n := l.t1.Len(); n != 0 { 178 | t.Fatalf("bad: %d", n) 179 | } 180 | if n := l.t2.Len(); n != 1 { 181 | t.Fatalf("bad: %d", n) 182 | } 183 | } 184 | 185 | func TestARC_Adaptive(t *testing.T) { 186 | l, err := NewARC[int, int](4) 187 | if err != nil { 188 | t.Fatalf("err: %v", err) 189 | } 190 | 191 | // Fill t1 192 | for i := 0; i < 4; i++ { 193 | l.Add(i, i) 194 | } 195 | if n := l.t1.Len(); n != 4 { 196 | t.Fatalf("bad: %d", n) 197 | } 198 | 199 | // Move to t2 200 | l.Get(0) 201 | l.Get(1) 202 | if n := l.t2.Len(); n != 2 { 203 | t.Fatalf("bad: %d", n) 204 | } 205 | 206 | // Evict from t1 207 | l.Add(4, 4) 208 | if n := l.b1.Len(); n != 1 { 209 | t.Fatalf("bad: %d", n) 210 | } 211 | 212 | // Current state 213 | // t1 : (MRU) [4, 3] (LRU) 214 | // t2 : (MRU) [1, 0] (LRU) 215 | // b1 : (MRU) [2] (LRU) 216 | // b2 : (MRU) [] (LRU) 217 | 218 | // Add 2, should cause hit on b1 219 | l.Add(2, 2) 220 | if n := l.b1.Len(); n != 1 { 221 | t.Fatalf("bad: %d", n) 222 | } 223 | if l.p != 1 { 224 | t.Fatalf("bad: %d", l.p) 225 | } 226 | if n := l.t2.Len(); n != 3 { 227 | t.Fatalf("bad: %d", n) 228 | } 229 | 230 | // Current state 231 | // t1 : (MRU) [4] (LRU) 232 | // t2 : (MRU) [2, 1, 0] (LRU) 233 | // b1 : (MRU) [3] (LRU) 234 | // b2 : (MRU) [] (LRU) 235 | 236 | // Add 4, should migrate to t2 237 | l.Add(4, 4) 238 | if n := l.t1.Len(); n != 0 { 239 | t.Fatalf("bad: %d", n) 240 | } 241 | if n := l.t2.Len(); n != 4 { 242 | t.Fatalf("bad: %d", n) 243 | } 244 | 245 | // Current state 246 | // t1 : (MRU) [] (LRU) 247 | // t2 : (MRU) [4, 2, 1, 0] (LRU) 248 | // b1 : (MRU) [3] (LRU) 249 | // b2 : (MRU) [] (LRU) 250 | 251 | // Add 4, should evict to b2 252 | l.Add(5, 5) 253 | if n := l.t1.Len(); n != 1 { 254 | t.Fatalf("bad: %d", n) 255 | } 256 | if n := l.t2.Len(); n != 3 { 257 | t.Fatalf("bad: %d", n) 258 | } 259 | if n := l.b2.Len(); n != 1 { 260 | t.Fatalf("bad: %d", n) 261 | } 262 | 263 | // Current state 264 | // t1 : (MRU) [5] (LRU) 265 | // t2 : (MRU) [4, 2, 1] (LRU) 266 | // b1 : (MRU) [3] (LRU) 267 | // b2 : (MRU) [0] (LRU) 268 | 269 | // Add 0, should decrease p 270 | l.Add(0, 0) 271 | if n := l.t1.Len(); n != 0 { 272 | t.Fatalf("bad: %d", n) 273 | } 274 | if n := l.t2.Len(); n != 4 { 275 | t.Fatalf("bad: %d", n) 276 | } 277 | if n := l.b1.Len(); n != 2 { 278 | t.Fatalf("bad: %d", n) 279 | } 280 | if n := l.b2.Len(); n != 0 { 281 | t.Fatalf("bad: %d", n) 282 | } 283 | if l.p != 0 { 284 | t.Fatalf("bad: %d", l.p) 285 | } 286 | 287 | // Current state 288 | // t1 : (MRU) [] (LRU) 289 | // t2 : (MRU) [0, 4, 2, 1] (LRU) 290 | // b1 : (MRU) [5, 3] (LRU) 291 | // b2 : (MRU) [0] (LRU) 292 | } 293 | 294 | func TestARC(t *testing.T) { 295 | l, err := NewARC[int, int](128) 296 | if err != nil { 297 | t.Fatalf("err: %v", err) 298 | } 299 | 300 | for i := 0; i < 256; i++ { 301 | l.Add(i, i) 302 | } 303 | if l.Len() != 128 { 304 | t.Fatalf("bad len: %v", l.Len()) 305 | } 306 | 307 | for i, k := range l.Keys() { 308 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 309 | t.Fatalf("bad key: %v", k) 310 | } 311 | } 312 | for i := 0; i < 128; i++ { 313 | _, ok := l.Get(i) 314 | if ok { 315 | t.Fatalf("should be evicted") 316 | } 317 | } 318 | for i := 128; i < 256; i++ { 319 | _, ok := l.Get(i) 320 | if !ok { 321 | t.Fatalf("should not be evicted") 322 | } 323 | } 324 | for i := 128; i < 192; i++ { 325 | l.Remove(i) 326 | _, ok := l.Get(i) 327 | if ok { 328 | t.Fatalf("should be deleted") 329 | } 330 | } 331 | 332 | l.Purge() 333 | if l.Len() != 0 { 334 | t.Fatalf("bad len: %v", l.Len()) 335 | } 336 | if _, ok := l.Get(200); ok { 337 | t.Fatalf("should contain nothing") 338 | } 339 | } 340 | 341 | // Test that Contains doesn't update recent-ness 342 | func TestARC_Contains(t *testing.T) { 343 | l, err := NewARC[int, int](2) 344 | if err != nil { 345 | t.Fatalf("err: %v", err) 346 | } 347 | 348 | l.Add(1, 1) 349 | l.Add(2, 2) 350 | if !l.Contains(1) { 351 | t.Errorf("1 should be contained") 352 | } 353 | 354 | l.Add(3, 3) 355 | if l.Contains(1) { 356 | t.Errorf("Contains should not have updated recent-ness of 1") 357 | } 358 | } 359 | 360 | // Test that Peek doesn't update recent-ness 361 | func TestARC_Peek(t *testing.T) { 362 | l, err := NewARC[int, int](2) 363 | if err != nil { 364 | t.Fatalf("err: %v", err) 365 | } 366 | 367 | l.Add(1, 1) 368 | l.Add(2, 2) 369 | if v, ok := l.Peek(1); !ok || v != 1 { 370 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 371 | } 372 | 373 | l.Add(3, 3) 374 | if l.Contains(1) { 375 | t.Errorf("should not have updated recent-ness of 1") 376 | } 377 | } 378 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License, version 2.0 2 | 3 | 1. Definitions 4 | 5 | 1.1. "Contributor" 6 | 7 | means each individual or legal entity that creates, contributes to the 8 | creation of, or owns Covered Software. 9 | 10 | 1.2. "Contributor Version" 11 | 12 | means the combination of the Contributions of others (if any) used by a 13 | Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. "Covered Software" 20 | 21 | means Source Code Form to which the initial Contributor has attached the 22 | notice in Exhibit A, the Executable Form of such Source Code Form, and 23 | Modifications of such Source Code Form, in each case including portions 24 | thereof. 25 | 26 | 1.5. "Incompatible With Secondary Licenses" 27 | means 28 | 29 | a. that the initial Contributor has attached the notice described in 30 | Exhibit B to the Covered Software; or 31 | 32 | b. that the Covered Software was made available under the terms of 33 | version 1.1 or earlier of the License, but not also under the terms of 34 | a Secondary License. 35 | 36 | 1.6. "Executable Form" 37 | 38 | means any form of the work other than Source Code Form. 39 | 40 | 1.7. "Larger Work" 41 | 42 | means a work that combines Covered Software with other material, in a 43 | separate file or files, that is not Covered Software. 44 | 45 | 1.8. "License" 46 | 47 | means this document. 48 | 49 | 1.9. "Licensable" 50 | 51 | means having the right to grant, to the maximum extent possible, whether 52 | at the time of the initial grant or subsequently, any and all of the 53 | rights conveyed by this License. 54 | 55 | 1.10. "Modifications" 56 | 57 | means any of the following: 58 | 59 | a. any file in Source Code Form that results from an addition to, 60 | deletion from, or modification of the contents of Covered Software; or 61 | 62 | b. any new file in Source Code Form that contains any Covered Software. 63 | 64 | 1.11. "Patent Claims" of a Contributor 65 | 66 | means any patent claim(s), including without limitation, method, 67 | process, and apparatus claims, in any patent Licensable by such 68 | Contributor that would be infringed, but for the grant of the License, 69 | by the making, using, selling, offering for sale, having made, import, 70 | or transfer of either its Contributions or its Contributor Version. 71 | 72 | 1.12. "Secondary License" 73 | 74 | means either the GNU General Public License, Version 2.0, the GNU Lesser 75 | General Public License, Version 2.1, the GNU Affero General Public 76 | License, Version 3.0, or any later versions of those licenses. 77 | 78 | 1.13. "Source Code Form" 79 | 80 | means the form of the work preferred for making modifications. 81 | 82 | 1.14. "You" (or "Your") 83 | 84 | means an individual or a legal entity exercising rights under this 85 | License. For legal entities, "You" includes any entity that controls, is 86 | controlled by, or is under common control with You. For purposes of this 87 | definition, "control" means (a) the power, direct or indirect, to cause 88 | the direction or management of such entity, whether by contract or 89 | otherwise, or (b) ownership of more than fifty percent (50%) of the 90 | outstanding shares or beneficial ownership of such entity. 91 | 92 | 93 | 2. License Grants and Conditions 94 | 95 | 2.1. Grants 96 | 97 | Each Contributor hereby grants You a world-wide, royalty-free, 98 | non-exclusive license: 99 | 100 | a. under intellectual property rights (other than patent or trademark) 101 | Licensable by such Contributor to use, reproduce, make available, 102 | modify, display, perform, distribute, and otherwise exploit its 103 | Contributions, either on an unmodified basis, with Modifications, or 104 | as part of a Larger Work; and 105 | 106 | b. under Patent Claims of such Contributor to make, use, sell, offer for 107 | sale, have made, import, and otherwise transfer either its 108 | Contributions or its Contributor Version. 109 | 110 | 2.2. Effective Date 111 | 112 | The licenses granted in Section 2.1 with respect to any Contribution 113 | become effective for each Contribution on the date the Contributor first 114 | distributes such Contribution. 115 | 116 | 2.3. Limitations on Grant Scope 117 | 118 | The licenses granted in this Section 2 are the only rights granted under 119 | this License. No additional rights or licenses will be implied from the 120 | distribution or licensing of Covered Software under this License. 121 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 122 | Contributor: 123 | 124 | a. for any code that a Contributor has removed from Covered Software; or 125 | 126 | b. for infringements caused by: (i) Your and any other third party's 127 | modifications of Covered Software, or (ii) the combination of its 128 | Contributions with other software (except as part of its Contributor 129 | Version); or 130 | 131 | c. under Patent Claims infringed by Covered Software in the absence of 132 | its Contributions. 133 | 134 | This License does not grant any rights in the trademarks, service marks, 135 | or logos of any Contributor (except as may be necessary to comply with 136 | the notice requirements in Section 3.4). 137 | 138 | 2.4. Subsequent Licenses 139 | 140 | No Contributor makes additional grants as a result of Your choice to 141 | distribute the Covered Software under a subsequent version of this 142 | License (see Section 10.2) or under the terms of a Secondary License (if 143 | permitted under the terms of Section 3.3). 144 | 145 | 2.5. Representation 146 | 147 | Each Contributor represents that the Contributor believes its 148 | Contributions are its original creation(s) or it has sufficient rights to 149 | grant the rights to its Contributions conveyed by this License. 150 | 151 | 2.6. Fair Use 152 | 153 | This License is not intended to limit any rights You have under 154 | applicable copyright doctrines of fair use, fair dealing, or other 155 | equivalents. 156 | 157 | 2.7. Conditions 158 | 159 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in 160 | Section 2.1. 161 | 162 | 163 | 3. Responsibilities 164 | 165 | 3.1. Distribution of Source Form 166 | 167 | All distribution of Covered Software in Source Code Form, including any 168 | Modifications that You create or to which You contribute, must be under 169 | the terms of this License. You must inform recipients that the Source 170 | Code Form of the Covered Software is governed by the terms of this 171 | License, and how they can obtain a copy of this License. You may not 172 | attempt to alter or restrict the recipients' rights in the Source Code 173 | Form. 174 | 175 | 3.2. Distribution of Executable Form 176 | 177 | If You distribute Covered Software in Executable Form then: 178 | 179 | a. such Covered Software must also be made available in Source Code Form, 180 | as described in Section 3.1, and You must inform recipients of the 181 | Executable Form how they can obtain a copy of such Source Code Form by 182 | reasonable means in a timely manner, at a charge no more than the cost 183 | of distribution to the recipient; and 184 | 185 | b. You may distribute such Executable Form under the terms of this 186 | License, or sublicense it under different terms, provided that the 187 | license for the Executable Form does not attempt to limit or alter the 188 | recipients' rights in the Source Code Form under this License. 189 | 190 | 3.3. Distribution of a Larger Work 191 | 192 | You may create and distribute a Larger Work under terms of Your choice, 193 | provided that You also comply with the requirements of this License for 194 | the Covered Software. If the Larger Work is a combination of Covered 195 | Software with a work governed by one or more Secondary Licenses, and the 196 | Covered Software is not Incompatible With Secondary Licenses, this 197 | License permits You to additionally distribute such Covered Software 198 | under the terms of such Secondary License(s), so that the recipient of 199 | the Larger Work may, at their option, further distribute the Covered 200 | Software under the terms of either this License or such Secondary 201 | License(s). 202 | 203 | 3.4. Notices 204 | 205 | You may not remove or alter the substance of any license notices 206 | (including copyright notices, patent notices, disclaimers of warranty, or 207 | limitations of liability) contained within the Source Code Form of the 208 | Covered Software, except that You may alter any license notices to the 209 | extent required to remedy known factual inaccuracies. 210 | 211 | 3.5. Application of Additional Terms 212 | 213 | You may choose to offer, and to charge a fee for, warranty, support, 214 | indemnity or liability obligations to one or more recipients of Covered 215 | Software. However, You may do so only on Your own behalf, and not on 216 | behalf of any Contributor. You must make it absolutely clear that any 217 | such warranty, support, indemnity, or liability obligation is offered by 218 | You alone, and You hereby agree to indemnify every Contributor for any 219 | liability incurred by such Contributor as a result of warranty, support, 220 | indemnity or liability terms You offer. You may include additional 221 | disclaimers of warranty and limitations of liability specific to any 222 | jurisdiction. 223 | 224 | 4. Inability to Comply Due to Statute or Regulation 225 | 226 | If it is impossible for You to comply with any of the terms of this License 227 | with respect to some or all of the Covered Software due to statute, 228 | judicial order, or regulation then You must: (a) comply with the terms of 229 | this License to the maximum extent possible; and (b) describe the 230 | limitations and the code they affect. Such description must be placed in a 231 | text file included with all distributions of the Covered Software under 232 | this License. Except to the extent prohibited by statute or regulation, 233 | such description must be sufficiently detailed for a recipient of ordinary 234 | skill to be able to understand it. 235 | 236 | 5. Termination 237 | 238 | 5.1. The rights granted under this License will terminate automatically if You 239 | fail to comply with any of its terms. However, if You become compliant, 240 | then the rights granted under this License from a particular Contributor 241 | are reinstated (a) provisionally, unless and until such Contributor 242 | explicitly and finally terminates Your grants, and (b) on an ongoing 243 | basis, if such Contributor fails to notify You of the non-compliance by 244 | some reasonable means prior to 60 days after You have come back into 245 | compliance. Moreover, Your grants from a particular Contributor are 246 | reinstated on an ongoing basis if such Contributor notifies You of the 247 | non-compliance by some reasonable means, this is the first time You have 248 | received notice of non-compliance with this License from such 249 | Contributor, and You become compliant prior to 30 days after Your receipt 250 | of the notice. 251 | 252 | 5.2. If You initiate litigation against any entity by asserting a patent 253 | infringement claim (excluding declaratory judgment actions, 254 | counter-claims, and cross-claims) alleging that a Contributor Version 255 | directly or indirectly infringes any patent, then the rights granted to 256 | You by any and all Contributors for the Covered Software under Section 257 | 2.1 of this License shall terminate. 258 | 259 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user 260 | license agreements (excluding distributors and resellers) which have been 261 | validly granted by You or Your distributors under this License prior to 262 | termination shall survive termination. 263 | 264 | 6. Disclaimer of Warranty 265 | 266 | Covered Software is provided under this License on an "as is" basis, 267 | without warranty of any kind, either expressed, implied, or statutory, 268 | including, without limitation, warranties that the Covered Software is free 269 | of defects, merchantable, fit for a particular purpose or non-infringing. 270 | The entire risk as to the quality and performance of the Covered Software 271 | is with You. Should any Covered Software prove defective in any respect, 272 | You (not any Contributor) assume the cost of any necessary servicing, 273 | repair, or correction. This disclaimer of warranty constitutes an essential 274 | part of this License. No use of any Covered Software is authorized under 275 | this License except under this disclaimer. 276 | 277 | 7. Limitation of Liability 278 | 279 | Under no circumstances and under no legal theory, whether tort (including 280 | negligence), contract, or otherwise, shall any Contributor, or anyone who 281 | distributes Covered Software as permitted above, be liable to You for any 282 | direct, indirect, special, incidental, or consequential damages of any 283 | character including, without limitation, damages for lost profits, loss of 284 | goodwill, work stoppage, computer failure or malfunction, or any and all 285 | other commercial damages or losses, even if such party shall have been 286 | informed of the possibility of such damages. This limitation of liability 287 | shall not apply to liability for death or personal injury resulting from 288 | such party's negligence to the extent applicable law prohibits such 289 | limitation. Some jurisdictions do not allow the exclusion or limitation of 290 | incidental or consequential damages, so this exclusion and limitation may 291 | not apply to You. 292 | 293 | 8. Litigation 294 | 295 | Any litigation relating to this License may be brought only in the courts 296 | of a jurisdiction where the defendant maintains its principal place of 297 | business and such litigation shall be governed by laws of that 298 | jurisdiction, without reference to its conflict-of-law provisions. Nothing 299 | in this Section shall prevent a party's ability to bring cross-claims or 300 | counter-claims. 301 | 302 | 9. Miscellaneous 303 | 304 | This License represents the complete agreement concerning the subject 305 | matter hereof. If any provision of this License is held to be 306 | unenforceable, such provision shall be reformed only to the extent 307 | necessary to make it enforceable. Any law or regulation which provides that 308 | the language of a contract shall be construed against the drafter shall not 309 | be used to construe this License against a Contributor. 310 | 311 | 312 | 10. Versions of the License 313 | 314 | 10.1. New Versions 315 | 316 | Mozilla Foundation is the license steward. Except as provided in Section 317 | 10.3, no one other than the license steward has the right to modify or 318 | publish new versions of this License. Each version will be given a 319 | distinguishing version number. 320 | 321 | 10.2. Effect of New Versions 322 | 323 | You may distribute the Covered Software under the terms of the version 324 | of the License under which You originally received the Covered Software, 325 | or under the terms of any subsequent version published by the license 326 | steward. 327 | 328 | 10.3. Modified Versions 329 | 330 | If you create software not governed by this License, and you want to 331 | create a new license for such software, you may create and use a 332 | modified version of this License if you rename the license and remove 333 | any references to the name of the license steward (except to note that 334 | such modified license differs from this License). 335 | 336 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 337 | Licenses If You choose to distribute Source Code Form that is 338 | Incompatible With Secondary Licenses under the terms of this version of 339 | the License, the notice described in Exhibit B of this License must be 340 | attached. 341 | 342 | Exhibit A - Source Code Form License Notice 343 | 344 | This Source Code Form is subject to the 345 | terms of the Mozilla Public License, v. 346 | 2.0. If a copy of the MPL was not 347 | distributed with this file, You can 348 | obtain one at 349 | http://mozilla.org/MPL/2.0/. 350 | 351 | If it is not possible or desirable to put the notice in a particular file, 352 | then You may include the notice in a location (such as a LICENSE file in a 353 | relevant directory) where a recipient would be likely to look for such a 354 | notice. 355 | 356 | You may add additional accurate notices of copyright ownership. 357 | 358 | Exhibit B - "Incompatible With Secondary Licenses" Notice 359 | 360 | This Source Code Form is "Incompatible 361 | With Secondary Licenses", as defined by 362 | the Mozilla Public License, v. 2.0. 363 | --------------------------------------------------------------------------------