示例#1
0
        public virtual async Task <PromotionSearchResult> SearchPromotionsAsync(PromotionSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), "SearchPromotionsAsync", criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(PromotionCacheRegion.CreateChangeToken());
                var retVal = AbstractTypeFactory <PromotionSearchResult> .TryCreateInstance();
                using (var repository = _repositoryFactory())
                {
                    var query = GetPromotionsQuery(repository, criteria);

                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] { new SortInfo {
                                                SortColumn = ReflectionUtility.GetPropertyName <Promotion>(x => x.Priority), SortDirection = SortDirection.Descending
                                            } };
                    }
                    query = query.OrderBySortInfos(sortInfos);

                    retVal.TotalCount = await query.CountAsync();

                    if (criteria.Take > 0)
                    {
                        var ids = await query.Select(x => x.Id)
                                  .Skip(criteria.Skip)
                                  .Take(criteria.Take).ToArrayAsync();
                        var promotions = await _promotionService.GetPromotionsByIdsAsync(ids);
                        retVal.Results = promotions.OrderBy(p => ids.ToList().IndexOf(p.Id)).ToList();
                    }
                }
                return retVal;
            }));
        }
        private void ClearCache(string[] promotionIds = null)
        {
            CouponCacheRegion.ExpireRegion();
            PromotionSearchCacheRegion.ExpireRegion();

            if (promotionIds != null)
            {
                PromotionCacheRegion.ExpirePromotions(promotionIds);
            }
        }
示例#3
0
        public virtual async Task DeletePromotionsAsync(string[] ids)
        {
            using (var repository = _repositoryFactory())
            {
                await repository.RemovePromotionsAsync(ids);

                await repository.UnitOfWork.CommitAsync();
            }

            PromotionCacheRegion.ExpireRegion();
        }
示例#4
0
        public virtual async Task <Promotion[]> GetPromotionsByIdsAsync(string[] ids)
        {
            var cacheKey = CacheKey.With(GetType(), "GetPromotionsByIds", string.Join("-", ids));

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(PromotionCacheRegion.CreateChangeToken());
                using (var repository = _repositoryFactory())
                {
                    var promotionEntities = await repository.GetPromotionsByIdsAsync(ids);
                    return promotionEntities.Select(x => x.ToModel(AbstractTypeFactory <DynamicPromotion> .TryCreateInstance())).ToArray();
                }
            }));
        }
示例#5
0
        public virtual async Task <Promotion[]> GetPromotionsByIdsAsync(string[] ids)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(GetPromotionsByIdsAsync), string.Join("-", ids));
            var result   = await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                //It is so important to generate change tokens for all ids even for not existing objects to prevent an issue
                //with caching of empty results for non - existing objects that have the infinitive lifetime in the cache
                //and future unavailability to create objects with these ids.
                cacheEntry.AddExpirationToken(PromotionCacheRegion.CreateChangeToken(ids));

                using (var repository = _repositoryFactory())
                {
                    var promotionEntities = await repository.GetPromotionsByIdsAsync(ids);
                    return(promotionEntities.Select(x => x.ToModel(AbstractTypeFactory <Promotion> .TryCreateInstance())).ToArray());
                }
            });

            return(result.Select(x => x.Clone() as Promotion).ToArray());
        }
示例#6
0
        public virtual async Task SavePromotionsAsync(Promotion[] promotions)
        {
            var pkMap          = new PrimaryKeyResolvingMap();
            var changedEntries = new List <GenericChangedEntry <Promotion> >();

            using (var repository = _repositoryFactory())
            {
                var existEntities = await repository.GetPromotionsByIdsAsync(promotions.Where(x => !x.IsTransient()).Select(x => x.Id).ToArray());

                foreach (var promotion in promotions.OfType <DynamicPromotion>())
                {
                    var sourceEntity = AbstractTypeFactory <PromotionEntity> .TryCreateInstance();

                    if (sourceEntity != null)
                    {
                        sourceEntity = sourceEntity.FromModel(promotion, pkMap);
                        var targetEntity = existEntities.FirstOrDefault(x => x.Id == promotion.Id);
                        if (targetEntity != null)
                        {
                            changedEntries.Add(new GenericChangedEntry <Promotion>(promotion, sourceEntity.ToModel(AbstractTypeFactory <DynamicPromotion> .TryCreateInstance()), EntryState.Modified));
                            sourceEntity.Patch(targetEntity);
                        }
                        else
                        {
                            changedEntries.Add(new GenericChangedEntry <Promotion>(promotion, EntryState.Added));
                            repository.Add(sourceEntity);
                        }
                    }
                }
                await repository.UnitOfWork.CommitAsync();

                pkMap.ResolvePrimaryKeys();
                await _eventPublisher.Publish(new PromotionChangedEvent(changedEntries));
            }

            PromotionCacheRegion.ExpireRegion();
        }
示例#7
0
 protected virtual void ClearCache(string[] promotionIds)
 {
     PromotionSearchCacheRegion.ExpireRegion();
     PromotionCacheRegion.ExpirePromotions(promotionIds);
 }
 private static void ClearCache(string[] promotionIds)
 {
     PromotionSearchCacheRegion.ExpireRegion();
     PromotionCacheRegion.ExpirePromotions(promotionIds);
 }