-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
61 lines (44 loc) · 1.09 KB
/
cache.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
package gpic
import (
"sync"
)
var cache inputCache = inputCache{items: make(map[string]int64)}
// A cache for gPic inputs. Stores them as a map and uses a RW mutex.
type inputCache struct {
mu sync.RWMutex
items map[string]int64
}
func (this *inputCache) read(input string) (int64, bool) {
this.mu.RLock()
id, found := this.items[input]
this.mu.RUnlock()
if !found {
return 0, false
}
return id, true
}
func (this *inputCache) write(input string, id int64) {
this.mu.Lock()
this.items[input] = id
this.mu.Unlock()
}
var strCache stringInputCache = stringInputCache{items: make(map[string]string)}
// A cache for gPic inputs where the value is a string. Stores them as a map and uses a RW mutex.
type stringInputCache struct {
mu sync.RWMutex
items map[string]string
}
func (this *stringInputCache) read(input string) (string, bool) {
this.mu.RLock()
id, found := this.items[input]
this.mu.RUnlock()
if !found {
return "", false
}
return id, true
}
func (this *stringInputCache) write(input string, id string) {
this.mu.Lock()
this.items[input] = id
this.mu.Unlock()
}