public virtual async Task <PromotionSearchResult> SearchPromotionsAsync(PromotionSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPromotionsAsync), criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(PromotionSearchCacheRegion.CreateChangeToken());

                var result = AbstractTypeFactory <PromotionSearchResult> .TryCreateInstance();

                using (var repository = _repositoryFactory())
                {
                    var sortInfos = BuildSortExpression(criteria);
                    var query = BuildQuery(repository, criteria);
                    //https://github.com/zzzprojects/EntityFramework-Plus/issues/293
                    //Workaround ArgumentException with CountAsync have no predicate when query has a Cast or OfType expression
                    result.TotalCount = await query.CountAsync(x => true);

                    if (criteria.Take > 0)
                    {
                        var ids = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id)
                                  .Select(x => x.Id)
                                  .Skip(criteria.Skip).Take(criteria.Take)
                                  .ToArrayAsync();

                        result.Results = (await _promotionService.GetPromotionsByIdsAsync(ids))
                                         .OrderBy(x => Array.IndexOf(ids, x.Id)).ToList();
                    }
                }
                return result;
            }));
        }
        private void ClearCache(string[] promotionIds = null)
        {
            CouponCacheRegion.ExpireRegion();
            PromotionSearchCacheRegion.ExpireRegion();

            if (promotionIds != null)
            {
                PromotionCacheRegion.ExpirePromotions(promotionIds);
            }
        }
        public virtual async Task <PromotionResult> EvaluatePromotionAsync(IEvaluationContext context)
        {
            var promoContext = GetPromotionEvaluationContext(context);

            var cacheKey = CacheKey.With(GetType(), nameof(EvaluatePromotionAsync), string.Join("-", promoContext.GetCacheKey()));
            var result   = await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.SlidingExpiration = TimeSpan.FromMinutes(1);
                cacheEntry.AddExpirationToken(PromotionSearchCacheRegion.CreateChangeToken());

                return(await EvaluatePromotionWithoutCache(promoContext));
            });

            return(result);
        }
Exemple #4
0
 protected virtual void ClearCache(string[] promotionIds)
 {
     PromotionSearchCacheRegion.ExpireRegion();
     PromotionCacheRegion.ExpirePromotions(promotionIds);
 }
 private static void ClearCache(string[] promotionIds)
 {
     PromotionSearchCacheRegion.ExpireRegion();
     PromotionCacheRegion.ExpirePromotions(promotionIds);
 }