Add memory cache to currency client
This commit is contained in:
parent
822398e286
commit
fe58c854ea
@ -1,4 +1,5 @@
|
|||||||
using System.Net;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
@ -8,34 +9,42 @@ namespace RhSolutions.Services;
|
|||||||
public class CurrencyClient : ICurrencyClient
|
public class CurrencyClient : ICurrencyClient
|
||||||
{
|
{
|
||||||
private readonly HttpClient _httpClient;
|
private readonly HttpClient _httpClient;
|
||||||
|
private readonly IMemoryCache _memoryCache;
|
||||||
private const string _requestAddress = @"https://www.cbr.ru/scripts/XML_daily.asp?date_req=";
|
private const string _requestAddress = @"https://www.cbr.ru/scripts/XML_daily.asp?date_req=";
|
||||||
public HttpStatusCode StatusCode { get; private set; }
|
public HttpStatusCode StatusCode { get; private set; }
|
||||||
|
|
||||||
public CurrencyClient(HttpClient httpClient)
|
public CurrencyClient(HttpClient httpClient, IMemoryCache memoryCache)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
_httpClient = httpClient;
|
||||||
|
_memoryCache = memoryCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<decimal?> GetExchangeRate(DateTime date)
|
public async Task<decimal?> GetExchangeRate(DateTime date)
|
||||||
{
|
{
|
||||||
string request = $"{_requestAddress}{date.Date:dd/MM/yyyy}";
|
if (!_memoryCache.TryGetValue(date, out decimal exchangeRate))
|
||||||
HttpResponseMessage response = await _httpClient.GetAsync(request);
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
response.EnsureSuccessStatusCode();
|
string request = $"{_requestAddress}{date.Date:dd/MM/yyyy}";
|
||||||
string xml = await response.Content.ReadAsStringAsync();
|
HttpResponseMessage response = await _httpClient.GetAsync(request);
|
||||||
XElement valCourses = XElement.Parse(xml);
|
try
|
||||||
|
{
|
||||||
decimal? exchangeRate = decimal.Parse(valCourses.Elements("Valute")
|
response.EnsureSuccessStatusCode();
|
||||||
.Where(e => e.Element("Name").Value == "Евро")
|
string xml = await response.Content.ReadAsStringAsync();
|
||||||
.FirstOrDefault()
|
XElement valCourses = XElement.Parse(xml);
|
||||||
.Element("Value").Value);
|
exchangeRate = decimal.Parse(valCourses.Elements("Valute")
|
||||||
return exchangeRate;
|
.Where(e => e.Element("Name").Value == "Евро")
|
||||||
}
|
.FirstOrDefault()
|
||||||
catch
|
.Element("Value").Value);
|
||||||
{
|
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||||
StatusCode = response.StatusCode;
|
.SetSlidingExpiration(TimeSpan.FromHours(1));
|
||||||
return null;
|
_memoryCache.Set(date, exchangeRate, cacheEntryOptions);
|
||||||
|
return exchangeRate;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
StatusCode = response.StatusCode;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return exchangeRate;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user