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.
43 func (adb *AniDB) EpisodeByID(eid EID) <-chan *Episode {
44 keys := []cacheKey{"eid", eid}
45 ch := make(chan *Episode, 1)
47 ic := make(chan Cacheable, 1)
48 go func() { ch <- (<-ic).(*Episode); close(ch) }()
49 if intentMap.Intent(ic, keys...) {
53 if !cache.CheckValid(keys...) {
54 intentMap.Notify((*Episode)(nil), keys...)
60 intentMap.Notify(e, keys...)
65 // The UDP API data is worse than the HTTP API anime data,
66 // try and get from the corresponding Anime
69 ok := cache.Get(&aid, "aid", "by-eid", eid) == nil
73 for i := 0; i < 2; i++ {
75 // couldn't get anime and we already ran the EPISODE query
80 // We don't know what the AID is yet.
81 reply := <-adb.udp.SendRecv("EPISODE", paramMap{"eid": eid})
83 if reply.Error() == nil {
84 parts := strings.Split(reply.Lines()[1], "|")
86 if id, err := strconv.ParseInt(parts[1], 10, 32); err == nil {
90 } else if reply.Code() == 340 {
91 cache.MarkInvalid(keys...)
97 a := <-adb.AnimeByID(AID(aid)) // this caches episodes...
98 ep := eid.Episode() // ...so this is now a cache hit
104 // if this is somehow still a miss, then the EID<->AID map broke
105 cache.Delete("aid", "by-eid", eid)
109 intentMap.Notify(e, keys...)