-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcache.go
More file actions
45 lines (37 loc) · 694 Bytes
/
cache.go
File metadata and controls
45 lines (37 loc) · 694 Bytes
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
package main
import (
"container/list"
"sync"
)
type FIFOCache struct {
l *list.List
m map[string]struct{}
length int
mu *sync.RWMutex
}
func (fc *FIFOCache) Init(length int) {
fc.l = list.New()
fc.m = make(map[string]struct{})
fc.length = length
fc.mu = &sync.RWMutex{}
}
func (fc *FIFOCache) Set(url string) {
fc.mu.Lock()
defer fc.mu.Unlock()
fc.l.PushFront(url)
fc.m[url] = struct{}{}
}
func (fc *FIFOCache) removeLast() {
fc.mu.Lock()
defer fc.mu.Unlock()
for fc.l.Len() >= fc.length {
item := fc.l.Back()
fc.l.Remove(item)
}
}
func (fc *FIFOCache) Check(url string) bool {
fc.mu.RLock()
defer fc.mu.RUnlock()
_, ok := fc.m[url]
return ok
}