// GetTrains searches the embedded cache for a result otherwise deletates the request to the underlying // facade. func (c *cachingfacade) GetTrains(key string, value string) (chan *models.TrainEventList, chan error) { logger.Printf("Intercepting request for GetTrains(%s, %s)", key, value) cacheKey := toCacheKey("GetTrains", key, value) success, failure := make(chan *models.TrainEventList), make(chan error) go func() { if cachedItem, found := c.localcache.Get(cacheKey); found { v := cachedItem.(*trainCacheEntry) if v.err != nil { failure <- v.err } else { success <- v.list } } else { privSuccess, privFailure := c.facade.GetTrains(key, value) select { case result := <-privSuccess: success <- c.cacheSuccess(cacheKey, result) case err := <-privFailure: failure <- c.cacheFalure(cacheKey, err) case <-time.After(time.Second * 30): failure <- services.NewServiceTimeoutError("Failed to get all trains in time") } } }() return success, failure }
func (s *trainService) TrainsByKeyValue(key string, value string) (result *models.TrainEventList, err error) { successChan, errChan := s.remoteAPI.GetTrains(key, value) select { case result := <-successChan: return s.joinStationName(result), nil case err = <-errChan: return nil, err case <-time.After(time.Second * 30): return nil, services.NewServiceTimeoutError("Failed to get all trains in time") } }
func (c *cachingfacade) GetStation(stationid string) (chan *models.StationList, chan error) { success, failure := make(chan *models.StationList), make(chan error) go func() { if stationByIDMap[stationid] == nil { downstreamSucces, downstreamFailure := c.facade.GetStation(stationid) select { case result := <-downstreamSucces: stationByIDMap[stationid] = result case err := <-downstreamFailure: failure <- err case <-time.After(time.Second * 30): failure <- services.NewServiceTimeoutError("Failed to get all trains in time") } } success <- stationByIDMap[stationid] }() return success, failure }
func (c *cachingfacade) GetStations() (chan *models.StationList, chan error) { //logger.Println("Intercepting request for GetStatins()") success, failure := make(chan *models.StationList), make(chan error) go func() { if cachedStationList == nil { logger.Println("Sending request downstream") downstreamSuccess, downstreamErr := c.facade.GetStations() select { case cachedStationList = <-downstreamSuccess: case err := <-downstreamErr: failure <- err case <-time.After(time.Second * 30): failure <- services.NewServiceTimeoutError("Failed to get all trains in time") } } success <- cachedStationList }() return success, failure }