diff --git a/Assistant/IProduct.cs b/Assistant/IProduct.cs
new file mode 100644
index 0000000..aca3ff5
--- /dev/null
+++ b/Assistant/IProduct.cs
@@ -0,0 +1,9 @@
+namespace Rehau.Sku.Assist
+{
+ interface IProduct
+ {
+ string Sku { get; }
+ string Name { get; }
+ string Uri { get; }
+ }
+}
diff --git a/Assistant/Product.cs b/Assistant/Product.cs
new file mode 100644
index 0000000..17a7065
--- /dev/null
+++ b/Assistant/Product.cs
@@ -0,0 +1,21 @@
+namespace Rehau.Sku.Assist
+{
+ public class Product : IProduct
+ {
+ public string Sku { get; }
+ public string Name { get; }
+
+ public string Uri => throw new System.NotImplementedException();
+
+ public Product(string sku, string name)
+ {
+ Sku = sku;
+ Name = name;
+ }
+
+ public override string ToString()
+ {
+ return $"{this.Name} ({this.Sku})";
+ }
+ }
+}
\ No newline at end of file
diff --git a/Assistant/SkuAssist.cs b/Assistant/SkuAssist.cs
index 1167274..9eb3328 100644
--- a/Assistant/SkuAssist.cs
+++ b/Assistant/SkuAssist.cs
@@ -1,8 +1,8 @@
-using System.Net.Http;
-using System.Threading.Tasks;
-using AngleSharp;
+using AngleSharp;
using System.Linq;
using System.Net;
+using System.Net.Http;
+using System.Threading.Tasks;
namespace Rehau.Sku.Assist
{
@@ -23,16 +23,14 @@ namespace Rehau.Sku.Assist
return await context.OpenAsync(req => req.Content(source));
}
- public static string GetResultFromDocument(AngleSharp.Dom.IDocument document)
+ public static IProduct GetProductFromDocument(AngleSharp.Dom.IDocument document)
{
- var result = document
+ return document
.All
.Where(e => e.ClassName == "product-item__desc-top")
- .Select(e => new { sku = e.Children[0].TextContent, title = e.Children[1].TextContent.Trim(new[] { '\n', ' ' }) })
- .Where(t => !t.sku.Any(c => char.IsLetter(c)))
+ .Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' })))
+ // .Where(product => !product.Sku.Any(c => char.IsLetter(c)))
.FirstOrDefault();
-
- return result == null ? "Не найдено" : $"{result.title} ({result.sku})";
}
}
}
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();
-
- public static object RAUNAME(string request)
- {
- return ExcelAsyncUtil.Run("RAUNAME", request, delegate
- {
- var document = SkuAssist.GetDocumentAsync(request, httpClient).Result;
- return SkuAssist.GetResultFromDocument(document);
- });
- }
+ private static HttpClient _httpClient;
+ private static ObjectCache _resultCache = MemoryCache.Default;
public void AutoClose()
{
}
-
public void AutoOpen()
{
+ _httpClient = new HttpClient();
+ }
+
+ [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
diff --git a/Rehau.Sku.Assist.csproj b/Rehau.Sku.Assist.csproj
index 2fc2307..29cdb12 100644
--- a/Rehau.Sku.Assist.csproj
+++ b/Rehau.Sku.Assist.csproj
@@ -38,26 +38,49 @@
packages\ExcelDna.Integration.1.5.0\lib\net452\ExcelDna.Integration.dll
+ False
packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll
+
+
+ packages\System.Configuration.ConfigurationManager.6.0.0\lib\net461\System.Configuration.ConfigurationManager.dll
+
+
+
+
packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll
+
+
+ packages\System.Security.AccessControl.6.0.0\lib\net461\System.Security.AccessControl.dll
+
+
+ packages\System.Security.Permissions.6.0.0\lib\net461\System.Security.Permissions.dll
+
+
+ packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll
+
+
packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll
+
+
+
+
diff --git a/packages.config b/packages.config
index 07a90ac..840c4e8 100644
--- a/packages.config
+++ b/packages.config
@@ -4,6 +4,11 @@
+
+
+
+
+
\ No newline at end of file