Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active September 19, 2016 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dcomartin/d2b01eba39ca1f7cbe5d81422a5bcad4 to your computer and use it in GitHub Desktop.
Save dcomartin/d2b01eba39ca1f7cbe5d81422a5bcad4 to your computer and use it in GitHub Desktop.
private static CurrencyExchange GetCurrencyRate(DateTime date)
{
var key = date.Date.ToString("yyyy-MM-dd");
var cachedExchange = _cache.GetAsync<CurrencyExchange>(key).Result;
if (cachedExchange.HasValue)
{
Console.WriteLine("Found in cache");
return cachedExchange.Value;
}
Console.WriteLine("Fetching from service");
var response = _httpClient.GetAsync("http://api.fixer.io/"+key+"?base=USD").Result;
var json = response.Content.ReadAsStringAsync().Result;
var exchange = JsonConvert.DeserializeObject<CurrencyExchange>(json);
_cache.AddAsync(key, exchange).Wait();
return exchange;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment