Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
Add polling change expiration token to association service
Browse files Browse the repository at this point in the history
  • Loading branch information
pushnitsa committed Oct 21, 2020
1 parent 715c806 commit 38acc3a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,39 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using VirtoCommerce.Storefront.AutoRestClients.DynamicAssociationsModuleModuleApi;
using VirtoCommerce.Storefront.Common;
using VirtoCommerce.Storefront.Model;
using VirtoCommerce.Storefront.Infrastructure;
using VirtoCommerce.Storefront.Model.Caching;
using VirtoCommerce.Storefront.Model.Catalog;
using VirtoCommerce.Storefront.Model.Common.Caching;
using VirtoCommerce.Storefront.Model.Recommendations;
using VirtoCommerce.Storefront.Model.Services;

namespace VirtoCommerce.Storefront.Domain
namespace VirtoCommerce.Storefront.Domain.Recommendations
{
public class DynamicAssociationsProvider : IRecommendationsProvider
{
private readonly ICatalogService _catalogService;
private readonly IAssociations _associationsApi;
private readonly IStorefrontMemoryCache _memoryCache;
private readonly IApiChangesWatcher _apiChangesWatcher;

public DynamicAssociationsProvider(ICatalogService catalogService, IAssociations associationsApi,
IStorefrontMemoryCache memoryCache)
public DynamicAssociationsProvider(
ICatalogService catalogService,
IAssociations associationsApi,
IStorefrontMemoryCache memoryCache,
IApiChangesWatcher apiChangesWatcher
)
{
_catalogService = catalogService;
_associationsApi = associationsApi;
_memoryCache = memoryCache;
_apiChangesWatcher = apiChangesWatcher;
}
public string ProviderName
{
get
{
return "DynamicAssociations";
}
}

public string ProviderName => "DynamicAssociations";

public Task AddEventAsync(IEnumerable<UsageEvent> events)
{
//Nothing todo
return Task.FromResult(true);
}

Expand All @@ -49,22 +48,27 @@ public RecommendationEvalContext CreateEvalContext()
public async Task<Product[]> GetRecommendationsAsync(RecommendationEvalContext context)
{
var dynamicAssociationsContext = context as DynamicAssociationsEvalContext;

if (dynamicAssociationsContext == null)
{
throw new InvalidCastException(nameof(context));
}

var cacheKey = CacheKey.With(GetType(), "GetRecommendationsAsync", context.GetCacheKey());
var cacheKey = CacheKey.With(GetType(), nameof(GetRecommendationsAsync), context.GetCacheKey());

return await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
{
cacheEntry.AddExpirationToken(RecommendationsCacheRegion.CreateChangeToken());
cacheEntry.AddExpirationToken(_apiChangesWatcher.CreateChangeToken());

var result = new List<Product>();
var recommendedProductIds = await _associationsApi.EvaluateDynamicAssociationsAsync(dynamicAssociationsContext.ToContextDto());

if (recommendedProductIds != null)
{
result.AddRange(await _catalogService.GetProductsAsync(recommendedProductIds.ToArray(), ItemResponseGroup.Seo | ItemResponseGroup.Outlines | ItemResponseGroup.ItemWithPrices | ItemResponseGroup.ItemWithDiscounts | ItemResponseGroup.Inventory));
}

return result.ToArray();
});
}
Expand Down
1 change: 1 addition & 0 deletions VirtoCommerce.Storefront/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using VirtoCommerce.Storefront.Domain;
using VirtoCommerce.Storefront.Domain.Cart.Demo;
using VirtoCommerce.Storefront.Domain.Catalog;
using VirtoCommerce.Storefront.Domain.Recommendations;
using VirtoCommerce.Storefront.Domain.Security;
using VirtoCommerce.Storefront.Extensions;
using VirtoCommerce.Storefront.Filters;
Expand Down

0 comments on commit 38acc3a

Please sign in to comment.