-
Notifications
You must be signed in to change notification settings - Fork 4
/
usercache.go
205 lines (167 loc) · 3.77 KB
/
usercache.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package anidb
import (
"github.com/Kovensky/go-anidb/udp"
"github.com/Kovensky/go-fscache"
"strconv"
"strings"
"sync"
"time"
)
type UID int
func (uid UID) User() *User {
var u User
if CacheGet(&u, "user", uid) == nil {
return &u
}
return nil
}
func UserByName(name string) *User {
var uid UID
if CacheGet(&uid, "user", "by-name", name) == nil {
return uid.User()
}
return nil
}
func (adb *AniDB) GetCurrentUser() <-chan *User {
ch := make(chan *User, 1)
if adb.udp.credentials == nil {
ch <- nil
close(ch)
return ch
}
return adb.GetUserByName(decrypt(adb.udp.credentials.username))
}
// This is an (almost) entirely local representation.
func (adb *AniDB) GetUserByID(uid UID) <-chan *User {
key := []fscache.CacheKey{"user", uid}
ch := make(chan *User, 1)
if uid < 1 {
ch <- nil
close(ch)
return ch
}
ic := make(chan notification, 1)
go func() { ch <- (<-ic).(*User); close(ch) }()
if intentMap.Intent(ic, key...) {
return ch
}
if !Cache.IsValid(InvalidKeyCacheDuration, key...) {
intentMap.NotifyClose((*User)(nil), key...)
return ch
}
go func() {
var user *User
if CacheGet(&user, key...) == nil {
intentMap.NotifyClose(user, key...)
return
}
<-adb.GetUserName(uid)
CacheGet(&user, key...)
intentMap.NotifyClose(user)
}()
return ch
}
func (adb *AniDB) GetUserByName(username string) <-chan *User {
ch := make(chan *User, 1)
if username == "" {
ch <- nil
close(ch)
return ch
}
go func() {
ch <- <-adb.GetUserByID(<-adb.GetUserUID(username))
close(ch)
}()
return ch
}
func (adb *AniDB) GetUserUID(username string) <-chan UID {
key := []fscache.CacheKey{"user", "by-name", username}
ch := make(chan UID, 1)
if username == "" {
ch <- 0
close(ch)
return ch
}
ic := make(chan notification, 1)
go func() { ch <- (<-ic).(UID); close(ch) }()
if intentMap.Intent(ic, key...) {
return ch
}
if !Cache.IsValid(InvalidKeyCacheDuration, key...) {
intentMap.NotifyClose((UID)(0), key...)
return ch
}
uid := UID(0)
switch ts, err := Cache.Get(&uid, key...); {
case err == nil && time.Now().Sub(ts) < UIDCacheDuration:
intentMap.NotifyClose(uid, key...)
return ch
}
go func() {
reply := <-adb.udp.SendRecv("USER",
paramMap{"user": username})
switch reply.Code() {
case 295:
uid, _ = parseUserReply(reply) // caches
case 394:
Cache.SetInvalid(key...)
}
intentMap.NotifyClose(uid, key...)
}()
return ch
}
func (adb *AniDB) GetUserName(uid UID) <-chan string {
key := []fscache.CacheKey{"user", "by-uid", uid}
ch := make(chan string, 1)
if uid < 1 {
ch <- ""
close(ch)
return ch
}
ic := make(chan notification, 1)
go func() { ch <- (<-ic).(string); close(ch) }()
if intentMap.Intent(ic, key...) {
return ch
}
if !Cache.IsValid(InvalidKeyCacheDuration, key...) {
intentMap.NotifyClose("", key...)
return ch
}
name := ""
switch ts, err := Cache.Get(&name, key...); {
case err == nil && time.Now().Sub(ts) < UIDCacheDuration:
intentMap.NotifyClose(name, key...)
return ch
}
go func() {
reply := <-adb.udp.SendRecv("USER",
paramMap{"uid": uid})
switch reply.Code() {
case 295:
_, name = parseUserReply(reply) // caches
case 394:
Cache.SetInvalid(key...)
}
intentMap.NotifyClose(name, key...)
}()
return ch
}
var userReplyMutex sync.Mutex
func parseUserReply(reply udpapi.APIReply) (UID, string) {
userReplyMutex.Lock()
defer userReplyMutex.Unlock()
if reply.Error() == nil {
parts := strings.Split(reply.Lines()[1], "|")
id, _ := strconv.ParseInt(parts[0], 10, 32)
CacheSet(UID(id), "user", "by-name", parts[1])
CacheSet(parts[1], "user", "by-uid", id)
if _, err := Cache.Stat("user", id); err != nil {
CacheSet(&User{
UID: UID(id),
Username: parts[1],
}, "user", id)
}
return UID(id), parts[1]
}
return 0, ""
}