예제 #1
0
        public async Task <Dictionary <string, string> > GetStationReferencesAndNamesForRiver(string riverName)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(riverName))
                {
                    throw new ArgumentNullException("riverName");
                }

                var refNamePairs           = new Dictionary <string, string>();
                var riverStationsFromCache = (RiverStations)_memoryCache.Get(riverName);

                if (riverStationsFromCache != null)
                {
                    if (riverStationsFromCache.Stations != null && riverStationsFromCache.Stations.Any())
                    {
                        riverStationsFromCache.Stations.ForEach(s => refNamePairs.Add(s.StationReference, s.Label));
                    }
                }
                else
                {
                    var riverNameTokens = riverName.Split(' ');
                    var newRiverName    = (riverNameTokens.Length > 1) ? string.Join("+", riverNameTokens) : riverName;
                    var requestUrl      = string.Format(_config.GetApiRiverStationsUrl(), newRiverName);

                    var responseData = await _externalService.GetAsync(requestUrl);

                    var riverStations = JsonConvert.DeserializeObject <RiverStations>(responseData);

                    if (riverStations != null)
                    {
                        if (riverStations.Stations != null && riverStations.Stations.Any())
                        {
                            // Add data to cache memory
                            _memoryCache.AddOrGetExisting(riverName, riverStations);

                            riverStations.Stations.ForEach(s => refNamePairs.Add(s.StationReference, s.Label));
                        }
                    }
                }

                return(refNamePairs);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex, _config.GetDefaultLoggingPath());
                return(null);
            }
        }
        private static void Cache <TReturnType>(LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ICacheWrapper cacheResolver,
                                                string key, Action work, Func <TReturnType, TReturnType> foundInCache)
        {
            TReturnType cachedData = cacheResolver.Get <TReturnType>(key);

            if (cachedData == null)
            {
                GetListFromSource <TReturnType>(aspect, cacheResolver, key);
            }
            else
            {
                // Give caller a chance to shape the cached item before it is returned
                TReturnType cachedType = foundInCache(cachedData);
                if (cachedType == null)
                {
                    GetListFromSource <TReturnType>(aspect, cacheResolver, key);
                }
                else
                {
                    aspect.WorkDelegate = new Func <TReturnType>(() => cachedType);
                }
            }

            work();
        }
        public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF CacheList <TItemType, TListType>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect,
                                                                                                         ICacheWrapper cacheResolver, string listCacheKey, Func <TItemType, string> getItemKey)
            where TListType : IList <TItemType>, new()
        {
            return(aspect.Combine(work =>
            {
                var workDelegate = aspect.WorkDelegate as Func <TListType>;

                // Replace the actual work delegate with a new delegate so that
                // when the actual work delegate returns a collection, each item
                // in the collection is stored in cache individually.
                Func <TListType> newWorkDelegate = () =>
                {
                    if (workDelegate != null)
                    {
                        TListType collection = workDelegate();
                        foreach (TItemType item in collection)
                        {
                            string key = getItemKey(item);
                            cacheResolver.Save(key, item);
                        }
                        return collection;
                    }
                    return default(TListType);
                };
                aspect.WorkDelegate = newWorkDelegate;

                // Get the collection from cache or real source. If collection is returned
                // from cache, resolve each item in the collection from cache
                Cache <TListType>(aspect, cacheResolver, listCacheKey, work,
                                  cached =>
                {
                    // Get each item from cache. If any of the item is not in cache
                    // then discard the whole collection from cache and reload the
                    // collection from source.
                    var itemList = new TListType();
                    foreach (TItemType cachedItem in cached.Select(item => cacheResolver.Get <TItemType>(getItemKey(item))))
                    {
                        if (null != cachedItem)
                        {
                            itemList.Add(cachedItem);
                        }
                        else
                        {
                            // One of the item is missing from cache. So, discard the
                            // cached list.
                            return default(TListType);
                        }
                    }

                    return itemList;
                });
            }));
        }
예제 #4
0
        protected virtual T GetFromCache <T>(string cacheKey)
        {
            var objectFromSession = cache.Get(cacheKey);

            return(objectFromSession == null ? default(T) : (T)objectFromSession);
        }
예제 #5
0
        /// <summary>
        /// Retrieve or construct the sighting details model
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task <SightingDetailsViewModel> GetSightingDetailsModelAsync(string userName, int?sightingId)
        {
            // Retrieve the model from the cache
            string key = GetCacheKey(SightingDetailsKeyPrefix, userName);
            SightingDetailsViewModel model = _cache.Get <SightingDetailsViewModel>(key);

            if ((model == null) || (model.SightingId != sightingId))
            {
                // Not cached or the ID has changed, so create a new one and set the "last sighting added" message
                string lastAdded = GetLastSightingAddedMessage(userName);
                ClearCachedLastSightingAddedMessage(userName);

                // If an existing sighting is specified, construct the model using its
                // details
                if (sightingId != null)
                {
                    Sighting sighting = await _sightings.GetSightingAsync(sightingId ?? 0);

                    model = new SightingDetailsViewModel
                    {
                        SightingId = sightingId,
                        LastSightingAddedMessage = lastAdded,
                        Altitude     = sighting.Altitude,
                        Date         = sighting.Date,
                        FlightNumber = sighting.Flight.Number,
                        LocationId   = sighting.LocationId,
                        Registration = sighting.Aircraft.Registration
                    };
                }
                else
                {
                    model = new SightingDetailsViewModel
                    {
                        LastSightingAddedMessage = lastAdded,
                        Date       = GetDefaultDate(userName),
                        LocationId = GetDefaultLocationId(userName)
                    };
                }
            }

            // Set the available locations
            List <Location> locations = await GetLocationsAsync();

            model.SetLocations(locations);

            return(model);
        }