-
Notifications
You must be signed in to change notification settings - Fork 320
/
lru.go
135 lines (118 loc) · 2.75 KB
/
lru.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (C) 2020-2021, IrineSistiana
//
// This file is part of mosdns.
//
// mosdns is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mosdns is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package lru
import (
"container/list"
"fmt"
)
type LRULike interface {
Add(key string, v interface{})
Del(key string)
Clean(f func(key string, v interface{}) (remove bool)) (removed int)
Get(key string) (v interface{}, ok bool)
Len() int
}
type LRU struct {
maxSize int
onEvict func(key string, v interface{})
l *list.List
m map[string]*list.Element
}
type listValue struct {
key string
v interface{}
}
func NewLRU(maxSize int, onEvict func(key string, v interface{})) *LRU {
if maxSize <= 0 {
panic(fmt.Sprintf("LRU: invalid max size: %d", maxSize))
}
return &LRU{
maxSize: maxSize,
onEvict: onEvict,
l: list.New(),
m: make(map[string]*list.Element),
}
}
func (q *LRU) Add(key string, v interface{}) {
e, ok := q.m[key]
if ok { // update existed key
e.Value.(*listValue).v = v
q.l.MoveToBack(e)
return
}
o := q.Len() - q.maxSize + 1
for o > 0 {
key, v, _ := q.PopOldest()
if q.onEvict != nil {
q.onEvict(key, v)
}
o--
}
q.m[key] = q.l.PushBack(&listValue{
key: key,
v: v,
})
}
func (q *LRU) Del(key string) {
e := q.m[key]
if e != nil {
q.mustDel(key, e)
}
}
func (q *LRU) mustDel(key string, e *list.Element) {
lv := q.l.Remove(e).(*listValue)
delete(q.m, key)
if q.onEvict != nil {
q.onEvict(key, lv.v)
}
}
func (q *LRU) PopOldest() (key string, v interface{}, ok bool) {
e := q.l.Front()
if e != nil {
lv := q.l.Remove(e).(*listValue)
delete(q.m, lv.key)
key, v = lv.key, lv.v
ok = true
return
}
return "", nil, false
}
func (q *LRU) Clean(f func(key string, v interface{}) (remove bool)) (removed int) {
next := q.l.Front()
for next != nil {
e := next
next = e.Next()
lv := e.Value.(*listValue)
key, v := lv.key, lv.v
if remove := f(key, v); remove {
q.mustDel(key, e)
removed++
}
}
return removed
}
func (q *LRU) Get(key string) (interface{}, bool) {
e, ok := q.m[key]
if !ok {
return nil, false
}
q.l.MoveToBack(e)
return e.Value.(*listValue).v, true
}
func (q *LRU) Len() int {
return q.l.Len()
}