Cache found product sku on full text search request.
This commit is contained in:
parent
2178c1bea3
commit
46e547e3e2
@ -21,36 +21,62 @@ public class DatabaseClient : IDatabaseClient
|
|||||||
public async Task<IEnumerable<Product>> GetProducts(string line)
|
public async Task<IEnumerable<Product>> GetProducts(string line)
|
||||||
{
|
{
|
||||||
string request;
|
string request;
|
||||||
|
IEnumerable<Product> products;
|
||||||
|
|
||||||
if (ProductSku.TryParse(line, out var skus))
|
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
|
else
|
||||||
{
|
{
|
||||||
request = @"https://rh.cebotari.ru/api/search?query=" + line;
|
request = @"https://rh.cebotari.ru/api/search?query=" + line;
|
||||||
}
|
|
||||||
|
if (!_memoryCache.TryGetValue(line, out products))
|
||||||
if (!_memoryCache.TryGetValue(line, out IEnumerable<Product> products))
|
|
||||||
{
|
|
||||||
var response = await _httpClient.GetAsync(request);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
response.EnsureSuccessStatusCode();
|
var response = await _httpClient.GetAsync(request);
|
||||||
string json = await response.Content.ReadAsStringAsync();
|
|
||||||
products = JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>();
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
StatusCode = response.StatusCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
try
|
||||||
.SetSlidingExpiration(TimeSpan.FromHours(1));
|
{
|
||||||
_memoryCache.Set(line, products, cacheEntryOptions);
|
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;
|
return products;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user