Add caching

This commit is contained in:
Sergey Chebotar 2023-05-16 07:43:57 +03:00
parent 05914bf519
commit 627195fafe
6 changed files with 63 additions and 40 deletions

View File

@ -19,6 +19,7 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
IServiceCollection Services = new ServiceCollection(); IServiceCollection Services = new ServiceCollection();
Services.AddHttpClient() Services.AddHttpClient()
.AddMemoryCache()
.AddSingleton((Application)ExcelDnaUtil.Application) .AddSingleton((Application)ExcelDnaUtil.Application)
.AddSingleton<IAddInConfiguration, AddInConfiguration>() .AddSingleton<IAddInConfiguration, AddInConfiguration>()
.AddSingleton<IDatabaseClient, DatabaseClient>() .AddSingleton<IDatabaseClient, DatabaseClient>()

View File

@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.6.3.0")] [assembly: AssemblyVersion("1.6.4.0")]
[assembly: AssemblyFileVersion("1.6.3.0")] [assembly: AssemblyFileVersion("1.6.4.0")]

View File

@ -3,6 +3,8 @@
<ExternalLibrary Path="RhSolutions.AddIn.dll" ExplicitExports="false" LoadFromBytes="true" Pack="true" IncludePdb="false" /> <ExternalLibrary Path="RhSolutions.AddIn.dll" ExplicitExports="false" LoadFromBytes="true" Pack="true" IncludePdb="false" />
<Reference Path="Microsoft.Bcl.AsyncInterfaces.dll" Pack="true" /> <Reference Path="Microsoft.Bcl.AsyncInterfaces.dll" Pack="true" />
<Reference Path="Microsoft.Bcl.HashCode.dll" Pack="true" /> <Reference Path="Microsoft.Bcl.HashCode.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.Caching.Abstractions.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.Caching.Memory.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.DependencyInjection.Abstractions.dll" Pack="true" /> <Reference Path="Microsoft.Extensions.DependencyInjection.Abstractions.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.DependencyInjection.dll" Pack="true" /> <Reference Path="Microsoft.Extensions.DependencyInjection.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.Http.dll" Pack="true" /> <Reference Path="Microsoft.Extensions.Http.dll" Pack="true" />

View File

@ -33,6 +33,7 @@
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" /> <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" /> <PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="netDxf" Version="2022.11.2" /> <PackageReference Include="netDxf" Version="2022.11.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

View File

@ -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,43 @@ 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=";
public HttpStatusCode StatusCode { get; private set; } public HttpStatusCode StatusCode { get; private set; }
public CurrencyClient(IServiceProvider serviceProvider) public CurrencyClient(HttpClient httpClient, IMemoryCache memoryCache)
{ {
_httpClient = serviceProvider.GetRequiredService<HttpClient>(); _httpClient = httpClient;
_memoryCache = memoryCache;
} }
public async Task<decimal?> GetCurrencyCourse(DateTime date) public async Task<decimal?> GetCurrencyCourse(DateTime date)
{ {
string request = $"https://www.cbr.ru/scripts/XML_daily.asp?date_req={date.Date.ToString("dd/MM/yyyy")}"; if (!_memoryCache.TryGetValue(date, out decimal? exchangeRate))
var response = await _httpClient.GetAsync(request); {
string request = $"{requestAddress}{date.Date:dd/MM/yyyy}";
HttpResponseMessage response = await _httpClient.GetAsync(request);
try try
{ {
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
string xml = await response.Content.ReadAsStringAsync(); string xml = await response.Content.ReadAsStringAsync();
XElement valCourses = XElement.Parse(xml); XElement valCourses = XElement.Parse(xml);
var course = decimal.Parse(valCourses.Elements("Valute") exchangeRate = decimal.Parse(valCourses.Elements("Valute")
.Where(e => e.Element("Name").Value == "Евро") .Where(e => e.Element("Name").Value == "Евро")
.FirstOrDefault() .FirstOrDefault()
.Element("Value").Value); .Element("Value").Value);
return course; return exchangeRate;
} }
catch catch
{ {
StatusCode = response.StatusCode; StatusCode = response.StatusCode;
exchangeRate = null;
} }
return null; var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(1));
_memoryCache.Set(date, exchangeRate, cacheEntryOptions);
}
return exchangeRate;
} }
} }

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json; using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -7,12 +8,14 @@ namespace RhSolutions.Services;
public class DatabaseClient : IDatabaseClient public class DatabaseClient : IDatabaseClient
{ {
private readonly IServiceProvider serviceProvider; private readonly HttpClient _httpClient;
private readonly IMemoryCache _memoryCache;
public HttpStatusCode StatusCode { get; private set; } public HttpStatusCode StatusCode { get; private set; }
public DatabaseClient(IServiceProvider provider) public DatabaseClient(HttpClient httpClient, IMemoryCache memoryCache)
{ {
this.serviceProvider = provider; _httpClient = httpClient;
_memoryCache = memoryCache;
} }
public async Task<IEnumerable<Product>> GetProducts(string line) public async Task<IEnumerable<Product>> GetProducts(string line)
@ -29,20 +32,26 @@ public class DatabaseClient : IDatabaseClient
request = @"https://rh.cebotari.ru/api/search?query=" + line; request = @"https://rh.cebotari.ru/api/search?query=" + line;
} }
var client = serviceProvider.GetRequiredService<HttpClient>(); if (!_memoryCache.TryGetValue(line, out IEnumerable<Product> products))
var response = await client.GetAsync(request); {
var response = await _httpClient.GetAsync(request);
try try
{ {
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync(); string json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>(); products = JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>();
} }
catch catch
{ {
StatusCode = response.StatusCode; StatusCode = response.StatusCode;
} }
return Enumerable.Empty<Product>(); var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(1));
_memoryCache.Set(line, products, cacheEntryOptions);
}
return products;
} }
} }