コード例 #1
0
        private void CacheGeoLocations(GeoServiceResponse data, CancellationToken cancellationToken)
        {
            List <object> cacheKeyList = new List <object>();

            cacheKeyList.Add("GeoLocationsAsync");

            string cacheKey = string.Join(":", cacheKeyList);

            CacheItemPolicy policy = new CacheItemPolicy();

            policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(Settings.GeoServiceCacheExpiration));
            cache.Add(cacheKey, data, policy);

            foreach (Continent continent in data.Continents)
            {
                cache.Add("continent:" + continent.Id, continent, policy);
                foreach (Country country in continent.Countries)
                {
                    cache.Add("country:" + country.Id, country, policy);
                    foreach (City city in country.Cities)
                    {
                        cache.Add("city:" + city.IataCode, city, policy);
                        foreach (Airport airport in city.Airports)
                        {
                            cache.Add("airport:" + airport.Id, airport, policy);
                        }
                    }
                }
            }
        }
コード例 #2
0
        public async Task <GeoServiceResponse> GetGeoLocationsAsync(CancellationToken cancellationToken)
        {
            List <object> cacheKeyList = new List <object>();

            cacheKeyList.Add("GeoLocationsAsync");

            string cacheKey = string.Join(":", cacheKeyList);

            GeoServiceResponse data = (GeoServiceResponse)cache.Get(cacheKey);

            if (data == null)
            {
                await Settings.GeoServiceLock.WaitAsync();

                try
                {
                    data = (GeoServiceResponse)cache.Get(cacheKey);
                    if (data == null)
                    {
                        string responseString = "";

                        if (_dataPath != "" && File.Exists(_dataPath + Settings.GeoServiceFileName))
                        {
                            var path = _dataPath + Settings.GeoServiceFileName;
                            responseString = File.ReadAllText(path);
                        }
                        else
                        {
                            var variables = new Dictionary <string, string>();
                            variables.Add("{apiKey}", ApiKey());

                            var URL      = Settings.GeoServiceURL.ReplaceFromDictionary(variables);
                            var response = await WebRequestHelper.GetAsync(URL, WebRequestHelper.ResponseAcceptType.JSON, cancellationToken);

                            responseString = response.Item1.BytesToString();

                            try
                            {
                                if (_dataPath != "")
                                {
                                    File.WriteAllText(_dataPath + Settings.GeoServiceFileName, responseString);
                                }
                            }
                            catch
                            {
                            }
                        }

                        data = JsonConvert.DeserializeObject <GeoServiceResponse>(responseString);

                        CacheGeoLocations(data, cancellationToken);
                    }
                }
                finally
                {
                    Settings.GeoServiceLock.Release();
                }
            }

            return(data);
        }