11 gob.RegisterName("*github.com/Kovensky/go-anidb.Episode", &Episode{})
14 func (e *Episode) Touch() {
18 func (e *Episode) IsStale() bool {
22 return time.Now().Sub(e.Cached) > EpisodeCacheDuration
25 // Unique Episode IDentifier.
28 // Retrieves the Episode corresponding to this EID from the cache.
29 func (eid EID) Episode() *Episode {
31 if cache.Get(&e, "eid", eid) == nil {
37 func cacheEpisode(ep *Episode) {
38 cache.Set(ep.AID, "aid", "by-eid", ep.EID)
39 cache.Set(ep, "eid", ep.EID)
42 // Retrieves an Episode by its EID.
44 // If we know which AID owns this EID, then it's equivalent
45 // to an Anime query. Otherwise, uses both the HTTP and UDP
46 // APIs to retrieve it.
47 func (adb *AniDB) EpisodeByID(eid EID) <-chan *Episode {
48 keys := []cacheKey{"eid", eid}
49 ch := make(chan *Episode, 1)
57 ic := make(chan Cacheable, 1)
58 go func() { ch <- (<-ic).(*Episode); close(ch) }()
59 if intentMap.Intent(ic, keys...) {
63 if !cache.CheckValid(keys...) {
64 intentMap.NotifyClose((*Episode)(nil), keys...)
70 intentMap.NotifyClose(e, keys...)
75 // The UDP API data is worse than the HTTP API anime data and
76 // might even get truncated on some pathological cases;
77 // try and get from the corresponding Anime, which uses the HTTP
81 ok := cache.Get(&aid, "aid", "by-eid", eid) == nil
85 for i := 0; i < 2; i++ {
87 // couldn't get anime and we already ran the EPISODE query
92 // We don't know what the AID is yet.
93 reply := <-adb.udp.SendRecv("EPISODE", paramMap{"eid": eid})
95 if reply.Error() == nil {
96 parts := strings.Split(reply.Lines()[1], "|")
98 if id, err := strconv.ParseInt(parts[1], 10, 32); err == nil {
104 } else if reply.Code() == 340 {
105 cache.MarkInvalid(keys...)
106 cache.Delete(keys...) // deleted EID?
113 a := <-adb.AnimeByID(AID(aid)) // updates the episode cache as well
114 ep := a.EpisodeByEID(eid)
120 // the EID<->AID map broke
122 cache.Delete("aid", "by-eid", eid)
125 intentMap.NotifyClose(e, keys...)