Пример #1
0
        protected virtual void ClearCache(IEnumerable <CustomerSegment> customerSegments)
        {
            foreach (var customerSegment in customerSegments)
            {
                CustomerSegmentCacheRegion.ExpireEntity(customerSegment);
            }

            CustomerSegmentSearchCacheRegion.ExpireRegion();
        }
Пример #2
0
        public virtual async Task <CustomerSegmentSearchResult> SearchCustomerSegmentsAsync(CustomerSegmentSearchCriteria criteria)
        {
            ValidateParameters(criteria);

            var cacheKey = CacheKey.With(GetType(), nameof(SearchCustomerSegmentsAsync), criteria.GetCacheKey());

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

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

                using (var customerSegmentsRepositoryFactory = _customerSegmentRepositoryFactory())
                {
                    //Optimize performance and CPU usage
                    customerSegmentsRepositoryFactory.DisableChangesTracking();

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

                    result.TotalCount = await query.CountAsync();

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

                        result.Results = (await _customerSegmentService.GetByIdsAsync(ids)).OrderBy(x => Array.IndexOf(ids, x.Id)).ToList();
                    }
                }

                return result;
            }));
        }