-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlruk.go
57 lines (53 loc) · 1017 Bytes
/
lruk.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
package gcache
import (
"runtime"
"time"
)
type LRUK struct {
*wrapper
}
func NewLRUK(opt ...LRUKOption) (lruk *LRUK) {
opts := defaultLRUKOptions
for _, o := range opt {
o.apply(&opts)
}
// create default lru
if opts.lru == nil {
opts.lru = NewLowLRU(
WithLowLRUCapacity(opts.capacity),
WithLowLRUExpiry(opts.expiry),
)
}
// create default history
if opts.k > 1 && opts.history == nil {
capacity := opts.capacity
if opts.historyOnlyKey {
capacity *= 10
}
opts.history = NewLowLRU(
WithLowLRUCapacity(capacity),
WithLowLRUExpiry(opts.expiry),
)
}
w := &wrapper{
impl: NewLowLRUK(
opts.history, opts.lru,
WithLowLRUK(opts.k),
WithLowLRUKHistoryOnlyKey(opts.historyOnlyKey),
),
closed: make(chan struct{}),
}
lruk = &LRUK{
wrapper: w,
}
if opts.expiry > 0 {
ticker := time.NewTicker(opts.clear)
lruk.ticker = ticker
go w.clearExpired(ticker.C)
runtime.SetFinalizer(lruk, (*LRUK).stop)
}
return
}
func (l *LRUK) stop() {
l.wrapper.stop()
}