aboutsummaryrefslogtreecommitdiff
path: root/Functions.cs
diff options
context:
space:
mode:
authorSergey Chebotar <s.chebotar@gmail.com>2021-11-29 11:26:25 +0300
committerSergey Chebotar <s.chebotar@gmail.com>2021-11-29 11:26:25 +0300
commit0fe8e038af7f33eae824bba263e7c54dea829679 (patch)
treeb71636e6ee0c558f491bccd93c0236dbccbc67d6 /Functions.cs
parentf5234e956c79d3019e975a4d3550574c92f769e7 (diff)
Добавлен кэшинг результатов запроса, интерфейс IProduct
Diffstat (limited to 'Functions.cs')
-rw-r--r--Functions.cs46
1 files changed, 36 insertions, 10 deletions
diff --git a/Functions.cs b/Functions.cs
index aefdd87..9d3eb28 100644
--- a/Functions.cs
+++ b/Functions.cs
@@ -1,27 +1,53 @@
using ExcelDna.Integration;
+using System.Runtime.Caching;
using System.Net.Http;
namespace Rehau.Sku.Assist
{
public class Functions : IExcelAddIn
{
- static readonly HttpClient httpClient = new HttpClient();
+ private static HttpClient _httpClient;
+ private static ObjectCache _resultCache = MemoryCache.Default;
- public static object RAUNAME(string request)
+ public void AutoClose()
{
- return ExcelAsyncUtil.Run("RAUNAME", request, delegate
- {
- var document = SkuAssist.GetDocumentAsync(request, httpClient).Result;
- return SkuAssist.GetResultFromDocument(document);
- });
}
-
- public void AutoClose()
+ public void AutoOpen()
{
+ _httpClient = new HttpClient();
}
- public void AutoOpen()
+ [ExcelFunction]
+ public static object RAUNAME(string request)
{
+ string cachedResult = _resultCache[request] as string;
+
+ if (cachedResult != null)
+ {
+ return cachedResult;
+ }
+
+ else
+ {
+ object result = ExcelAsyncUtil.Run("RAUNAME", null,
+ delegate
+ {
+ var document = SkuAssist.GetDocumentAsync(request, _httpClient).Result;
+ var product = SkuAssist.GetProductFromDocument(document);
+ return product.ToString();
+ });
+
+ if (result.Equals(ExcelError.ExcelErrorNA))
+ {
+ return "Загрузка...";
+ }
+
+ else
+ {
+ _resultCache.Add(request, result, System.DateTime.Now.AddMinutes(20));
+ return result.ToString();
+ }
+ }
}
}
} \ No newline at end of file