]> git.lizzy.rs Git - go-anidb.git/blob - groupcache.go
anidb: Return the previously available data if API refresh failed
[go-anidb.git] / groupcache.go
1 package anidb
2
3 import (
4         "encoding/gob"
5         "github.com/Kovensky/go-anidb/http"
6         "github.com/Kovensky/go-anidb/udp"
7         "strconv"
8         "strings"
9         "time"
10 )
11
12 func init() {
13         gob.RegisterName("*github.com/Kovensky/go-anidb.Group", &Group{})
14         gob.RegisterName("github.com/Kovensky/go-anidb.GID", GID(0))
15         gob.RegisterName("*github.com/Kovensky/go-anidb.gidCache", &gidCache{})
16 }
17
18 func (g *Group) Touch() {
19         g.Cached = time.Now()
20 }
21
22 func (g *Group) IsStale() bool {
23         if g == nil {
24                 return true
25         }
26         return time.Now().Sub(g.Cached) > GroupCacheDuration
27 }
28
29 // Unique Group IDentifier
30 type GID int
31
32 // make GID cacheable
33
34 func (e GID) Touch()        {}
35 func (e GID) IsStale() bool { return false }
36
37 // Retrieves the Group from the cache.
38 func (gid GID) Group() *Group {
39         var g Group
40         if cache.Get(&g, "gid", gid) == nil {
41                 return &g
42         }
43         return nil
44 }
45
46 type gidCache struct {
47         GID
48         Time time.Time
49 }
50
51 func (c *gidCache) Touch() { c.Time = time.Now() }
52 func (c *gidCache) IsStale() bool {
53         if c != nil && time.Now().Sub(c.Time) < GroupCacheDuration {
54                 return false
55         }
56         return true
57 }
58
59 // Retrieves a Group by its GID.
60 func (adb *AniDB) GroupByID(gid GID) <-chan *Group {
61         keys := []cacheKey{"gid", gid}
62         ch := make(chan *Group, 1)
63
64         ic := make(chan Cacheable, 1)
65         go func() { ch <- (<-ic).(*Group); close(ch) }()
66         if intentMap.Intent(ic, keys...) {
67                 return ch
68         }
69
70         if !cache.CheckValid(keys...) {
71                 intentMap.Notify((*Group)(nil), keys...)
72                 return ch
73         }
74
75         g := gid.Group()
76         if !g.IsStale() {
77                 intentMap.Notify(g, keys...)
78                 return ch
79         }
80
81         go func() {
82                 reply := <-adb.udp.SendRecv("GROUP",
83                         paramMap{"gid": gid})
84
85                 if reply.Error() == nil {
86                         g = parseGroupReply(reply)
87
88                         cache.Set(&gidCache{GID: g.GID}, "gid", "by-name", g.Name)
89                         cache.Set(&gidCache{GID: g.GID}, "gid", "by-shortname", g.ShortName)
90                         cache.Set(g, keys...)
91                 } else if reply.Code() == 350 {
92                         cache.MarkInvalid(keys...)
93                 }
94
95                 intentMap.Notify(g, keys...)
96         }()
97         return ch
98 }
99
100 // Retrieves a Group by its name. Either full or short names are matched.
101 func (adb *AniDB) GroupByName(gname string) <-chan *Group {
102         keys := []cacheKey{"gid", "by-name", gname}
103         altKeys := []cacheKey{"gid", "by-shortname", gname}
104         ch := make(chan *Group, 1)
105
106         ic := make(chan Cacheable, 1)
107         go func() {
108                 gid := (<-ic).(GID)
109                 if gid > 0 {
110                         ch <- <-adb.GroupByID(gid)
111                 }
112                 close(ch)
113         }()
114         if intentMap.Intent(ic, keys...) {
115                 return ch
116         }
117
118         if !cache.CheckValid(keys...) {
119                 intentMap.Notify(GID(0), keys...)
120                 return ch
121         }
122
123         gid := GID(0)
124
125         var gc gidCache
126         if cache.Get(&gc, keys...) == nil; !gc.IsStale() {
127                 intentMap.Notify(gc.GID, keys...)
128                 return ch
129         }
130         gid = gc.GID
131
132         if gid == 0 {
133                 if cache.Get(&gc, altKeys...) == nil; !gc.IsStale() {
134                         intentMap.Notify(gc.GID, keys...)
135                         return ch
136                 }
137                 gid = gc.GID
138         }
139
140         go func() {
141                 reply := <-adb.udp.SendRecv("GROUP",
142                         paramMap{"gname": gname})
143
144                 var g *Group
145                 if reply.Error() == nil {
146                         g = parseGroupReply(reply)
147
148                         gid = g.GID
149
150                         cache.Set(&gidCache{GID: gid}, keys...)
151                         cache.Set(&gidCache{GID: gid}, altKeys...)
152                         cache.Set(g, "gid", gid)
153                 } else if reply.Code() == 350 {
154                         cache.MarkInvalid(keys...)
155                 }
156
157                 intentMap.Notify(gid, keys...)
158         }()
159         return ch
160 }
161
162 func parseGroupReply(reply udpapi.APIReply) *Group {
163         parts := strings.Split(reply.Lines()[1], "|")
164         ints := make([]int64, len(parts))
165         for i := range parts {
166                 ints[i], _ = strconv.ParseInt(parts[i], 10, 32)
167         }
168
169         irc := ""
170         if parts[7] != "" {
171                 irc = "irc://" + parts[8] + "/" + parts[7][1:]
172         }
173
174         pic := ""
175         if parts[10] != "" {
176                 pic = httpapi.AniDBImageBaseURL + parts[10]
177         }
178
179         rellist := strings.Split(parts[16], "'")
180         relations := make(map[GID]GroupRelationType, len(rellist))
181         for _, rel := range rellist {
182                 r := strings.Split(rel, ",")
183                 if len(r) < 2 {
184                         continue
185                 }
186                 gid, _ := strconv.ParseInt(r[0], 10, 32)
187                 typ, _ := strconv.ParseInt(r[1], 10, 32)
188
189                 relations[GID(gid)] = GroupRelationType(typ)
190         }
191
192         ft := time.Unix(ints[11], 0)
193         if ints[11] == 0 {
194                 ft = time.Time{}
195         }
196         dt := time.Unix(ints[12], 0)
197         if ints[12] == 0 {
198                 dt = time.Time{}
199         }
200         lr := time.Unix(ints[14], 0)
201         if ints[14] == 0 {
202                 lr = time.Time{}
203         }
204         la := time.Unix(ints[15], 0)
205         if ints[15] == 0 {
206                 la = time.Time{}
207         }
208
209         return &Group{
210                 GID: GID(ints[0]),
211
212                 Name:      parts[5],
213                 ShortName: parts[6],
214
215                 IRC:     irc,
216                 URL:     parts[9],
217                 Picture: pic,
218
219                 Founded:   ft,
220                 Disbanded: dt,
221                 // ignore ints[13]
222                 LastRelease:  lr,
223                 LastActivity: la,
224
225                 Rating: Rating{
226                         Rating:    float32(ints[1]) / 100,
227                         VoteCount: int(ints[2]),
228                 },
229                 AnimeCount: int(ints[3]),
230                 FileCount:  int(ints[4]),
231
232                 RelatedGroups: relations,
233
234                 Cached: time.Now(),
235         }
236 }