]> git.lizzy.rs Git - go-anidb.git/blob - episodecache.go
0ed142e262b4f2062bb0a78132f0a82a3d92d5c3
[go-anidb.git] / episodecache.go
1 package anidb
2
3 import (
4         "encoding/gob"
5         "strconv"
6         "strings"
7         "time"
8 )
9
10 func init() {
11         gob.RegisterName("*github.com/Kovensky/go-anidb.Episode", &Episode{})
12 }
13
14 func (e *Episode) Touch() {
15         e.Cached = time.Now()
16 }
17
18 func (e *Episode) IsStale() bool {
19         if e == nil {
20                 return true
21         }
22         return time.Now().Sub(e.Cached) > EpisodeCacheDuration
23 }
24
25 // Unique Episode IDentifier.
26 type EID int
27
28 // Retrieves the Episode corresponding to this EID from the cache.
29 func (eid EID) Episode() *Episode {
30         var e Episode
31         if cache.Get(&e, "eid", eid) == nil {
32                 return &e
33         }
34         return nil
35 }
36
37 func cacheEpisode(ep *Episode) {
38         cache.Set(ep.AID, "aid", "by-eid", ep.EID)
39         cache.Set(ep, "eid", ep.EID)
40 }
41
42 // Retrieves an Episode by its EID.
43 func (adb *AniDB) EpisodeByID(eid EID) <-chan *Episode {
44         keys := []cacheKey{"eid", eid}
45         ch := make(chan *Episode, 1)
46
47         ic := make(chan Cacheable, 1)
48         go func() { ch <- (<-ic).(*Episode); close(ch) }()
49         if intentMap.Intent(ic, keys...) {
50                 return ch
51         }
52
53         if !cache.CheckValid(keys...) {
54                 intentMap.Notify((*Episode)(nil), keys...)
55                 return ch
56         }
57
58         e := eid.Episode()
59         if !e.IsStale() {
60                 intentMap.Notify(e, keys...)
61                 return ch
62         }
63
64         go func() {
65                 // The UDP API data is worse than the HTTP API anime data,
66                 // try and get from the corresponding Anime
67
68                 aid := AID(0)
69                 ok := cache.Get(&aid, "aid", "by-eid", eid) == nil
70
71                 udpDone := false
72
73                 for i := 0; i < 2; i++ {
74                         if !ok && udpDone {
75                                 // couldn't get anime and we already ran the EPISODE query
76                                 break
77                         }
78
79                         if !ok {
80                                 // We don't know what the AID is yet.
81                                 reply := <-adb.udp.SendRecv("EPISODE", paramMap{"eid": eid})
82
83                                 if reply.Error() == nil {
84                                         parts := strings.Split(reply.Lines()[1], "|")
85
86                                         if id, err := strconv.ParseInt(parts[1], 10, 32); err == nil {
87                                                 ok = true
88                                                 aid = AID(id)
89                                         }
90                                 } else if reply.Code() == 340 {
91                                         cache.MarkInvalid(keys...)
92                                 } else {
93                                         break
94                                 }
95                                 udpDone = true
96                         }
97                         a := <-adb.AnimeByID(AID(aid)) // this caches episodes...
98                         ep := eid.Episode()            // ...so this is now a cache hit
99
100                         if !ep.IsStale() {
101                                 e = ep
102                                 break
103                         } else {
104                                 // if this is somehow still a miss, then the EID<->AID map broke
105                                 cache.Delete("aid", "by-eid", eid)
106                                 ok = false
107                         }
108                 }
109                 intentMap.Notify(e, keys...)
110         }()
111         return ch
112 }