コード例 #1
0
        public async Task <SubscriptionSearchResult> SearchAsync(SubscriptionSearchCriteria searchCriteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchAsync), searchCriteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(SubscriptionSearchCacheRegion.CreateChangeToken());
                var result = new SubscriptionSearchResult();

                using (var repository = _subscriptionRepositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var sortInfos = BuildSortExpression(searchCriteria);
                    var query = BuildQuery(searchCriteria, repository);

                    result.TotalCount = await query.CountAsync();

                    if (searchCriteria.Take > 0 && result.TotalCount > 0)
                    {
                        var subscriptionIds = query.OrderBySortInfos(sortInfos)
                                              .ThenBy(x => x.Id)
                                              .Select(x => x.Id)
                                              .Skip(searchCriteria.Skip)
                                              .Take(searchCriteria.Take)
                                              .ToArray();

                        result.Results = (await _subscriptionService.GetByIdsAsync(subscriptionIds, searchCriteria.ResponseGroup)).OrderBy(x => Array.IndexOf(subscriptionIds, x.Id)).ToArray();
                    }
                }

                return result;
            }));
        }
コード例 #2
0
        public async Task <IPagedList <Subscription> > SearchSubscriptionsAsync(SubscriptionSearchCriteria criteria)
        {
            var workContext = _workContextAccessor.WorkContext;
            var cacheKey    = CacheKey.With(GetType(), "SearchSubscription", criteria.GetCacheKey());

            return(await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                //Observe all subscriptions for current user and invalidate them in cache if any changed (one limitation - this toke doesn't detect subscriptions deletions)
                cacheEntry.AddExpirationToken(new PollingApiSubscriptionsChangeToken(_subscriptionApi, _options.ChangesPollingInterval));
                cacheEntry.AddExpirationToken(SubscriptionCacheRegion.CreateCustomerSubscriptionChangeToken(criteria.CustomerId));
                var result = await _subscriptionApi.SearchSubscriptionsAsync(criteria.ToSearchCriteriaDto());
                return new StaticPagedList <Subscription>(result.Subscriptions.Select(x => x.ToSubscription(workContext.AllCurrencies, workContext.CurrentLanguage)),
                                                          criteria.PageNumber, criteria.PageSize, result.TotalCount.Value);
            }));
        }
コード例 #3
0
        public IPagedList <Subscription> SearchSubscription(SubscriptionSearchCriteria criteria)
        {
            var workContext = _workContextAccessor.WorkContext;
            //It is very important to have both versions for Sync and Async methods with same cache key due to performance for multithreaded requests
            //you should avoid of call async version with TaskFactory.StartNew() out of the cache getter function
            var cacheKey = CacheKey.With(GetType(), "SearchSubscription", criteria.GetCacheKey());

            return(_memoryCache.GetOrCreateExclusive(cacheKey, (cacheEntry) =>
            {
                //Observe all subscriptions for current user and invalidate them in cache if any changed (one limitation - this toke doesn't detect subscriptions deletions)
                cacheEntry.AddExpirationToken(new PoolingApiSubscriptionsChangeToken(_subscriptionApi, _options.ChangesPoolingInterval));
                cacheEntry.AddExpirationToken(SubscriptionCacheRegion.CreateCustomerSubscriptionChangeToken(criteria.CustomerId));
                var result = _subscriptionApi.SearchSubscriptions(criteria.ToSearchCriteriaDto());
                return new StaticPagedList <Subscription>(result.Subscriptions.Select(x => x.ToSubscription(workContext.AllCurrencies, workContext.CurrentLanguage)),
                                                          criteria.PageNumber, criteria.PageSize, result.TotalCount.Value);
            }));
        }
コード例 #4
0
        public virtual async Task <SubscriptionSearchResult> SearchSubscriptionsAsync(SubscriptionSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchSubscriptionsAsync), criteria.GetCacheKey());

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

                var retVal = AbstractTypeFactory <SubscriptionSearchResult> .TryCreateInstance();
                using (var repository = _subscriptionRepositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var query = BuildQuery(repository, criteria);
                    var sortInfos = BuildSortExpression(criteria);

                    retVal.TotalCount = await query.CountAsync();

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

                        //Load subscriptions with preserving sorting order
                        var unorderedResults = await _subscriptionService.GetByIdsAsync(subscriptionsIds, criteria.ResponseGroup);
                        retVal.Results = unorderedResults.OrderBy(x => Array.IndexOf(subscriptionsIds, x.Id)).ToArray();
                    }

                    return retVal;
                }
            }));
        }
コード例 #5
0
        public virtual async Task <GenericSearchResult <Subscription> > SearchSubscriptionsAsync(SubscriptionSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchSubscriptionsAsync), criteria.GetCacheKey());

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

                var retVal = new GenericSearchResult <Subscription>();
                using (var repository = SubscriptionRepositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var query = await GetSubscriptionsQueryForCriteria(repository, criteria);

                    retVal.TotalCount = await query.CountAsync();

                    if (criteria.Take > 0)
                    {
                        var subscriptionEntities = await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        var subscriptionsIds = subscriptionEntities.Select(x => x.Id).ToArray();

                        //Load subscriptions with preserving sorting order
                        var unorderedResults = await GetByIdsAsync(subscriptionsIds, criteria.ResponseGroup);
                        retVal.Results = unorderedResults.OrderBy(x => Array.IndexOf(subscriptionsIds, x.Id)).ToArray();
                    }

                    return retVal;
                }
            }));
        }