public async Task <SeoInfo[]> FindSeoBySlugAsync(string slug)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(FindSeoBySlugAsync), slug);

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                cacheEntry.AddExpirationToken(SeoInfoCacheRegion.CreateChangeToken());
                var result = new List <SeoInfo>();
                using (var repository = _repositoryFactory())
                {
                    // Find seo entries for specified keyword. Also add other seo entries related to found object.
                    result = (await repository.SeoInfos.Where(x => x.Keyword == slug)
                              .Join(repository.SeoInfos, x => new { x.ItemId, x.CategoryId }, y => new { y.ItemId, y.CategoryId }, (x, y) => y)
                              .ToArrayAsync()).Select(x => x.ToModel(AbstractTypeFactory <SeoInfo> .TryCreateInstance())).ToList();
                }

                return result.ToArray();
            }));
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <SeoInfo> > GetAllSeoDuplicatesAsync()
        {
            var cacheKey = CacheKey.With(GetType(), "GetAllSeoDuplicatesAsync");

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                var result = new List <SeoInfo>();
                using (var repository = _repositoryFactory())
                {
                    var dublicateSeoRecords = await repository.SeoUrlKeywords.GroupBy(x => x.Keyword + ":" + x.StoreId).Where(x => x.Count() > 1)
                                              .SelectMany(x => x).ToArrayAsync();
                    result = dublicateSeoRecords.Select(x => x.ToModel(AbstractTypeFactory <SeoInfo> .TryCreateInstance())).ToList();
                    foreach (var duplicateSeoEntity in dublicateSeoRecords)
                    {
                        var seoInfo = duplicateSeoEntity.ToModel(AbstractTypeFactory <SeoInfo> .TryCreateInstance());
                        cacheEntry.AddExpirationToken(SeoInfoCacheRegion.CreateChangeToken(seoInfo.Id));
                    }
                }
                return result;
            }));
        }
Ejemplo n.º 3
0
        public async Task <IEnumerable <SeoInfo> > GetSeoByKeywordAsync(string keyword)
        {
            var cacheKey = CacheKey.With(GetType(), "GetSeoByKeywordAsync", keyword);

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                using (var repository = _repositoryFactory())
                {
                    // Find seo entries for specified keyword. Also add other seo entries related to found object.
                    var query = await repository.SeoUrlKeywords.Where(x => x.Keyword == keyword)
                                .Join(repository.SeoUrlKeywords, x => new { x.ObjectId, x.ObjectType }, y => new { y.ObjectId, y.ObjectType }, (x, y) => y)
                                .ToArrayAsync();
                    var result = query.Select(x =>
                    {
                        var seoInfo = x.ToModel(AbstractTypeFactory <SeoInfo> .TryCreateInstance());
                        cacheEntry.AddExpirationToken(SeoInfoCacheRegion.CreateChangeToken(seoInfo.Id));
                        return seoInfo;
                    }).ToList();
                    return result;
                }
            }));
        }
Ejemplo n.º 4
0
        public async Task LoadSeoForObjectsAsync(ISeoSupport[] seoSupportObjects)
        {
            var cacheKey = CacheKey.With(GetType(), "LoadSeoForObjectsAsync", string.Join("-", seoSupportObjects.Select(x => $"{x.GetType()} : { x.Id }").Distinct()));
            await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                using (var repository = _repositoryFactory())
                {
                    var objectIds = seoSupportObjects.Where(x => x.Id != null).Select(x => x.Id).Distinct().ToArray();

                    var seoInfosEntities = await repository.SeoUrlKeywords.Where(x => objectIds.Contains(x.ObjectId)).ToArrayAsync();
                    var seoInfos         = seoInfosEntities.Select(x => x.ToModel(AbstractTypeFactory <SeoInfo> .TryCreateInstance())).ToList();

                    foreach (var seoSupportObject in seoSupportObjects)
                    {
                        seoSupportObject.SeoInfos = seoInfos.Where(x => x.ObjectId == seoSupportObject.Id && x.ObjectType == seoSupportObject.SeoObjectType).ToList();
                        foreach (var seoInfo in seoSupportObject.SeoInfos)
                        {
                            cacheEntry.AddExpirationToken(SeoInfoCacheRegion.CreateChangeToken(seoInfo.Id));
                        }
                    }
                }
                return(Task.CompletedTask);
            });
        }