public async Task <LocalesServiceResponse> GetLocalesAsync(CancellationToken cancellationToken) { List <object> cacheKeyList = new List <object>(); cacheKeyList.Add("GetLocalesAsync"); string cacheKey = string.Join(":", cacheKeyList); LocalesServiceResponse data = (LocalesServiceResponse)cache.Get(cacheKey); if (data == null) { await Settings.LocalesServiceLock.WaitAsync(); try { data = (LocalesServiceResponse)cache.Get(cacheKey); if (data == null) { string responseString = ""; if (_dataPath != "" && File.Exists(_dataPath + Settings.LocalesServiceFileName)) { var path = _dataPath + Settings.LocalesServiceFileName; responseString = File.ReadAllText(path); } else { var variables = new Dictionary <string, string>(); variables.Add("{apiKey}", ApiKey()); var URL = Settings.LocalesServiceURL.ReplaceFromDictionary(variables); var response = await WebRequestHelper.GetAsync(URL, WebRequestHelper.ResponseAcceptType.JSON, cancellationToken); responseString = response.Item1.BytesToString(); try { if (_dataPath != "") { File.WriteAllText(_dataPath + Settings.LocalesServiceFileName, responseString); } } catch { } } data = JsonConvert.DeserializeObject <LocalesServiceResponse>(responseString); await cacheLocales(data, cancellationToken); } } finally { Settings.LocalesServiceLock.Release(); } } return(data); }
private async Task cacheLocales(LocalesServiceResponse data, CancellationToken cancellationToken) { List <object> cacheKeyList = new List <object>(); cacheKeyList.Add("GetLocalesAsync"); string cacheKey = string.Join(":", cacheKeyList); CacheItemPolicy policy = new CacheItemPolicy(); policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(Settings.LocalesServiceCacheExpiration)); cache.Add(cacheKey, data, policy); //cache currencies await GetCurrenciesAsync(cancellationToken); foreach (Locale locale in data.Locales) { //cache countries await GetCountriesByLocaleAsync(locale.Code, cancellationToken); cache.Add("locale:" + locale.Code, locale, policy); } }