diff --git a/BBWY.Client/APIServices/OneBoundAPIService.cs b/BBWY.Client/APIServices/OneBoundAPIService.cs index a7a77013..a5898855 100644 --- a/BBWY.Client/APIServices/OneBoundAPIService.cs +++ b/BBWY.Client/APIServices/OneBoundAPIService.cs @@ -29,6 +29,7 @@ namespace BBWY.Client.APIServices { try { + https://api-gw.onebound.cn/1688/item_get/key=t5060712539&secret=20211103&num_iid=649560646832&lang=zh-CN&cache=no var result = restApiService.SendRequest("https://api-gw.onebound.cn/", $"{platform}/item_get", $"key={key}&secret={secret}&num_iid={productId}&lang=zh-CN&cache=no", null, HttpMethod.Get, paramPosition: ParamPosition.Query, enableRandomTimeStamp: true); if (result.StatusCode != System.Net.HttpStatusCode.OK) throw new Exception($"{result.StatusCode} {result.Content}"); diff --git a/BBWY.Client/APIServices/PurchaseProductAPIService.cs b/BBWY.Client/APIServices/PurchaseProductAPIService.cs index 86432de9..b6ecab86 100644 --- a/BBWY.Client/APIServices/PurchaseProductAPIService.cs +++ b/BBWY.Client/APIServices/PurchaseProductAPIService.cs @@ -2,6 +2,7 @@ using BBWY.Client.Models; using BBWY.Common.Http; using BBWY.Common.Models; +using Microsoft.Extensions.Caching.Memory; using Newtonsoft.Json.Linq; using System; using System.Collections.Concurrent; @@ -16,15 +17,21 @@ namespace BBWY.Client.APIServices public class PurchaseProductAPIService : IDenpendency { private RestApiService restApiService; + private IMemoryCache memoryCache; private string oneBoundKey = "t5060712539"; private string oneBoundSecret = "20211103"; - private ConcurrentDictionary purchaseSchemeProductSkus)> productChaches; + + private TimeSpan purchaseProductCacheTimeSpan; + //private TimeSpan _1688SessionIdTimeSpan; + + //private ConcurrentDictionary purchaseSchemeProductSkus)> productChaches; private IDictionary _1688ProductDetailRequestHeader; - public PurchaseProductAPIService(RestApiService restApiService) + public PurchaseProductAPIService(RestApiService restApiService, IMemoryCache memoryCache) { this.restApiService = restApiService; + this.memoryCache = memoryCache; _1688ProductDetailRequestHeader = new Dictionary() { { "Host","detail.1688.com"}, @@ -33,12 +40,14 @@ namespace BBWY.Client.APIServices { "Accept-Encoding","gzip, deflate, br"}, { "Accept-Language","zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"} }; - productChaches = new ConcurrentDictionary purchaseSchemeProductSkus)>(); + purchaseProductCacheTimeSpan = TimeSpan.FromDays(1); + //_1688SessionIdTimeSpan = TimeSpan.FromMinutes(10); + //productChaches = new ConcurrentDictionary purchaseSchemeProductSkus)>(); } public (Purchaser purchaser, IList purchaseSchemeProductSkus)? GetProductInfo(Platform platform, string productId, string skuId, string purchaseProductId, PurchaseOrderMode priceMode, PurchaseProductAPIMode apiMode) { - if (productChaches.TryGetValue($"{purchaseProductId}_{priceMode}", out (Purchaser purchaser, IList purchaseSchemeProductSkus) tuple)) + if (memoryCache.TryGetValue<(Purchaser, IList)>($"{purchaseProductId}_{priceMode}", out var tuple)) return tuple.Copy(); (Purchaser purchaser, IList purchaseSchemeProductSkus)? data = null; @@ -57,7 +66,14 @@ namespace BBWY.Client.APIServices } if (data != null) - productChaches.TryAdd($"{purchaseProductId}_{priceMode}", data.Value); + { + try + { + memoryCache.Set<(Purchaser, IList)>($"{purchaseProductId}_{priceMode}", data.Value, purchaseProductCacheTimeSpan); + } + catch { } + } + return data?.Copy(); } @@ -189,7 +205,7 @@ namespace BBWY.Client.APIServices imageUrl = j.Value("imageUrl") }).ToList(); - var price = jobject["data"]["1081181309582"]["data"]["priceModel"]["currentPrices"][0].Value("price"); + var firstPrice = jobject["data"]["1081181309582"]["data"]["priceModel"]["currentPrices"][0].Value("price"); var purchaseSchemeProductSkus = new List(); @@ -200,12 +216,14 @@ namespace BBWY.Client.APIServices var matchName = name.Contains(">") ? name.Substring(0, name.IndexOf(">")) : name; var value = jskuProperty.Value; + var skuPrice = value.Value("price"); + purchaseSchemeProductSkus.Add(new PurchaseSchemeProductSku() { ProductId = productId, SkuId = skuId, PurchaseProductId = purchaseProductId, - Price = price, + Price = skuPrice == 0M ? firstPrice : skuPrice, Title = name, PurchaseSkuId = value.Value("skuId"), PurchaseSkuSpecId = value.Value("specId"), diff --git a/BBWY.Client/App.xaml.cs b/BBWY.Client/App.xaml.cs index 7ad30a21..760a8ef5 100644 --- a/BBWY.Client/App.xaml.cs +++ b/BBWY.Client/App.xaml.cs @@ -75,6 +75,8 @@ namespace BBWY.Client { AutomaticDecompression = System.Net.DecompressionMethods.GZip }); + + serviceCollection.AddMemoryCache(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(gl); serviceCollection.BatchRegisterServices(new Assembly[] { Assembly.Load("BBWY.Client") }, typeof(IDenpendency)); diff --git a/BBWY.Client/BBWY.Client.csproj b/BBWY.Client/BBWY.Client.csproj index 36a7ead6..67503ccc 100644 --- a/BBWY.Client/BBWY.Client.csproj +++ b/BBWY.Client/BBWY.Client.csproj @@ -29,6 +29,7 @@ +