├── .github └── workflows │ └── go.yml ├── .gitignore ├── 2q.go ├── 2q_test.go ├── LICENSE ├── README.md ├── _config.yml ├── arc.go ├── arc_test.go ├── go.mod ├── lru.go ├── lru_test.go └── simplelru ├── list.go ├── lru.go └── lru_test.go /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: Build and Test 2 | 3 | on: 4 | push: 5 | branches: 6 | - 'master' 7 | pull_request: 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | fail-fast: false 14 | matrix: 15 | version: ["1.21.x", "1.22.x"] 16 | 17 | steps: 18 | - name: Check out repository 19 | uses: actions/checkout@v4 20 | 21 | - name: Set up Go 22 | uses: actions/setup-go@v5 23 | with: 24 | go-version: ${{ matrix.version }} 25 | cache: true 26 | 27 | - name: generate and test 28 | run: go test -race -cover -coverprofile=coverage.out ./... 29 | 30 | - uses: codecov/codecov-action@v4 31 | env: 32 | CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} 33 | with: 34 | files: ./coverage.out 35 | flags: unittests # optional 36 | name: codecov-umbrella # optional 37 | fail_ci_if_error: false # optional (default = false) 38 | verbose: false # optional (default = false) 39 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /2q.go: -------------------------------------------------------------------------------- 1 | package lru 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | "time" 7 | 8 | "github.com/hnlq715/golang-lru/simplelru" 9 | ) 10 | 11 | const ( 12 | // Default2QRecentRatio is the ratio of the 2Q cache dedicated 13 | // to recently added entries that have only been accessed once. 14 | Default2QRecentRatio = 0.25 15 | 16 | // Default2QGhostEntries is the default ratio of ghost 17 | // entries kept to track entries recently evicted 18 | Default2QGhostEntries = 0.50 19 | ) 20 | 21 | // TwoQueueCache is a thread-safe fixed size 2Q cache. 22 | // 2Q is an enhancement over the standard LRU cache 23 | // in that it tracks both frequently and recently used 24 | // entries separately. This avoids a burst in access to new 25 | // entries from evicting frequently used entries. It adds some 26 | // additional tracking overhead to the standard LRU cache, and is 27 | // computationally about 2x the cost, and adds some metadata over 28 | // head. The ARCCache is similar, but does not require setting any 29 | // parameters. 30 | type TwoQueueCache struct { 31 | size int 32 | recentSize int 33 | 34 | recent *simplelru.LRU 35 | frequent *simplelru.LRU 36 | recentEvict *simplelru.LRU 37 | lock sync.RWMutex 38 | } 39 | 40 | // New2Q creates a new TwoQueueCache using the default 41 | // values for the parameters. 42 | func New2Q(size int) (*TwoQueueCache, error) { 43 | return New2QWithExpire(size, 0) 44 | } 45 | 46 | // New2QParams creates a new TwoQueueCache using the provided 47 | // parameter values. 48 | func New2QParams(size int, recentRatio float64, ghostRatio float64) (*TwoQueueCache, error) { 49 | return New2QParamsWithExpire(size, 0, recentRatio, ghostRatio) 50 | } 51 | 52 | // New2QWithExpire creates a new TwoQueueCache using the default 53 | // values for the parameters with expire feature. 54 | func New2QWithExpire(size int, expire time.Duration) (*TwoQueueCache, error) { 55 | return New2QParamsWithExpire(size, expire, Default2QRecentRatio, Default2QGhostEntries) 56 | } 57 | 58 | // New2QParamsWithExpire creates a new TwoQueueCache using the provided 59 | // parameter values with expire feature. 60 | func New2QParamsWithExpire(size int, expire time.Duration, recentRatio float64, ghostRatio float64) (*TwoQueueCache, error) { 61 | if size <= 0 { 62 | return nil, fmt.Errorf("invalid size") 63 | } 64 | if recentRatio < 0.0 || recentRatio > 1.0 { 65 | return nil, fmt.Errorf("invalid recent ratio") 66 | } 67 | if ghostRatio < 0.0 || ghostRatio > 1.0 { 68 | return nil, fmt.Errorf("invalid ghost ratio") 69 | } 70 | 71 | // Determine the sub-sizes 72 | recentSize := int(float64(size) * recentRatio) 73 | evictSize := int(float64(size) * ghostRatio) 74 | 75 | // Allocate the LRUs 76 | recent, err := simplelru.NewLRUWithExpire(size, expire, nil) 77 | if err != nil { 78 | return nil, err 79 | } 80 | frequent, err := simplelru.NewLRUWithExpire(size, expire, nil) 81 | if err != nil { 82 | return nil, err 83 | } 84 | recentEvict, err := simplelru.NewLRUWithExpire(evictSize, expire, nil) 85 | if err != nil { 86 | return nil, err 87 | } 88 | 89 | // Initialize the cache 90 | c := &TwoQueueCache{ 91 | size: size, 92 | recentSize: recentSize, 93 | recent: recent, 94 | frequent: frequent, 95 | recentEvict: recentEvict, 96 | } 97 | return c, nil 98 | } 99 | 100 | func (c *TwoQueueCache) Get(key interface{}) (interface{}, bool) { 101 | c.lock.Lock() 102 | defer c.lock.Unlock() 103 | 104 | // Check if this is a frequent value 105 | if val, ok := c.frequent.Get(key); ok { 106 | return val, ok 107 | } 108 | 109 | // If the value is contained in recent, then we 110 | // promote it to frequent 111 | if val, expire, ok := c.recent.PeekWithExpireTime(key); ok { 112 | c.recent.Remove(key) 113 | var expireDuration time.Duration 114 | if expire != nil { 115 | expireDuration = expire.Sub(time.Now()) 116 | if expireDuration < 0 { 117 | return nil, false 118 | } 119 | } 120 | c.frequent.AddEx(key, val, expireDuration) 121 | return val, ok 122 | } 123 | 124 | // No hit 125 | return nil, false 126 | } 127 | 128 | func (c *TwoQueueCache) Add(key, value interface{}) { 129 | c.AddEx(key, value, 0) 130 | } 131 | 132 | func (c *TwoQueueCache) AddEx(key, value interface{}, expire time.Duration) { 133 | c.lock.Lock() 134 | defer c.lock.Unlock() 135 | 136 | // Check if the value is frequently used already, 137 | // and just update the value 138 | if c.frequent.Contains(key) { 139 | c.frequent.AddEx(key, value, expire) 140 | return 141 | } 142 | 143 | // Check if the value is recently used, and promote 144 | // the value into the frequent list 145 | if c.recent.Contains(key) { 146 | c.recent.Remove(key) 147 | c.frequent.AddEx(key, value, expire) 148 | return 149 | } 150 | 151 | // If the value was recently evicted, add it to the 152 | // frequently used list 153 | if c.recentEvict.Contains(key) { 154 | c.ensureSpace(true) 155 | c.recentEvict.Remove(key) 156 | c.frequent.AddEx(key, value, expire) 157 | return 158 | } 159 | 160 | // Add to the recently seen list 161 | c.ensureSpace(false) 162 | c.recent.AddEx(key, value, expire) 163 | return 164 | } 165 | 166 | // ensureSpace is used to ensure we have space in the cache 167 | func (c *TwoQueueCache) ensureSpace(recentEvict bool) { 168 | // If we have space, nothing to do 169 | recentLen := c.recent.Len() 170 | freqLen := c.frequent.Len() 171 | if recentLen+freqLen < c.size { 172 | return 173 | } 174 | 175 | // If the recent buffer is larger than 176 | // the target, evict from there 177 | if recentLen > 0 && (recentLen > c.recentSize || (recentLen == c.recentSize && !recentEvict)) { 178 | k, _, _ := c.recent.RemoveOldest() 179 | c.recentEvict.Add(k, nil) 180 | return 181 | } 182 | 183 | // Remove from the frequent list otherwise 184 | c.frequent.RemoveOldest() 185 | } 186 | 187 | func (c *TwoQueueCache) Len() int { 188 | c.lock.RLock() 189 | defer c.lock.RUnlock() 190 | return c.recent.Len() + c.frequent.Len() 191 | } 192 | 193 | func (c *TwoQueueCache) Keys() []interface{} { 194 | c.lock.RLock() 195 | defer c.lock.RUnlock() 196 | k1 := c.frequent.Keys() 197 | k2 := c.recent.Keys() 198 | return append(k1, k2...) 199 | } 200 | 201 | func (c *TwoQueueCache) Remove(key interface{}) { 202 | c.lock.Lock() 203 | defer c.lock.Unlock() 204 | if c.frequent.Remove(key) { 205 | return 206 | } 207 | if c.recent.Remove(key) { 208 | return 209 | } 210 | if c.recentEvict.Remove(key) { 211 | return 212 | } 213 | } 214 | 215 | func (c *TwoQueueCache) Purge() { 216 | c.lock.Lock() 217 | defer c.lock.Unlock() 218 | c.recent.Purge() 219 | c.frequent.Purge() 220 | c.recentEvict.Purge() 221 | } 222 | 223 | func (c *TwoQueueCache) Contains(key interface{}) bool { 224 | c.lock.RLock() 225 | defer c.lock.RUnlock() 226 | return c.frequent.Contains(key) || c.recent.Contains(key) 227 | } 228 | 229 | func (c *TwoQueueCache) Peek(key interface{}) (interface{}, bool) { 230 | c.lock.RLock() 231 | defer c.lock.RUnlock() 232 | if val, ok := c.frequent.Peek(key); ok { 233 | return val, ok 234 | } 235 | return c.recent.Peek(key) 236 | } 237 | -------------------------------------------------------------------------------- /2q_test.go: -------------------------------------------------------------------------------- 1 | package lru 2 | 3 | import ( 4 | "math/rand" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | func Benchmark2Q_Rand(b *testing.B) { 10 | l, err := New2Q(8192) 11 | if err != nil { 12 | b.Fatalf("err: %v", err) 13 | } 14 | 15 | trace := make([]int64, b.N*2) 16 | for i := 0; i < b.N*2; i++ { 17 | trace[i] = rand.Int63() % 32768 18 | } 19 | 20 | b.ResetTimer() 21 | 22 | var hit, miss int 23 | for i := 0; i < 2*b.N; i++ { 24 | if i%2 == 0 { 25 | l.Add(trace[i], trace[i]) 26 | } else { 27 | _, ok := l.Get(trace[i]) 28 | if ok { 29 | hit++ 30 | } else { 31 | miss++ 32 | } 33 | } 34 | } 35 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss)) 36 | } 37 | 38 | func Benchmark2Q_Freq(b *testing.B) { 39 | l, err := New2Q(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] = rand.Int63() % 16384 48 | } else { 49 | trace[i] = rand.Int63() % 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 | _, ok := l.Get(trace[i]) 61 | if ok { 62 | hit++ 63 | } else { 64 | miss++ 65 | } 66 | } 67 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss)) 68 | } 69 | 70 | func Test2Q_RandomOps(t *testing.T) { 71 | size := 128 72 | l, err := New2Q(128) 73 | if err != nil { 74 | t.Fatalf("err: %v", err) 75 | } 76 | 77 | n := 200000 78 | for i := 0; i < n; i++ { 79 | key := rand.Int63() % 512 80 | r := rand.Int63() 81 | switch r % 3 { 82 | case 0: 83 | l.Add(key, key) 84 | case 1: 85 | l.Get(key) 86 | case 2: 87 | l.Remove(key) 88 | } 89 | 90 | if l.recent.Len()+l.frequent.Len() > size { 91 | t.Fatalf("bad: recent: %d freq: %d", 92 | l.recent.Len(), l.frequent.Len()) 93 | } 94 | } 95 | } 96 | 97 | func Test2Q_Get_RecentToFrequent(t *testing.T) { 98 | l, err := New2Q(128) 99 | if err != nil { 100 | t.Fatalf("err: %v", err) 101 | } 102 | 103 | // Touch all the entries, should be in t1 104 | for i := 0; i < 128; i++ { 105 | l.Add(i, i) 106 | } 107 | if n := l.recent.Len(); n != 128 { 108 | t.Fatalf("bad: %d", n) 109 | } 110 | if n := l.frequent.Len(); n != 0 { 111 | t.Fatalf("bad: %d", n) 112 | } 113 | 114 | // Get should upgrade to t2 115 | for i := 0; i < 128; i++ { 116 | _, ok := l.Get(i) 117 | if !ok { 118 | t.Fatalf("missing: %d", i) 119 | } 120 | } 121 | if n := l.recent.Len(); n != 0 { 122 | t.Fatalf("bad: %d", n) 123 | } 124 | if n := l.frequent.Len(); n != 128 { 125 | t.Fatalf("bad: %d", n) 126 | } 127 | 128 | // Get be from t2 129 | for i := 0; i < 128; i++ { 130 | _, ok := l.Get(i) 131 | if !ok { 132 | t.Fatalf("missing: %d", i) 133 | } 134 | } 135 | if n := l.recent.Len(); n != 0 { 136 | t.Fatalf("bad: %d", n) 137 | } 138 | if n := l.frequent.Len(); n != 128 { 139 | t.Fatalf("bad: %d", n) 140 | } 141 | } 142 | 143 | func Test2Q_Add_RecentToFrequent(t *testing.T) { 144 | l, err := New2Q(128) 145 | if err != nil { 146 | t.Fatalf("err: %v", err) 147 | } 148 | 149 | // Add initially to recent 150 | l.Add(1, 1) 151 | if n := l.recent.Len(); n != 1 { 152 | t.Fatalf("bad: %d", n) 153 | } 154 | if n := l.frequent.Len(); n != 0 { 155 | t.Fatalf("bad: %d", n) 156 | } 157 | 158 | // Add should upgrade to frequent 159 | l.Add(1, 1) 160 | if n := l.recent.Len(); n != 0 { 161 | t.Fatalf("bad: %d", n) 162 | } 163 | if n := l.frequent.Len(); n != 1 { 164 | t.Fatalf("bad: %d", n) 165 | } 166 | 167 | // Add should remain in frequent 168 | l.Add(1, 1) 169 | if n := l.recent.Len(); n != 0 { 170 | t.Fatalf("bad: %d", n) 171 | } 172 | if n := l.frequent.Len(); n != 1 { 173 | t.Fatalf("bad: %d", n) 174 | } 175 | } 176 | 177 | func Test2Q_Add_RecentEvict(t *testing.T) { 178 | l, err := New2Q(4) 179 | if err != nil { 180 | t.Fatalf("err: %v", err) 181 | } 182 | 183 | // Add 1,2,3,4,5 -> Evict 1 184 | l.Add(1, 1) 185 | l.Add(2, 2) 186 | l.Add(3, 3) 187 | l.Add(4, 4) 188 | l.Add(5, 5) 189 | if n := l.recent.Len(); n != 4 { 190 | t.Fatalf("bad: %d", n) 191 | } 192 | if n := l.recentEvict.Len(); n != 1 { 193 | t.Fatalf("bad: %d", n) 194 | } 195 | if n := l.frequent.Len(); n != 0 { 196 | t.Fatalf("bad: %d", n) 197 | } 198 | 199 | // Pull in the recently evicted 200 | l.Add(1, 1) 201 | if n := l.recent.Len(); n != 3 { 202 | t.Fatalf("bad: %d", n) 203 | } 204 | if n := l.recentEvict.Len(); n != 1 { 205 | t.Fatalf("bad: %d", n) 206 | } 207 | if n := l.frequent.Len(); n != 1 { 208 | t.Fatalf("bad: %d", n) 209 | } 210 | 211 | // Add 6, should cause another recent evict 212 | l.Add(6, 6) 213 | if n := l.recent.Len(); n != 3 { 214 | t.Fatalf("bad: %d", n) 215 | } 216 | if n := l.recentEvict.Len(); n != 2 { 217 | t.Fatalf("bad: %d", n) 218 | } 219 | if n := l.frequent.Len(); n != 1 { 220 | t.Fatalf("bad: %d", n) 221 | } 222 | } 223 | 224 | func Test2Q(t *testing.T) { 225 | l, err := New2Q(128) 226 | if err != nil { 227 | t.Fatalf("err: %v", err) 228 | } 229 | 230 | for i := 0; i < 256; i++ { 231 | l.Add(i, i) 232 | } 233 | if l.Len() != 128 { 234 | t.Fatalf("bad len: %v", l.Len()) 235 | } 236 | 237 | for i, k := range l.Keys() { 238 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 239 | t.Fatalf("bad key: %v", k) 240 | } 241 | } 242 | for i := 0; i < 128; i++ { 243 | _, ok := l.Get(i) 244 | if ok { 245 | t.Fatalf("should be evicted") 246 | } 247 | } 248 | for i := 128; i < 256; i++ { 249 | _, ok := l.Get(i) 250 | if !ok { 251 | t.Fatalf("should not be evicted") 252 | } 253 | } 254 | for i := 128; i < 192; i++ { 255 | l.Remove(i) 256 | _, ok := l.Get(i) 257 | if ok { 258 | t.Fatalf("should be deleted") 259 | } 260 | } 261 | 262 | l.Purge() 263 | if l.Len() != 0 { 264 | t.Fatalf("bad len: %v", l.Len()) 265 | } 266 | if _, ok := l.Get(200); ok { 267 | t.Fatalf("should contain nothing") 268 | } 269 | } 270 | 271 | // Test that Contains doesn't update recent-ness 272 | func Test2Q_Contains(t *testing.T) { 273 | l, err := New2Q(2) 274 | if err != nil { 275 | t.Fatalf("err: %v", err) 276 | } 277 | 278 | l.Add(1, 1) 279 | l.Add(2, 2) 280 | if !l.Contains(1) { 281 | t.Errorf("1 should be contained") 282 | } 283 | 284 | l.Add(3, 3) 285 | if l.Contains(1) { 286 | t.Errorf("Contains should not have updated recent-ness of 1") 287 | } 288 | } 289 | 290 | // Test that Peek doesn't update recent-ness 291 | func Test2Q_Peek(t *testing.T) { 292 | l, err := New2Q(2) 293 | if err != nil { 294 | t.Fatalf("err: %v", err) 295 | } 296 | 297 | l.Add(1, 1) 298 | l.Add(2, 2) 299 | if v, ok := l.Peek(1); !ok || v != 1 { 300 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 301 | } 302 | 303 | l.Add(3, 3) 304 | if l.Contains(1) { 305 | t.Errorf("should not have updated recent-ness of 1") 306 | } 307 | } 308 | 309 | // Test that values expire as expected 310 | func Test2Q_Expire(t *testing.T) { 311 | l, err := New2Q(100) 312 | if err != nil { 313 | t.Fatalf("failed to create LRU: %v", err) 314 | } 315 | 316 | l.AddEx("hey", "hello", 300*time.Millisecond) 317 | 318 | value, ok := l.Get("hey") 319 | if !ok { 320 | t.Fatal("failed to read back value") 321 | } 322 | if value.(string) != "hello" { 323 | t.Errorf("expected \"hello\", got %v", value) 324 | } 325 | 326 | time.Sleep(500 * time.Millisecond) 327 | _, ok = l.Get("hey") 328 | if ok { 329 | t.Errorf("cached didn't properly expire") 330 | } 331 | } 332 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | golang-lru 2 | ========== 3 | [![Build and Test](https://github.com/sysulq/golang-lru/actions/workflows/go.yml/badge.svg)](https://github.com/sysulq/golang-lru/actions/workflows/go.yml) 4 | [![Coverage](https://codecov.io/gh/sysulq/golang-lru/branch/master/graph/badge.svg)](https://codecov.io/gh/sysulq/golang-lru) 5 | 6 | This provides the `lru` package which implements a fixed-size 7 | thread safe LRU cache with expire feature. It is based on [golang-lru](https://github.com/hashicorp/golang-lru). 8 | 9 | Documentation 10 | ============= 11 | 12 | Full docs are available on [Godoc](http://godoc.org/github.com/sysulq/golang-lru) 13 | 14 | Example 15 | ======= 16 | 17 | Using the LRU is very simple: 18 | 19 | ```go 20 | l, _ := NewARCWithExpire(128, 30*time.Second) 21 | for i := 0; i < 256; i++ { 22 | l.Add(i, nil) 23 | } 24 | if l.Len() != 128 { 25 | panic(fmt.Sprintf("bad len: %v", l.Len())) 26 | } 27 | ``` 28 | 29 | Benchmarks 30 | === 31 | 32 | [without pool](https://github.com/sysulq/golang-lru/tree/master) 33 | --- 34 | ``` 35 | Running tool: /home/liqi/workspace/go/bin/go test -benchmem -run=^$ -coverprofile=/tmp/vscode-govFyHq9/go-code-cover -bench . github.com/sysulq/golang-lru 36 | 37 | goos: linux 38 | goarch: amd64 39 | pkg: github.com/sysulq/golang-lru 40 | Benchmark2Q_Rand-4 1000000 1415 ns/op 158 B/op 5 allocs/op 41 | --- BENCH: Benchmark2Q_Rand-4 42 | 2q_test.go:34: hit: 0 miss: 1 ratio: 0.000000 43 | 2q_test.go:34: hit: 1 miss: 99 ratio: 0.010101 44 | 2q_test.go:34: hit: 1381 miss: 8619 ratio: 0.160227 45 | 2q_test.go:34: hit: 248530 miss: 751470 ratio: 0.330725 46 | Benchmark2Q_Freq-4 1000000 1017 ns/op 143 B/op 5 allocs/op 47 | --- BENCH: Benchmark2Q_Freq-4 48 | 2q_test.go:66: hit: 1 miss: 0 ratio: +Inf 49 | 2q_test.go:66: hit: 100 miss: 0 ratio: +Inf 50 | 2q_test.go:66: hit: 9840 miss: 160 ratio: 61.500000 51 | 2q_test.go:66: hit: 332417 miss: 667583 ratio: 0.497941 52 | BenchmarkARC_Rand-4 1000000 1402 ns/op 193 B/op 6 allocs/op 53 | --- BENCH: BenchmarkARC_Rand-4 54 | arc_test.go:39: hit: 0 miss: 1 ratio: 0.000000 55 | arc_test.go:39: hit: 1 miss: 99 ratio: 0.010101 56 | arc_test.go:39: hit: 1398 miss: 8602 ratio: 0.162520 57 | arc_test.go:39: hit: 249099 miss: 750901 ratio: 0.331733 58 | BenchmarkARC_Freq-4 963909 1190 ns/op 166 B/op 5 allocs/op 59 | --- BENCH: BenchmarkARC_Freq-4 60 | arc_test.go:71: hit: 1 miss: 0 ratio: +Inf 61 | arc_test.go:71: hit: 100 miss: 0 ratio: +Inf 62 | arc_test.go:71: hit: 9860 miss: 140 ratio: 70.428571 63 | arc_test.go:71: hit: 310475 miss: 653434 ratio: 0.475144 64 | BenchmarkLRU_Rand-4 2287102 613 ns/op 88 B/op 3 allocs/op 65 | --- BENCH: BenchmarkLRU_Rand-4 66 | lru_test.go:34: hit: 0 miss: 1 ratio: 0.000000 67 | lru_test.go:34: hit: 0 miss: 100 ratio: 0.000000 68 | lru_test.go:34: hit: 1379 miss: 8621 ratio: 0.159958 69 | lru_test.go:34: hit: 248489 miss: 751511 ratio: 0.330653 70 | lru_test.go:34: hit: 570640 miss: 1716462 ratio: 0.332451 71 | BenchmarkLRU_Freq-4 2456690 487 ns/op 83 B/op 3 allocs/op 72 | --- BENCH: BenchmarkLRU_Freq-4 73 | lru_test.go:66: hit: 1 miss: 0 ratio: +Inf 74 | lru_test.go:66: hit: 100 miss: 0 ratio: +Inf 75 | lru_test.go:66: hit: 9846 miss: 154 ratio: 63.935065 76 | lru_test.go:66: hit: 312529 miss: 687471 ratio: 0.454607 77 | lru_test.go:66: hit: 752485 miss: 1704205 ratio: 0.441546 78 | PASS 79 | coverage: 54.9% of statements 80 | ok github.com/sysulq/golang-lru 9.138s 81 | 82 | ``` 83 | 84 | 85 | [with sync pool](https://github.com/sysulq/golang-lru/tree/feature/syncpool) 86 | --- 87 | ``` 88 | Running tool: /home/liqi/workspace/go/bin/go test -benchmem -run=^$ -coverprofile=/tmp/vscode-govFyHq9/go-code-cover -bench . github.com/sysulq/golang-lru 89 | 90 | goos: linux 91 | goarch: amd64 92 | pkg: github.com/sysulq/golang-lru 93 | Benchmark2Q_Rand-4 1000000 1090 ns/op 92 B/op 4 allocs/op 94 | --- BENCH: Benchmark2Q_Rand-4 95 | 2q_test.go:34: hit: 0 miss: 1 ratio: 0.000000 96 | 2q_test.go:34: hit: 0 miss: 100 ratio: 0.000000 97 | 2q_test.go:34: hit: 1375 miss: 8625 ratio: 0.159420 98 | 2q_test.go:34: hit: 249496 miss: 750504 ratio: 0.332438 99 | Benchmark2Q_Freq-4 1223035 944 ns/op 85 B/op 4 allocs/op 100 | --- BENCH: Benchmark2Q_Freq-4 101 | 2q_test.go:66: hit: 1 miss: 0 ratio: +Inf 102 | 2q_test.go:66: hit: 100 miss: 0 ratio: +Inf 103 | 2q_test.go:66: hit: 9872 miss: 128 ratio: 77.125000 104 | 2q_test.go:66: hit: 334464 miss: 665536 ratio: 0.502548 105 | 2q_test.go:66: hit: 405282 miss: 817753 ratio: 0.495604 106 | BenchmarkARC_Rand-4 1000000 1330 ns/op 111 B/op 4 allocs/op 107 | --- BENCH: BenchmarkARC_Rand-4 108 | arc_test.go:39: hit: 0 miss: 1 ratio: 0.000000 109 | arc_test.go:39: hit: 0 miss: 100 ratio: 0.000000 110 | arc_test.go:39: hit: 1368 miss: 8632 ratio: 0.158480 111 | arc_test.go:39: hit: 248419 miss: 751581 ratio: 0.330529 112 | BenchmarkARC_Freq-4 1000000 1090 ns/op 93 B/op 4 allocs/op 113 | --- BENCH: BenchmarkARC_Freq-4 114 | arc_test.go:71: hit: 1 miss: 0 ratio: +Inf 115 | arc_test.go:71: hit: 100 miss: 0 ratio: +Inf 116 | arc_test.go:71: hit: 9876 miss: 124 ratio: 79.645161 117 | arc_test.go:71: hit: 337535 miss: 662465 ratio: 0.509514 118 | BenchmarkLRU_Rand-4 2327682 509 ns/op 52 B/op 2 allocs/op 119 | --- BENCH: BenchmarkLRU_Rand-4 120 | lru_test.go:34: hit: 0 miss: 1 ratio: 0.000000 121 | lru_test.go:34: hit: 1 miss: 99 ratio: 0.010101 122 | lru_test.go:34: hit: 1478 miss: 8522 ratio: 0.173433 123 | lru_test.go:34: hit: 249019 miss: 750981 ratio: 0.331592 124 | lru_test.go:34: hit: 580746 miss: 1746936 ratio: 0.332437 125 | BenchmarkLRU_Freq-4 2630702 475 ns/op 49 B/op 2 allocs/op 126 | --- BENCH: BenchmarkLRU_Freq-4 127 | lru_test.go:66: hit: 1 miss: 0 ratio: +Inf 128 | lru_test.go:66: hit: 100 miss: 0 ratio: +Inf 129 | lru_test.go:66: hit: 9784 miss: 216 ratio: 45.296296 130 | lru_test.go:66: hit: 311783 miss: 688217 ratio: 0.453030 131 | lru_test.go:66: hit: 810266 miss: 1820436 ratio: 0.445094 132 | PASS 133 | coverage: 55.3% of statements 134 | ok github.com/sysulq/golang-lru 9.714s 135 | ``` 136 | 137 | 138 | 139 | [with list pool](https://github.com/sysulq/golang-lru/tree/feature/listpool) 140 | --- 141 | ``` 142 | Running tool: /home/liqi/workspace/go/bin/go test -benchmem -run=^$ -coverprofile=/tmp/vscode-govFyHq9/go-code-cover -bench . github.com/sysulq/golang-lru 143 | 144 | goos: linux 145 | goarch: amd64 146 | pkg: github.com/sysulq/golang-lru 147 | Benchmark2Q_Rand-4 1000000 1311 ns/op 26 B/op 2 allocs/op 148 | --- BENCH: Benchmark2Q_Rand-4 149 | 2q_test.go:34: hit: 0 miss: 1 ratio: 0.000000 150 | 2q_test.go:34: hit: 0 miss: 100 ratio: 0.000000 151 | 2q_test.go:34: hit: 1320 miss: 8680 ratio: 0.152074 152 | 2q_test.go:34: hit: 249497 miss: 750503 ratio: 0.332440 153 | Benchmark2Q_Freq-4 1515253 811 ns/op 25 B/op 2 allocs/op 154 | --- BENCH: Benchmark2Q_Freq-4 155 | 2q_test.go:66: hit: 1 miss: 0 ratio: +Inf 156 | 2q_test.go:66: hit: 100 miss: 0 ratio: +Inf 157 | 2q_test.go:66: hit: 9880 miss: 120 ratio: 82.333333 158 | 2q_test.go:66: hit: 333341 miss: 666659 ratio: 0.500017 159 | 2q_test.go:66: hit: 416877 miss: 839436 ratio: 0.496616 160 | 2q_test.go:66: hit: 500425 miss: 1014828 ratio: 0.493113 161 | BenchmarkARC_Rand-4 1000000 1162 ns/op 27 B/op 2 allocs/op 162 | --- BENCH: BenchmarkARC_Rand-4 163 | arc_test.go:39: hit: 0 miss: 1 ratio: 0.000000 164 | arc_test.go:39: hit: 0 miss: 100 ratio: 0.000000 165 | arc_test.go:39: hit: 1437 miss: 8563 ratio: 0.167815 166 | arc_test.go:39: hit: 248890 miss: 751110 ratio: 0.331363 167 | BenchmarkARC_Freq-4 1418329 860 ns/op 26 B/op 2 allocs/op 168 | --- BENCH: BenchmarkARC_Freq-4 169 | arc_test.go:71: hit: 1 miss: 0 ratio: +Inf 170 | arc_test.go:71: hit: 100 miss: 0 ratio: +Inf 171 | arc_test.go:71: hit: 9847 miss: 153 ratio: 64.359477 172 | arc_test.go:71: hit: 337917 miss: 662083 ratio: 0.510385 173 | arc_test.go:71: hit: 472261 miss: 946068 ratio: 0.499183 174 | BenchmarkLRU_Rand-4 2970676 397 ns/op 16 B/op 1 allocs/op 175 | --- BENCH: BenchmarkLRU_Rand-4 176 | lru_test.go:34: hit: 0 miss: 1 ratio: 0.000000 177 | lru_test.go:34: hit: 0 miss: 100 ratio: 0.000000 178 | lru_test.go:34: hit: 1375 miss: 8625 ratio: 0.159420 179 | lru_test.go:34: hit: 249937 miss: 750063 ratio: 0.333221 180 | lru_test.go:34: hit: 741873 miss: 2228803 ratio: 0.332857 181 | BenchmarkLRU_Freq-4 3186918 359 ns/op 16 B/op 1 allocs/op 182 | --- BENCH: BenchmarkLRU_Freq-4 183 | lru_test.go:66: hit: 1 miss: 0 ratio: +Inf 184 | lru_test.go:66: hit: 100 miss: 0 ratio: +Inf 185 | lru_test.go:66: hit: 9874 miss: 126 ratio: 78.365079 186 | lru_test.go:66: hit: 312430 miss: 687570 ratio: 0.454397 187 | lru_test.go:66: hit: 977757 miss: 2209161 ratio: 0.442592 188 | PASS 189 | coverage: 55.3% of statements 190 | ok github.com/sysulq/golang-lru 11.639s 191 | ``` 192 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /arc.go: -------------------------------------------------------------------------------- 1 | package lru 2 | 3 | import ( 4 | "sync" 5 | "time" 6 | 7 | "github.com/hnlq715/golang-lru/simplelru" 8 | ) 9 | 10 | // ARCCache is a thread-safe fixed size Adaptive Replacement Cache (ARC). 11 | // ARC is an enhancement over the standard LRU cache in that tracks both 12 | // frequency and recency of use. This avoids a burst in access to new 13 | // entries from evicting the frequently used older entries. It adds some 14 | // additional tracking overhead to a standard LRU cache, computationally 15 | // it is roughly 2x the cost, and the extra memory overhead is linear 16 | // with the size of the cache. ARC has been patented by IBM, but is 17 | // similar to the TwoQueueCache (2Q) which requires setting parameters. 18 | type ARCCache struct { 19 | size int // Size is the total capacity of the cache 20 | p int // P is the dynamic preference towards T1 or T2 21 | 22 | t1 *simplelru.LRU // T1 is the LRU for recently accessed items 23 | b1 *simplelru.LRU // B1 is the LRU for evictions from t1 24 | 25 | t2 *simplelru.LRU // T2 is the LRU for frequently accessed items 26 | b2 *simplelru.LRU // B2 is the LRU for evictions from t2 27 | 28 | lock sync.RWMutex 29 | } 30 | 31 | // NewARC creates an ARC of the given size 32 | func NewARC(size int) (*ARCCache, error) { 33 | return NewARCWithExpire(size, 0) 34 | } 35 | 36 | // NewARCWithExpire creates an ARC of the given size 37 | func NewARCWithExpire(size int, expire time.Duration) (*ARCCache, error) { 38 | // Create the sub LRUs 39 | b1, err := simplelru.NewLRUWithExpire(size, expire, nil) 40 | if err != nil { 41 | return nil, err 42 | } 43 | b2, err := simplelru.NewLRUWithExpire(size, expire, nil) 44 | if err != nil { 45 | return nil, err 46 | } 47 | t1, err := simplelru.NewLRUWithExpire(size, expire, nil) 48 | if err != nil { 49 | return nil, err 50 | } 51 | t2, err := simplelru.NewLRUWithExpire(size, expire, nil) 52 | if err != nil { 53 | return nil, err 54 | } 55 | 56 | // Initialize the ARC 57 | c := &ARCCache{ 58 | size: size, 59 | p: 0, 60 | t1: t1, 61 | b1: b1, 62 | t2: t2, 63 | b2: b2, 64 | } 65 | return c, nil 66 | } 67 | 68 | // Get looks up a key's value from the cache. 69 | func (c *ARCCache) Get(key interface{}) (interface{}, bool) { 70 | c.lock.Lock() 71 | defer c.lock.Unlock() 72 | 73 | // Ff the value is contained in T1 (recent), then 74 | // promote it to T2 (frequent) 75 | if val, ok := c.t1.Peek(key); ok { 76 | c.t1.Remove(key) 77 | c.t2.Add(key, val) 78 | return val, ok 79 | } 80 | 81 | // Check if the value is contained in T2 (frequent) 82 | if val, ok := c.t2.Get(key); ok { 83 | return val, ok 84 | } 85 | 86 | // No hit 87 | return nil, false 88 | } 89 | 90 | // Add adds a value to the cache. 91 | func (c *ARCCache) Add(key, value interface{}) { 92 | c.AddEx(key, value, 0) 93 | } 94 | 95 | // AddEx adds a value to the cache. 96 | func (c *ARCCache) AddEx(key, value interface{}, expire time.Duration) { 97 | c.lock.Lock() 98 | defer c.lock.Unlock() 99 | 100 | // Check if the value is contained in T1 (recent), and potentially 101 | // promote it to frequent T2 102 | if c.t1.Contains(key) { 103 | c.t1.Remove(key) 104 | c.t2.AddEx(key, value, expire) 105 | return 106 | } 107 | 108 | // Check if the value is already in T2 (frequent) and update it 109 | if c.t2.Contains(key) { 110 | c.t2.AddEx(key, value, expire) 111 | return 112 | } 113 | 114 | // Check if this value was recently evicted as part of the 115 | // recently used list 116 | if c.b1.Contains(key) { 117 | // T1 set is too small, increase P appropriately 118 | delta := 1 119 | b1Len := c.b1.Len() 120 | b2Len := c.b2.Len() 121 | if b2Len > b1Len { 122 | delta = b2Len / b1Len 123 | } 124 | if c.p+delta >= c.size { 125 | c.p = c.size 126 | } else { 127 | c.p += delta 128 | } 129 | 130 | // Potentially need to make room in the cache 131 | if c.t1.Len()+c.t2.Len() >= c.size { 132 | c.replace(false) 133 | } 134 | 135 | // Remove from B1 136 | c.b1.Remove(key) 137 | 138 | // Add the key to the frequently used list 139 | c.t2.AddEx(key, value, expire) 140 | return 141 | } 142 | 143 | // Check if this value was recently evicted as part of the 144 | // frequently used list 145 | if c.b2.Contains(key) { 146 | // T2 set is too small, decrease P appropriately 147 | delta := 1 148 | b1Len := c.b1.Len() 149 | b2Len := c.b2.Len() 150 | if b1Len > b2Len { 151 | delta = b1Len / b2Len 152 | } 153 | if delta >= c.p { 154 | c.p = 0 155 | } else { 156 | c.p -= delta 157 | } 158 | 159 | // Potentially need to make room in the cache 160 | if c.t1.Len()+c.t2.Len() >= c.size { 161 | c.replace(true) 162 | } 163 | 164 | // Remove from B2 165 | c.b2.Remove(key) 166 | 167 | // Add the key to the frequntly used list 168 | c.t2.AddEx(key, value, expire) 169 | return 170 | } 171 | 172 | // Potentially need to make room in the cache 173 | if c.t1.Len()+c.t2.Len() >= c.size { 174 | c.replace(false) 175 | } 176 | 177 | // Keep the size of the ghost buffers trim 178 | if c.b1.Len() > c.size-c.p { 179 | c.b1.RemoveOldest() 180 | } 181 | if c.b2.Len() > c.p { 182 | c.b2.RemoveOldest() 183 | } 184 | 185 | // Add to the recently seen list 186 | c.t1.AddEx(key, value, expire) 187 | return 188 | } 189 | 190 | // replace is used to adaptively evict from either T1 or T2 191 | // based on the current learned value of P 192 | func (c *ARCCache) replace(b2ContainsKey bool) { 193 | t1Len := c.t1.Len() 194 | if t1Len > 0 && (t1Len > c.p || (t1Len == c.p && b2ContainsKey)) { 195 | k, _, ok := c.t1.RemoveOldest() 196 | if ok { 197 | c.b1.Add(k, nil) 198 | } 199 | } else { 200 | k, _, ok := c.t2.RemoveOldest() 201 | if ok { 202 | c.b2.Add(k, nil) 203 | } 204 | } 205 | } 206 | 207 | // Len returns the number of cached entries 208 | func (c *ARCCache) Len() int { 209 | c.lock.RLock() 210 | defer c.lock.RUnlock() 211 | return c.t1.Len() + c.t2.Len() 212 | } 213 | 214 | // Keys returns all the cached keys 215 | func (c *ARCCache) Keys() []interface{} { 216 | c.lock.RLock() 217 | defer c.lock.RUnlock() 218 | k1 := c.t1.Keys() 219 | k2 := c.t2.Keys() 220 | return append(k1, k2...) 221 | } 222 | 223 | // Remove is used to purge a key from the cache 224 | func (c *ARCCache) Remove(key interface{}) { 225 | c.lock.Lock() 226 | defer c.lock.Unlock() 227 | if c.t1.Remove(key) { 228 | return 229 | } 230 | if c.t2.Remove(key) { 231 | return 232 | } 233 | if c.b1.Remove(key) { 234 | return 235 | } 236 | if c.b2.Remove(key) { 237 | return 238 | } 239 | } 240 | 241 | // Purge is used to clear the cache 242 | func (c *ARCCache) Purge() { 243 | c.lock.Lock() 244 | defer c.lock.Unlock() 245 | c.t1.Purge() 246 | c.t2.Purge() 247 | c.b1.Purge() 248 | c.b2.Purge() 249 | } 250 | 251 | // Contains is used to check if the cache contains a key 252 | // without updating recency or frequency. 253 | func (c *ARCCache) Contains(key interface{}) bool { 254 | c.lock.RLock() 255 | defer c.lock.RUnlock() 256 | return c.t1.Contains(key) || c.t2.Contains(key) 257 | } 258 | 259 | // Peek is used to inspect the cache value of a key 260 | // without updating recency or frequency. 261 | func (c *ARCCache) Peek(key interface{}) (interface{}, bool) { 262 | c.lock.RLock() 263 | defer c.lock.RUnlock() 264 | if val, ok := c.t1.Peek(key); ok { 265 | return val, ok 266 | } 267 | return c.t2.Peek(key) 268 | } 269 | -------------------------------------------------------------------------------- /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(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(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(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(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(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(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(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(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(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 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/hnlq715/golang-lru 2 | 3 | go 1.18 4 | -------------------------------------------------------------------------------- /lru.go: -------------------------------------------------------------------------------- 1 | // This package provides a simple LRU cache. It is based on the 2 | // LRU implementation in groupcache: 3 | // https://github.com/golang/groupcache/tree/master/lru 4 | package lru 5 | 6 | import ( 7 | "sync" 8 | "time" 9 | 10 | "github.com/hnlq715/golang-lru/simplelru" 11 | ) 12 | 13 | // Cache is a thread-safe fixed size LRU cache. 14 | type Cache struct { 15 | lru *simplelru.LRU 16 | lock sync.RWMutex 17 | } 18 | 19 | // New creates an LRU of the given size 20 | func New(size int) (*Cache, error) { 21 | return NewWithEvict(size, nil) 22 | } 23 | 24 | // NewWithEvict constructs a fixed size cache with the given eviction 25 | // callback. 26 | func NewWithEvict(size int, onEvicted func(key interface{}, value interface{})) (*Cache, error) { 27 | lru, err := simplelru.NewLRU(size, simplelru.EvictCallback(onEvicted)) 28 | if err != nil { 29 | return nil, err 30 | } 31 | c := &Cache{ 32 | lru: lru, 33 | } 34 | return c, nil 35 | } 36 | 37 | // NewWithExpire constructs a fixed size cache with expire feature 38 | func NewWithExpire(size int, expire time.Duration) (*Cache, error) { 39 | lru, err := simplelru.NewLRUWithExpire(size, expire, nil) 40 | if err != nil { 41 | return nil, err 42 | } 43 | c := &Cache{ 44 | lru: lru, 45 | } 46 | return c, nil 47 | } 48 | 49 | // NewWithExpireAndEvict constructs a fixed size cache with expire 50 | //and with the given eviction callback 51 | func NewWithExpireAndEvict(size int, expire time.Duration, onEvicted func(key interface{}, value interface{})) (*Cache, error) { 52 | lru, err := simplelru.NewLRUWithExpire(size, expire, simplelru.EvictCallback(onEvicted)) 53 | if err != nil { 54 | return nil, err 55 | } 56 | c := &Cache{ 57 | lru: lru, 58 | } 59 | return c, nil 60 | } 61 | 62 | // Purge is used to completely clear the cache 63 | func (c *Cache) Purge() { 64 | c.lock.Lock() 65 | c.lru.Purge() 66 | c.lock.Unlock() 67 | } 68 | 69 | // Add adds a value to the cache. Returns true if an eviction occurred. 70 | func (c *Cache) Add(key, value interface{}) bool { 71 | c.lock.Lock() 72 | defer c.lock.Unlock() 73 | return c.lru.Add(key, value) 74 | } 75 | 76 | // AddEx adds a value to the cache. Returns true if an eviction occurred. 77 | func (c *Cache) AddEx(key, value interface{}, expire time.Duration) bool { 78 | c.lock.Lock() 79 | defer c.lock.Unlock() 80 | return c.lru.AddEx(key, value, expire) 81 | } 82 | 83 | // Get looks up a key's value from the cache. 84 | func (c *Cache) Get(key interface{}) (interface{}, bool) { 85 | c.lock.Lock() 86 | defer c.lock.Unlock() 87 | return c.lru.Get(key) 88 | } 89 | 90 | // Check if a key is in the cache, without updating the recent-ness 91 | // or deleting it for being stale. 92 | func (c *Cache) Contains(key interface{}) bool { 93 | c.lock.RLock() 94 | defer c.lock.RUnlock() 95 | return c.lru.Contains(key) 96 | } 97 | 98 | // Returns the key value (or undefined if not found) without updating 99 | // the "recently used"-ness of the key. 100 | func (c *Cache) Peek(key interface{}) (interface{}, bool) { 101 | c.lock.RLock() 102 | defer c.lock.RUnlock() 103 | return c.lru.Peek(key) 104 | } 105 | 106 | // ContainsOrAdd checks if a key is in the cache without updating the 107 | // recent-ness or deleting it for being stale, and if not, adds the value. 108 | // Returns whether found and whether an eviction occurred. 109 | func (c *Cache) ContainsOrAdd(key, value interface{}) (ok, evict bool) { 110 | c.lock.Lock() 111 | defer c.lock.Unlock() 112 | 113 | if c.lru.Contains(key) { 114 | return true, false 115 | } else { 116 | evict := c.lru.Add(key, value) 117 | return false, evict 118 | } 119 | } 120 | 121 | // Remove removes the provided key from the cache. 122 | func (c *Cache) Remove(key interface{}) { 123 | c.lock.Lock() 124 | c.lru.Remove(key) 125 | c.lock.Unlock() 126 | } 127 | 128 | // RemoveOldest removes the oldest item from the cache. 129 | func (c *Cache) RemoveOldest() { 130 | c.lock.Lock() 131 | c.lru.RemoveOldest() 132 | c.lock.Unlock() 133 | } 134 | 135 | // Keys returns a slice of the keys in the cache, from oldest to newest. 136 | func (c *Cache) Keys() []interface{} { 137 | c.lock.RLock() 138 | defer c.lock.RUnlock() 139 | return c.lru.Keys() 140 | } 141 | 142 | // Len returns the number of items in the cache. 143 | func (c *Cache) Len() int { 144 | c.lock.RLock() 145 | defer c.lock.RUnlock() 146 | return c.lru.Len() 147 | } 148 | -------------------------------------------------------------------------------- /lru_test.go: -------------------------------------------------------------------------------- 1 | package lru 2 | 3 | import ( 4 | "math/rand" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | func BenchmarkLRU_Rand(b *testing.B) { 10 | l, err := New(8192) 11 | if err != nil { 12 | b.Fatalf("err: %v", err) 13 | } 14 | 15 | trace := make([]int64, b.N*2) 16 | for i := 0; i < b.N*2; i++ { 17 | trace[i] = rand.Int63() % 32768 18 | } 19 | 20 | b.ResetTimer() 21 | 22 | var hit, miss int 23 | for i := 0; i < 2*b.N; i++ { 24 | if i%2 == 0 { 25 | l.Add(trace[i], trace[i]) 26 | } else { 27 | _, ok := l.Get(trace[i]) 28 | if ok { 29 | hit++ 30 | } else { 31 | miss++ 32 | } 33 | } 34 | } 35 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss)) 36 | } 37 | 38 | func BenchmarkLRU_Freq(b *testing.B) { 39 | l, err := New(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] = rand.Int63() % 16384 48 | } else { 49 | trace[i] = rand.Int63() % 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 | _, ok := l.Get(trace[i]) 61 | if ok { 62 | hit++ 63 | } else { 64 | miss++ 65 | } 66 | } 67 | b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss)) 68 | } 69 | 70 | func TestLRU(t *testing.T) { 71 | evictCounter := 0 72 | onEvicted := func(k interface{}, v interface{}) { 73 | if k != v { 74 | t.Fatalf("Evict values not equal (%v!=%v)", k, v) 75 | } 76 | evictCounter += 1 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 | 90 | if evictCounter != 128 { 91 | t.Fatalf("bad evict count: %v", evictCounter) 92 | } 93 | 94 | for i, k := range l.Keys() { 95 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 96 | t.Fatalf("bad key: %v", k) 97 | } 98 | } 99 | for i := 0; i < 128; i++ { 100 | _, ok := l.Get(i) 101 | if ok { 102 | t.Fatalf("should be evicted") 103 | } 104 | } 105 | for i := 128; i < 256; i++ { 106 | _, ok := l.Get(i) 107 | if !ok { 108 | t.Fatalf("should not be evicted") 109 | } 110 | } 111 | for i := 128; i < 192; i++ { 112 | l.Remove(i) 113 | _, ok := l.Get(i) 114 | if ok { 115 | t.Fatalf("should be deleted") 116 | } 117 | } 118 | 119 | l.Get(192) // expect 192 to be last key in l.Keys() 120 | 121 | for i, k := range l.Keys() { 122 | if (i < 63 && k != i+193) || (i == 63 && k != 192) { 123 | t.Fatalf("out of order key: %v", k) 124 | } 125 | } 126 | 127 | l.Purge() 128 | if l.Len() != 0 { 129 | t.Fatalf("bad len: %v", l.Len()) 130 | } 131 | if _, ok := l.Get(200); ok { 132 | t.Fatalf("should contain nothing") 133 | } 134 | } 135 | 136 | // test that Add returns true/false if an eviction occurred 137 | func TestLRUAdd(t *testing.T) { 138 | evictCounter := 0 139 | onEvicted := func(k interface{}, v interface{}) { 140 | evictCounter += 1 141 | } 142 | 143 | l, err := NewWithEvict(1, onEvicted) 144 | if err != nil { 145 | t.Fatalf("err: %v", err) 146 | } 147 | 148 | if l.Add(1, 1) == true || evictCounter != 0 { 149 | t.Errorf("should not have an eviction") 150 | } 151 | if l.Add(2, 2) == false || evictCounter != 1 { 152 | t.Errorf("should have an eviction") 153 | } 154 | } 155 | 156 | // test that Contains doesn't update recent-ness 157 | func TestLRUContains(t *testing.T) { 158 | l, err := New(2) 159 | if err != nil { 160 | t.Fatalf("err: %v", err) 161 | } 162 | 163 | l.Add(1, 1) 164 | l.Add(2, 2) 165 | if !l.Contains(1) { 166 | t.Errorf("1 should be contained") 167 | } 168 | 169 | l.Add(3, 3) 170 | if l.Contains(1) { 171 | t.Errorf("Contains should not have updated recent-ness of 1") 172 | } 173 | } 174 | 175 | // test that Contains doesn't update recent-ness 176 | func TestLRUContainsOrAdd(t *testing.T) { 177 | l, err := New(2) 178 | if err != nil { 179 | t.Fatalf("err: %v", err) 180 | } 181 | 182 | l.Add(1, 1) 183 | l.Add(2, 2) 184 | contains, evict := l.ContainsOrAdd(1, 1) 185 | if !contains { 186 | t.Errorf("1 should be contained") 187 | } 188 | if evict { 189 | t.Errorf("nothing should be evicted here") 190 | } 191 | 192 | l.Add(3, 3) 193 | contains, evict = l.ContainsOrAdd(1, 1) 194 | if contains { 195 | t.Errorf("1 should not have been contained") 196 | } 197 | if !evict { 198 | t.Errorf("an eviction should have occurred") 199 | } 200 | if !l.Contains(1) { 201 | t.Errorf("now 1 should be contained") 202 | } 203 | } 204 | 205 | // test that Peek doesn't update recent-ness 206 | func TestLRUPeek(t *testing.T) { 207 | l, err := New(2) 208 | if err != nil { 209 | t.Fatalf("err: %v", err) 210 | } 211 | 212 | l.Add(1, 1) 213 | l.Add(2, 2) 214 | if v, ok := l.Peek(1); !ok || v != 1 { 215 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 216 | } 217 | 218 | l.Add(3, 3) 219 | if l.Contains(1) { 220 | t.Errorf("should not have updated recent-ness of 1") 221 | } 222 | } 223 | 224 | // test that NewWithExpireAndEvict works as expected 225 | func TestNewWithExpireAndEvict(t *testing.T) { 226 | evictCounter := 0 227 | onEvicted := func(k interface{}, v interface{}) { 228 | evictCounter += 1 229 | } 230 | 231 | 232 | expire := 200 * time.Millisecond 233 | l, err := NewWithExpireAndEvict(3, expire, onEvicted) 234 | if err != nil { 235 | t.Fatalf("err: %v", err) 236 | } 237 | //test evict by expiration 238 | l.Add(1, 1) 239 | 240 | time.Sleep(201 * time.Millisecond) //eviction counter 1 241 | 242 | l.Add(2, 2) 243 | l.Add(3, 3) 244 | 245 | //test length 246 | if l.Len() != 3 { 247 | t.Fatalf("Wrong size!") 248 | } 249 | 250 | //simple get 251 | var observed interface{} 252 | var observedInt int 253 | var ok bool 254 | if observed, ok = l.Get(2); !ok { 255 | t.Fatalf("expected value does not exist in cache") 256 | } 257 | observedInt, ok = observed.(int) 258 | if !ok { 259 | t.Fatalf("type assertion failed expected value") 260 | } 261 | 262 | if observedInt != 2 { 263 | t.Fatalf("value retrieved from cache does equal expected value") 264 | } 265 | 266 | 267 | //test evict by manual removal 268 | l.Remove(2) // increment eviction counter = 2 269 | 270 | 271 | //test evict by surpassing capacity 272 | l.Add(4, 4) 273 | l.Add(5, 5) 274 | l.Add(6, 6) 275 | //increment eviction counter = 3 276 | 277 | if evictCounter != 3 { 278 | t.Fatalf("evict handler did not increment correctly: count %v", evictCounter) 279 | } 280 | 281 | } 282 | -------------------------------------------------------------------------------- /simplelru/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 file. 4 | 5 | // Package list implements a doubly linked list. 6 | // 7 | // To iterate over a list (where l is a *List): 8 | // for e := l.Front(); e != nil; e = e.Next() { 9 | // // do something with e.Value 10 | // } 11 | // 12 | package simplelru 13 | 14 | // Element is an element of a linked list. 15 | type Element struct { 16 | // Next and previous pointers in the doubly-linked list of elements. 17 | // To simplify the implementation, internally a list l is implemented 18 | // as a ring, such that &l.root is both the next element of the last 19 | // list element (l.Back()) and the previous element of the first list 20 | // element (l.Front()). 21 | next, prev *Element 22 | 23 | // The list to which this element belongs. 24 | list *List 25 | 26 | // The value stored with this element. 27 | Value interface{} 28 | } 29 | 30 | // Next returns the next list element or nil. 31 | func (e *Element) Next() *Element { 32 | if p := e.next; e.list != nil && p != &e.list.root { 33 | return p 34 | } 35 | return nil 36 | } 37 | 38 | // Prev returns the previous list element or nil. 39 | func (e *Element) Prev() *Element { 40 | if p := e.prev; e.list != nil && p != &e.list.root { 41 | return p 42 | } 43 | return nil 44 | } 45 | 46 | // List represents a doubly linked list. 47 | // The zero value for List is an empty list ready to use. 48 | type List struct { 49 | root Element // sentinel list element, only &root, root.prev, and root.next are used 50 | len int // current list length excluding (this) sentinel element 51 | } 52 | 53 | // Init initializes or clears list l. 54 | func (l *List) Init() *List { 55 | l.root.next = &l.root 56 | l.root.prev = &l.root 57 | l.len = 0 58 | return l 59 | } 60 | 61 | // New returns an initialized list. 62 | func New() *List { return new(List).Init() } 63 | 64 | // Len returns the number of elements of list l. 65 | // The complexity is O(1). 66 | func (l *List) Len() int { return l.len } 67 | 68 | // Front returns the first element of list l or nil if the list is empty. 69 | func (l *List) Front() *Element { 70 | if l.len == 0 { 71 | return nil 72 | } 73 | return l.root.next 74 | } 75 | 76 | // Back returns the last element of list l or nil if the list is empty. 77 | func (l *List) Back() *Element { 78 | if l.len == 0 { 79 | return nil 80 | } 81 | return l.root.prev 82 | } 83 | 84 | // lazyInit lazily initializes a zero List value. 85 | func (l *List) lazyInit() { 86 | if l.root.next == nil { 87 | l.Init() 88 | } 89 | } 90 | 91 | // insert inserts e after at, increments l.len, and returns e. 92 | func (l *List) insert(e, at *Element) *Element { 93 | e.prev = at 94 | e.next = at.next 95 | e.prev.next = e 96 | e.next.prev = e 97 | e.list = l 98 | l.len++ 99 | return e 100 | } 101 | 102 | // insertValue is a convenience wrapper for insert(&Element{Value: v}, at). 103 | func (l *List) insertValue(v interface{}, at *Element) *Element { 104 | return l.insert(&Element{Value: v}, at) 105 | } 106 | 107 | // remove removes e from its list, decrements l.len, and returns e. 108 | func (l *List) remove(e *Element) *Element { 109 | e.prev.next = e.next 110 | e.next.prev = e.prev 111 | e.next = nil // avoid memory leaks 112 | e.prev = nil // avoid memory leaks 113 | e.list = nil 114 | l.len-- 115 | return e 116 | } 117 | 118 | // move moves e to next to at and returns e. 119 | func (l *List) move(e, at *Element) *Element { 120 | if e == at { 121 | return e 122 | } 123 | e.prev.next = e.next 124 | e.next.prev = e.prev 125 | 126 | e.prev = at 127 | e.next = at.next 128 | e.prev.next = e 129 | e.next.prev = e 130 | 131 | return e 132 | } 133 | 134 | // Remove removes e from l if e is an element of list l. 135 | // It returns the element value e.Value. 136 | // The element must not be nil. 137 | func (l *List) Remove(e *Element) interface{} { 138 | if e.list == l { 139 | // if e.list == l, l must have been initialized when e was inserted 140 | // in l or l == nil (e is a zero Element) and l.remove will crash 141 | l.remove(e) 142 | } 143 | return e.Value 144 | } 145 | 146 | // PushFront inserts a new element e with value v at the front of list l and returns e. 147 | func (l *List) PushFront(v interface{}) *Element { 148 | l.lazyInit() 149 | return l.insertValue(v, &l.root) 150 | } 151 | 152 | func (l *List) PushElementFront(e *Element) *Element { 153 | return l.insert(e, &l.root) 154 | } 155 | 156 | // PushBack inserts a new element e with value v at the back of list l and returns e. 157 | func (l *List) PushBack(v interface{}) *Element { 158 | l.lazyInit() 159 | return l.insertValue(v, l.root.prev) 160 | } 161 | 162 | // InsertBefore inserts a new element e with value v immediately before mark and returns e. 163 | // If mark is not an element of l, the list is not modified. 164 | // The mark must not be nil. 165 | func (l *List) InsertBefore(v interface{}, mark *Element) *Element { 166 | if mark.list != l { 167 | return nil 168 | } 169 | // see comment in List.Remove about initialization of l 170 | return l.insertValue(v, mark.prev) 171 | } 172 | 173 | // InsertAfter inserts a new element e with value v immediately after mark and returns e. 174 | // If mark is not an element of l, the list is not modified. 175 | // The mark must not be nil. 176 | func (l *List) InsertAfter(v interface{}, mark *Element) *Element { 177 | if mark.list != l { 178 | return nil 179 | } 180 | // see comment in List.Remove about initialization of l 181 | return l.insertValue(v, mark) 182 | } 183 | 184 | // MoveToFront moves element e to the front of list l. 185 | // If e is not an element of l, the list is not modified. 186 | // The element must not be nil. 187 | func (l *List) MoveToFront(e *Element) { 188 | if l.root.next == e { 189 | return 190 | } 191 | // see comment in List.Remove about initialization of l 192 | l.move(e, &l.root) 193 | } 194 | 195 | // MoveToBack moves element e to the back of list l. 196 | // If e is not an element of l, the list is not modified. 197 | // The element must not be nil. 198 | func (l *List) MoveToBack(e *Element) { 199 | if l.root.prev == e { 200 | return 201 | } 202 | // see comment in List.Remove about initialization of l 203 | l.move(e, l.root.prev) 204 | } 205 | 206 | // MoveBefore moves element e to its new position before mark. 207 | // If e or mark is not an element of l, or e == mark, the list is not modified. 208 | // The element and mark must not be nil. 209 | func (l *List) MoveBefore(e, mark *Element) { 210 | if e == mark { 211 | return 212 | } 213 | l.move(e, mark.prev) 214 | } 215 | 216 | // MoveAfter moves element e to its new position after mark. 217 | // If e or mark is not an element of l, or e == mark, the list is not modified. 218 | // The element and mark must not be nil. 219 | func (l *List) MoveAfter(e, mark *Element) { 220 | if e == mark { 221 | return 222 | } 223 | l.move(e, mark) 224 | } 225 | 226 | // PushBackList inserts a copy of another list at the back of list l. 227 | // The lists l and other may be the same. They must not be nil. 228 | func (l *List) PushBackList(other *List) { 229 | l.lazyInit() 230 | for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { 231 | l.insertValue(e.Value, l.root.prev) 232 | } 233 | } 234 | 235 | // PushFrontList inserts a copy of another list at the front of list l. 236 | // The lists l and other may be the same. They must not be nil. 237 | func (l *List) PushFrontList(other *List) { 238 | l.lazyInit() 239 | for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { 240 | l.insertValue(e.Value, &l.root) 241 | } 242 | } 243 | -------------------------------------------------------------------------------- /simplelru/lru.go: -------------------------------------------------------------------------------- 1 | package simplelru 2 | 3 | import ( 4 | "errors" 5 | "time" 6 | ) 7 | 8 | // EvictCallback is used to get a callback when a cache entry is evicted 9 | type EvictCallback func(key interface{}, value interface{}) 10 | 11 | // LRU implements a non-thread safe fixed size LRU cache 12 | type LRU struct { 13 | size int 14 | evictList *List 15 | freeList *List 16 | items map[interface{}]*Element 17 | expire time.Duration 18 | onEvict EvictCallback 19 | } 20 | 21 | // entry is used to hold a value in the evictList 22 | type entry struct { 23 | key interface{} 24 | value interface{} 25 | expire *time.Time 26 | } 27 | 28 | func (e *entry) IsExpired() bool { 29 | if e.expire == nil { 30 | return false 31 | } 32 | return time.Now().After(*e.expire) 33 | } 34 | 35 | // NewLRU constructs an LRU of the given size 36 | func NewLRU(size int, onEvict EvictCallback) (*LRU, error) { 37 | if size <= 0 { 38 | return nil, errors.New("Must provide a positive size") 39 | } 40 | c := &LRU{ 41 | size: size, 42 | evictList: New(), 43 | freeList: New(), 44 | items: make(map[interface{}]*Element), 45 | expire: 0, 46 | onEvict: onEvict, 47 | } 48 | for i := 0; i < size; i++ { 49 | c.freeList.PushFront(&entry{}) 50 | } 51 | return c, nil 52 | } 53 | 54 | // NewLRUWithExpire contrusts an LRU of the given size and expire time 55 | func NewLRUWithExpire(size int, expire time.Duration, onEvict EvictCallback) (*LRU, error) { 56 | if size <= 0 { 57 | return nil, errors.New("Must provide a positive size") 58 | } 59 | c := &LRU{ 60 | size: size, 61 | evictList: New(), 62 | freeList: New(), 63 | items: make(map[interface{}]*Element), 64 | expire: expire, 65 | onEvict: onEvict, 66 | } 67 | for i := 0; i < size; i++ { 68 | c.freeList.PushFront(&entry{}) 69 | } 70 | 71 | return c, nil 72 | } 73 | 74 | // Purge is used to completely clear the cache 75 | func (c *LRU) Purge() { 76 | for k, v := range c.items { 77 | if c.onEvict != nil { 78 | c.onEvict(k, v.Value.(*entry).value) 79 | } 80 | delete(c.items, k) 81 | } 82 | c.evictList.Init() 83 | c.freeList.Init() 84 | for i := 0; i < c.size; i++ { 85 | c.freeList.PushFront(&entry{}) 86 | } 87 | } 88 | 89 | // Add adds a value to the cache. Returns true if an eviction occurred. 90 | func (c *LRU) Add(key, value interface{}) bool { 91 | return c.AddEx(key, value, 0) 92 | } 93 | 94 | // AddEx adds a value to the cache with expire. Returns true if an eviction occurred. 95 | func (c *LRU) AddEx(key, value interface{}, expire time.Duration) bool { 96 | var ex *time.Time = nil 97 | if expire > 0 { 98 | expire := time.Now().Add(expire) 99 | ex = &expire 100 | } else if c.expire > 0 { 101 | expire := time.Now().Add(c.expire) 102 | ex = &expire 103 | } 104 | // Check for existing item 105 | if ent, ok := c.items[key]; ok { 106 | c.evictList.MoveToFront(ent) 107 | ent.Value.(*entry).value = value 108 | ent.Value.(*entry).expire = ex 109 | return false 110 | } 111 | 112 | evict := c.evictList.Len() >= c.size 113 | // Verify size not exceeded 114 | if evict { 115 | c.removeOldest() 116 | } 117 | 118 | // Add new item 119 | ent := c.freeList.Front() 120 | ent.Value.(*entry).key = key 121 | ent.Value.(*entry).value = value 122 | ent.Value.(*entry).expire = ex 123 | c.freeList.Remove(ent) 124 | c.evictList.PushElementFront(ent) 125 | c.items[key] = ent 126 | 127 | return evict 128 | } 129 | 130 | // Get looks up a key's value from the cache. 131 | func (c *LRU) Get(key interface{}) (value interface{}, ok bool) { 132 | if ent, ok := c.items[key]; ok { 133 | if ent.Value.(*entry).IsExpired() { 134 | return nil, false 135 | } 136 | c.evictList.MoveToFront(ent) 137 | return ent.Value.(*entry).value, true 138 | } 139 | return 140 | } 141 | 142 | // Check if a key is in the cache, without updating the recent-ness 143 | // or deleting it for being stale. 144 | func (c *LRU) Contains(key interface{}) (ok bool) { 145 | if ent, ok := c.items[key]; ok { 146 | if ent.Value.(*entry).IsExpired() { 147 | return false 148 | } 149 | return ok 150 | } 151 | return 152 | } 153 | 154 | // Returns the key value (or undefined if not found) without updating 155 | // the "recently used"-ness of the key. 156 | func (c *LRU) Peek(key interface{}) (value interface{}, ok bool) { 157 | v, _, ok := c.PeekWithExpireTime(key) 158 | return v, ok 159 | } 160 | 161 | // Returns the key value (or undefined if not found) and its associated expire 162 | // time without updating the "recently used"-ness of the key. 163 | func (c *LRU) PeekWithExpireTime(key interface{}) ( 164 | value interface{}, expire *time.Time, ok bool) { 165 | if ent, ok := c.items[key]; ok { 166 | if ent.Value.(*entry).IsExpired() { 167 | return nil, nil, false 168 | } 169 | return ent.Value.(*entry).value, ent.Value.(*entry).expire, true 170 | } 171 | return nil, nil, ok 172 | } 173 | 174 | // Remove removes the provided key from the cache, returning if the 175 | // key was contained. 176 | func (c *LRU) Remove(key interface{}) bool { 177 | if ent, ok := c.items[key]; ok { 178 | c.removeElement(ent) 179 | return true 180 | } 181 | return false 182 | } 183 | 184 | // RemoveOldest removes the oldest item from the cache. 185 | func (c *LRU) RemoveOldest() (interface{}, interface{}, bool) { 186 | ent := c.evictList.Back() 187 | if ent != nil { 188 | c.removeElement(ent) 189 | kv := ent.Value.(*entry) 190 | return kv.key, kv.value, true 191 | } 192 | return nil, nil, false 193 | } 194 | 195 | // GetOldest returns the oldest entry 196 | func (c *LRU) GetOldest() (interface{}, interface{}, bool) { 197 | ent := c.evictList.Back() 198 | if ent != nil { 199 | kv := ent.Value.(*entry) 200 | return kv.key, kv.value, true 201 | } 202 | return nil, nil, false 203 | } 204 | 205 | // Keys returns a slice of the keys in the cache, from oldest to newest. 206 | func (c *LRU) Keys() []interface{} { 207 | keys := make([]interface{}, len(c.items)) 208 | i := 0 209 | for ent := c.evictList.Back(); ent != nil; ent = ent.Prev() { 210 | keys[i] = ent.Value.(*entry).key 211 | i++ 212 | } 213 | return keys 214 | } 215 | 216 | // Len returns the number of items in the cache. 217 | func (c *LRU) Len() int { 218 | return c.evictList.Len() 219 | } 220 | 221 | // Resize changes the cache size. 222 | func (c *LRU) Resize(size int) (evicted int) { 223 | diff := c.Len() - size 224 | if diff < 0 { 225 | diff = 0 226 | } 227 | for i := 0; i < diff; i++ { 228 | c.removeOldest() 229 | } 230 | c.size = size 231 | return diff 232 | } 233 | 234 | // removeOldest removes the oldest item from the cache. 235 | func (c *LRU) removeOldest() { 236 | ent := c.evictList.Back() 237 | if ent != nil { 238 | c.removeElement(ent) 239 | } 240 | } 241 | 242 | // removeElement is used to remove a given list element from the cache 243 | func (c *LRU) removeElement(e *Element) { 244 | c.evictList.Remove(e) 245 | c.freeList.PushElementFront(e) 246 | kv := e.Value.(*entry) 247 | delete(c.items, kv.key) 248 | if c.onEvict != nil { 249 | c.onEvict(kv.key, kv.value) 250 | } 251 | } 252 | -------------------------------------------------------------------------------- /simplelru/lru_test.go: -------------------------------------------------------------------------------- 1 | package simplelru 2 | 3 | import ( 4 | "testing" 5 | "time" 6 | ) 7 | 8 | func TestLRU(t *testing.T) { 9 | evictCounter := 0 10 | onEvicted := func(k interface{}, v interface{}) { 11 | if k != v { 12 | t.Fatalf("Evict values not equal (%v!=%v)", k, v) 13 | } 14 | evictCounter += 1 15 | } 16 | l, err := NewLRU(128, onEvicted) 17 | if err != nil { 18 | t.Fatalf("err: %v", err) 19 | } 20 | 21 | for i := 0; i < 256; i++ { 22 | l.Add(i, i) 23 | } 24 | if l.Len() != 128 { 25 | t.Fatalf("bad len: %v", l.Len()) 26 | } 27 | 28 | if evictCounter != 128 { 29 | t.Fatalf("bad evict count: %v", evictCounter) 30 | } 31 | 32 | for i, k := range l.Keys() { 33 | if v, ok := l.Get(k); !ok || v != k || v != i+128 { 34 | t.Fatalf("bad key: %v", k) 35 | } 36 | } 37 | for i := 0; i < 128; i++ { 38 | _, ok := l.Get(i) 39 | if ok { 40 | t.Fatalf("should be evicted") 41 | } 42 | } 43 | for i := 128; i < 256; i++ { 44 | _, ok := l.Get(i) 45 | if !ok { 46 | t.Fatalf("should not be evicted") 47 | } 48 | } 49 | for i := 128; i < 192; i++ { 50 | ok := l.Remove(i) 51 | if !ok { 52 | t.Fatalf("should be contained") 53 | } 54 | ok = l.Remove(i) 55 | if ok { 56 | t.Fatalf("should not be contained") 57 | } 58 | _, ok = l.Get(i) 59 | if ok { 60 | t.Fatalf("should be deleted") 61 | } 62 | } 63 | 64 | l.Get(192) // expect 192 to be last key in l.Keys() 65 | 66 | for i, k := range l.Keys() { 67 | if (i < 63 && k != i+193) || (i == 63 && k != 192) { 68 | t.Fatalf("out of order key: %v", k) 69 | } 70 | } 71 | 72 | l.Purge() 73 | if l.Len() != 0 { 74 | t.Fatalf("bad len: %v", l.Len()) 75 | } 76 | if _, ok := l.Get(200); ok { 77 | t.Fatalf("should contain nothing") 78 | } 79 | } 80 | 81 | func TestLRU_GetOldest_RemoveOldest(t *testing.T) { 82 | l, err := NewLRU(128, nil) 83 | if err != nil { 84 | t.Fatalf("err: %v", err) 85 | } 86 | for i := 0; i < 256; i++ { 87 | l.Add(i, i) 88 | } 89 | k, _, ok := l.GetOldest() 90 | if !ok { 91 | t.Fatalf("missing") 92 | } 93 | if k.(int) != 128 { 94 | t.Fatalf("bad: %v", k) 95 | } 96 | 97 | k, _, ok = l.RemoveOldest() 98 | if !ok { 99 | t.Fatalf("missing") 100 | } 101 | if k.(int) != 128 { 102 | t.Fatalf("bad: %v", k) 103 | } 104 | 105 | k, _, ok = l.RemoveOldest() 106 | if !ok { 107 | t.Fatalf("missing") 108 | } 109 | if k.(int) != 129 { 110 | t.Fatalf("bad: %v", k) 111 | } 112 | } 113 | 114 | // Test that Add returns true/false if an eviction occurred 115 | func TestLRU_Add(t *testing.T) { 116 | evictCounter := 0 117 | onEvicted := func(k interface{}, v interface{}) { 118 | evictCounter += 1 119 | } 120 | 121 | l, err := NewLRU(1, onEvicted) 122 | if err != nil { 123 | t.Fatalf("err: %v", err) 124 | } 125 | 126 | if l.Add(1, 1) == true || evictCounter != 0 { 127 | t.Errorf("should not have an eviction") 128 | } 129 | if l.Add(2, 2) == false || evictCounter != 1 { 130 | t.Errorf("should have an eviction") 131 | } 132 | } 133 | 134 | // Test that Contains doesn't update recent-ness 135 | func TestLRU_Contains(t *testing.T) { 136 | l, err := NewLRU(2, nil) 137 | if err != nil { 138 | t.Fatalf("err: %v", err) 139 | } 140 | 141 | l.Add(1, 1) 142 | l.Add(2, 2) 143 | if !l.Contains(1) { 144 | t.Errorf("1 should be contained") 145 | } 146 | 147 | l.Add(3, 3) 148 | if l.Contains(1) { 149 | t.Errorf("Contains should not have updated recent-ness of 1") 150 | } 151 | } 152 | 153 | // Test that Peek doesn't update recent-ness 154 | func TestLRU_Peek(t *testing.T) { 155 | l, err := NewLRU(2, nil) 156 | if err != nil { 157 | t.Fatalf("err: %v", err) 158 | } 159 | 160 | l.Add(1, 1) 161 | l.Add(2, 2) 162 | if v, ok := l.Peek(1); !ok || v != 1 { 163 | t.Errorf("1 should be set to 1: %v, %v", v, ok) 164 | } 165 | 166 | l.Add(3, 3) 167 | if l.Contains(1) { 168 | t.Errorf("should not have updated recent-ness of 1") 169 | } 170 | } 171 | 172 | // Test that expire feature 173 | func TestLRU_Expire(t *testing.T) { 174 | l, err := NewLRUWithExpire(2, 2*time.Second, nil) 175 | if err != nil { 176 | t.Fatalf("err: %v", err) 177 | } 178 | 179 | l.Add(1, 1) 180 | 181 | if !l.Contains(1) { 182 | t.Errorf("1 should be contained") 183 | } 184 | time.Sleep(2 * time.Second) 185 | if l.Contains(1) { 186 | t.Errorf("1 should not be contained") 187 | } 188 | 189 | l.AddEx(1, 1, 1*time.Second) 190 | 191 | if !l.Contains(1) { 192 | t.Errorf("1 should be contained") 193 | } 194 | time.Sleep(1 * time.Second) 195 | if l.Contains(1) { 196 | t.Errorf("1 should not be contained") 197 | } 198 | } 199 | 200 | // Test that Resize can upsize and downsize 201 | func TestLRU_Resize(t *testing.T) { 202 | onEvictCounter := 0 203 | onEvicted := func(k interface{}, v interface{}) { 204 | onEvictCounter++ 205 | } 206 | l, err := NewLRU(2, onEvicted) 207 | if err != nil { 208 | t.Fatalf("err: %v", err) 209 | } 210 | 211 | // Downsize 212 | l.Add(1, 1) 213 | l.Add(2, 2) 214 | evicted := l.Resize(1) 215 | if evicted != 1 { 216 | t.Errorf("1 element should have been evicted: %v", evicted) 217 | } 218 | if onEvictCounter != 1 { 219 | t.Errorf("onEvicted should have been called 1 time: %v", onEvictCounter) 220 | } 221 | 222 | l.Add(3, 3) 223 | if l.Contains(1) { 224 | t.Errorf("Element 1 should have been evicted") 225 | } 226 | 227 | // Upsize 228 | evicted = l.Resize(2) 229 | if evicted != 0 { 230 | t.Errorf("0 elements should have been evicted: %v", evicted) 231 | } 232 | 233 | l.Add(4, 4) 234 | if !l.Contains(3) || !l.Contains(4) { 235 | t.Errorf("Cache should have contained 2 elements") 236 | } 237 | } 238 | 239 | func BenchmarkAdd(b *testing.B) { 240 | l, _ := NewLRU(1000, nil) 241 | b.ResetTimer() 242 | 243 | b.Run("Add with small keys", func(b *testing.B) { 244 | for i := 0; i < b.N; i++ { 245 | l.Add(i%100, "this is a foo bar") 246 | } 247 | }) 248 | 249 | b.Run("Add with large keys", func(b *testing.B) { 250 | for i := 0; i < b.N; i++ { 251 | l.Add(i%10000, "this is a foo bar") 252 | } 253 | }) 254 | } 255 | --------------------------------------------------------------------------------