Compare commits

...

2 Commits

Author SHA1 Message Date
Sergey Chebotar
46e547e3e2 Cache found product sku on full text search request. 2023-05-16 15:28:36 +03:00
Sergey Chebotar
2178c1bea3 Find last row by Sku column 2023-05-16 09:03:51 +03:00
2 changed files with 47 additions and 21 deletions

View File

@ -21,36 +21,62 @@ public class DatabaseClient : IDatabaseClient
public async Task<IEnumerable<Product>> GetProducts(string line)
{
string request;
IEnumerable<Product> products;
if (ProductSku.TryParse(line, out var skus))
{
request = @"https://rh.cebotari.ru/api/products/" + skus.FirstOrDefault().ToString();
ProductSku sku = skus.FirstOrDefault();
request = @"https://rh.cebotari.ru/api/products/" + sku.ToString();
if (!_memoryCache.TryGetValue(sku, out products))
{
var response = await _httpClient.GetAsync(request);
try
{
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
products = JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>();
}
catch
{
StatusCode = response.StatusCode;
}
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(1));
_memoryCache.Set(sku, products, cacheEntryOptions);
}
}
else
{
request = @"https://rh.cebotari.ru/api/search?query=" + line;
}
if (!_memoryCache.TryGetValue(line, out IEnumerable<Product> products))
{
var response = await _httpClient.GetAsync(request);
try
if (!_memoryCache.TryGetValue(line, out products))
{
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
products = JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>();
}
catch
{
StatusCode = response.StatusCode;
}
var response = await _httpClient.GetAsync(request);
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(1));
_memoryCache.Set(line, products, cacheEntryOptions);
}
try
{
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
products = JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>();
}
catch
{
StatusCode = response.StatusCode;
}
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(1));
_memoryCache.Set(line, products, cacheEntryOptions);
if (products.Count() > 0)
{
_memoryCache.Set(products.First(), products, cacheEntryOptions);
}
}
}
return products;
}

View File

@ -95,7 +95,7 @@ public class ExcelReader : IReader, IDisposable
ProductLineCell = worksheet.Cells.Find(headers["ProductLine"]),
NameCell = worksheet.Cells.Find(headers["Name"]),
MeasureCell = worksheet.Cells.Find(headers["Measure"]);
var lastRowIndex = worksheet.Cells[worksheet.Rows.Count, AmountCell.Column]
var lastRowIndex = worksheet.Cells[worksheet.Rows.Count, SkuCell.Column]
.End[XlDirection.xlUp].Row;
Dictionary<Product, double> readResult = new();