-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathlru.go
145 lines (120 loc) · 3.1 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
136
137
138
139
140
141
142
143
144
145
package peer
import (
"context"
"sync"
"time"
"golang.org/x/sync/singleflight"
"github.com/gotd/td/clock"
"github.com/gotd/td/tg"
)
// LRUResolver is simple decorator for Resolver to cache result in LRU.
type LRUResolver struct {
next Resolver
clock clock.Clock
expiration time.Duration
capacity int
cache map[string]*linkedNode
lruList *linkedList
// Guards LRU state — cache and lruList
mux sync.Mutex
// Prevents multiple identical requests at the same time.
sg singleflight.Group
}
// NewLRUResolver creates new LRUResolver.
func NewLRUResolver(next Resolver, capacity int) *LRUResolver {
return &LRUResolver{
next: next,
clock: clock.System,
expiration: time.Minute,
capacity: capacity,
cache: make(map[string]*linkedNode, capacity),
lruList: &linkedList{},
sg: singleflight.Group{},
}
}
// WithClock sets clock to use when counting expiration.
func (l *LRUResolver) WithClock(c clock.Clock) *LRUResolver {
l.clock = c
return l
}
// WithExpiration sets expiration timeout for records in cache.
// If zero, expiration will be disabled. Default value is a minute.
func (l *LRUResolver) WithExpiration(expiration time.Duration) *LRUResolver {
l.expiration = expiration
return l
}
// Evict deletes record from cache.
func (l *LRUResolver) Evict(key string) (tg.InputPeerClass, bool) {
return l.delete(key)
}
// ResolveDomain implements Resolver.
func (l *LRUResolver) ResolveDomain(ctx context.Context, domain string) (tg.InputPeerClass, error) {
if v, ok := l.get(domain); ok {
return v, nil
}
r, err := l.next.ResolveDomain(ctx, domain)
if err != nil {
return nil, err
}
l.put(domain, r)
return r, nil
}
// ResolvePhone implements Resolver.
func (l *LRUResolver) ResolvePhone(ctx context.Context, phone string) (tg.InputPeerClass, error) {
if v, ok := l.get(phone); ok {
return v, nil
}
r, err := l.next.ResolvePhone(ctx, phone)
if err != nil {
return nil, err
}
l.put(phone, r)
return r, nil
}
func (l *LRUResolver) get(key string) (v tg.InputPeerClass, ok bool) {
l.mux.Lock()
defer l.mux.Unlock()
if found, ok := l.cache[key]; ok {
// Delete expired and return false.
if l.expiration > 0 && l.clock.Now().After(found.expiresAt) {
l.deleteLocked(key)
return nil, false
}
l.lruList.MoveToFront(found)
return found.value, true
}
return
}
func (l *LRUResolver) put(key string, value tg.InputPeerClass) {
l.mux.Lock()
defer l.mux.Unlock()
if found, ok := l.cache[key]; ok {
found.value = value
l.lruList.MoveToFront(found)
} else {
if len(l.cache) >= l.capacity {
l.deleteLocked(l.lruList.Back().key)
}
l.cache[key] = l.lruList.PushFront(nodeData{
key,
value,
l.clock.Now().Add(l.expiration),
})
}
}
func (l *LRUResolver) delete(key string) (tg.InputPeerClass, bool) {
l.mux.Lock()
defer l.mux.Unlock()
return l.deleteLocked(key)
}
// deleteLocked deletes record from cache.
// Assumes mutex is locked.
func (l *LRUResolver) deleteLocked(key string) (tg.InputPeerClass, bool) {
found, ok := l.cache[key]
if !ok {
return nil, false
}
l.lruList.Remove(found)
delete(l.cache, key)
return nil, true
}