/// <summary>
        /// Executed when the cache updates, which means we can know what the new URL is for a given document
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="cacheRefresherEventArgs"></param>
        private void PageCacheRefresher_CacheUpdated(PageCacheRefresher sender, CacheRefresherEventArgs cacheRefresherEventArgs)
        {
            // only on master / single, not on replicas!
            if (IsReplicaServer)
            {
                return;
            }

            // simply getting OldRoutes will register it in the request cache,
            // so whatever we do with it, try/finally it to ensure it's cleared

            try
            {
                foreach (var oldRoute in OldRoutes)
                {
                    // assuming we cannot have 'CacheUpdated' for only part of the infos else we'd need
                    // to set a flag in 'Published' to indicate which entities have been refreshed ok
                    CreateRedirect(oldRoute.Key, oldRoute.Value.Item1, oldRoute.Value.Item2);
                }
            }
            finally
            {
                OldRoutes.Clear();
                RequestCache.ClearCacheItem(ContextKey3);
            }
        }
 private void PageCacheRefresher_CacheUpdated(PageCacheRefresher sender, CacheRefresherEventArgs e)
 {
     if (e.MessageType == Umbraco.Core.Sync.MessageType.RefreshAll)
     {
         VortoUrlRouteCache.Current.StartNewCacheFile();
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles all cache 'requests', and checks to see if the current machine should respond (with another 'cache refresher')
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CacheWiper_Request(CacheWiper sender, CacheRefresherEventArgs e)
        {
            var rawPayLoad = (string)e.MessageObject;

            var payload = JsonConvert.DeserializeObject <CachedImagesWipe>(rawPayLoad);

            if (
                ApplicationContext.Current.Services.ServerRegistrationService.CurrentServerIdentity.InvariantEquals(
                    payload.ServerIdentity))
            {
                // This server should wipe it's application cache

                if (payload.WebUrl != null)
                {
                    // wipe specific url
                    var cachePrefix = AzureCDNToolkit.Constants.Keys.CachePrefix;
                    var cacheKey    = string.Format("{0}{1}", cachePrefix, payload.WebUrl);
                    Cache.ClearCacheItem(cacheKey);

                    LogHelper.Info <CacheEvents>(string.Format("Azure CDN Toolkit: CDN image path runtime cache for key {0} cleared by dashboard control request", payload.WebUrl));
                }
                else
                {
                    // clear all keys
                    Cache.ClearCacheByKeySearch(AzureCDNToolkit.Constants.Keys.CachePrefix);

                    LogHelper.Info <CacheEvents>("Azure CDN Toolkit: CDN image path runtime cache cleared by dashboard control request");
                }
            }
        }
Ejemplo n.º 4
0
 private void PageCacheRefresher_CacheUpdated(PageCacheRefresher sender, CacheRefresherEventArgs e)
 {
     if (e.MessageType == MessageType.RefreshByInstance && e.MessageObject is IContent instance)
     {
         GuidToIdCache.TryAdd(instance);
     }
 }
Ejemplo n.º 5
0
        private void MediaCacheRefresherUpdated(MediaCacheRefresher sender, CacheRefresherEventArgs args)
        {
            if (args.MessageType != MessageType.RefreshByPayload)
            {
                throw new NotSupportedException();
            }

            var mediaService = _services.MediaService;

            foreach (var payload in (MediaCacheRefresher.JsonPayload[])args.MessageObject)
            {
                if (payload.ChangeTypes.HasType(TreeChangeTypes.Remove))
                {
                    // remove from *all* indexes
                    DeleteIndexForEntity(payload.Id, false);
                }
                else if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshAll))
                {
                    // ExamineEvents does not support RefreshAll
                    // just ignore that payload
                    // so what?!
                }
                else // RefreshNode or RefreshBranch (maybe trashed)
                {
                    var media = mediaService.GetById(payload.Id);
                    if (media == null)
                    {
                        // gone fishing, remove entirely
                        DeleteIndexForEntity(payload.Id, false);
                        continue;
                    }

                    if (media.Trashed)
                    {
                        DeleteIndexForEntity(payload.Id, true);
                    }

                    // just that media
                    ReIndexForMedia(media, !media.Trashed);

                    // branch
                    if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshBranch))
                    {
                        const int pageSize = 500;
                        var       page     = 0;
                        var       total    = long.MaxValue;
                        while (page * pageSize < total)
                        {
                            var descendants = mediaService.GetPagedDescendants(media.Id, page++, pageSize, out total);
                            foreach (var descendant in descendants)
                            {
                                ReIndexForMedia(descendant, !descendant.Trashed);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CacheResponder_Response(CacheResponder sender, CacheRefresherEventArgs e)
        {
            var rawPayLoad = (string)e.MessageObject;
            var payload    = JsonConvert.DeserializeObject <CachedImagesResponse>(rawPayLoad);

            var cacheKey = string.Format("{0}{1}", AzureCDNToolkit.Constants.Keys.CachePrefixResponse, payload.RequestId);

            Cache.InsertCacheItem <IEnumerable <CachedImage> >(cacheKey, () => payload.CachedImages);
        }
Ejemplo n.º 7
0
        private void MemberCacheRefresherUpdated(MemberCacheRefresher sender, CacheRefresherEventArgs args)
        {
            if (Suspendable.ExamineEvents.CanIndex == false)
            {
                return;
            }

            switch (args.MessageType)
            {
            case MessageType.RefreshById:
                var c1 = _services.MemberService.GetById((int)args.MessageObject);
                if (c1 != null)
                {
                    ReIndexForMember(c1);
                }
                break;

            case MessageType.RemoveById:

                // This is triggered when the item is permanently deleted

                DeleteIndexForEntity((int)args.MessageObject, false);
                break;

            case MessageType.RefreshByInstance:
                if (args.MessageObject is IMember c3)
                {
                    ReIndexForMember(c3);
                }
                break;

            case MessageType.RemoveByInstance:

                // This is triggered when the item is permanently deleted

                if (args.MessageObject is IMember c4)
                {
                    DeleteIndexForEntity(c4.Id, false);
                }
                break;

            case MessageType.RefreshByPayload:
                var payload = (MemberCacheRefresher.JsonPayload[])args.MessageObject;
                var members = payload.Select(x => _services.MemberService.GetById(x.Id));
                foreach (var m in members)
                {
                    ReIndexForMember(m);
                }
                break;

            case MessageType.RefreshAll:
            case MessageType.RefreshByJson:
            default:
                //We don't support these, these message types will not fire for unpublished content
                break;
            }
        }
Ejemplo n.º 8
0
 private void DataTypeCacheRefresher_Updated(DataTypeCacheRefresher sender, CacheRefresherEventArgs e)
 {
     if (e.MessageType == MessageType.RefreshByJson)
     {
         var payload = DeserializeFromJsonPayload((string)e.MessageObject);
         foreach (var item in payload)
         {
             var cacheKey = string.Format("{0}{1}", Constants.Keys.CachePrefix, item.Id);
             LocalCache.ClearLocalCacheItem(cacheKey);
         }
     }
 }
Ejemplo n.º 9
0
        static void MemberCacheRefresherCacheUpdated(MemberCacheRefresher sender, CacheRefresherEventArgs e)
        {
            if (Suspendable.ExamineEvents.CanIndex == false)
            {
                return;
            }

            switch (e.MessageType)
            {
            case MessageType.RefreshById:
                var c1 = ApplicationContext.Current.Services.MemberService.GetById((int)e.MessageObject);
                if (c1 != null)
                {
                    ReIndexForMember(c1);
                }
                break;

            case MessageType.RemoveById:

                // This is triggered when the item is permanently deleted

                DeleteIndexForEntity((int)e.MessageObject, false);
                break;

            case MessageType.RefreshByInstance:
                var c3 = e.MessageObject as IMember;
                if (c3 != null)
                {
                    ReIndexForMember(c3);
                }
                break;

            case MessageType.RemoveByInstance:

                // This is triggered when the item is permanently deleted

                var c4 = e.MessageObject as IMember;
                if (c4 != null)
                {
                    DeleteIndexForEntity(c4.Id, false);
                }
                break;

            case MessageType.RefreshAll:
            case MessageType.RefreshByJson:
            default:
                //We don't support these, these message types will not fire for unpublished content
                break;
            }
        }
Ejemplo n.º 10
0
 private void DataTypeCacheRefresher_Updated(DataTypeCacheRefresher sender, CacheRefresherEventArgs e)
 {
     if (e.MessageType == MessageType.RefreshByJson)
     {
         var payload = JsonConvert.DeserializeObject <JsonPayload[]>((string)e.MessageObject);
         if (payload != null)
         {
             foreach (var item in payload)
             {
                 NestedContentHelper.ClearCache(item.Id);
             }
         }
     }
 }
Ejemplo n.º 11
0
        private void HandleCacheUpdated(ContentCacheRefresher sender, CacheRefresherEventArgs args)
        {
            if (args.MessageType != MessageType.RefreshByPayload)
            {
                return;
            }
            var payloads           = (ContentCacheRefresher.JsonPayload[])args.MessageObject;
            var hubContextInstance = _hubContext.Value;

            foreach (var payload in payloads)
            {
                var id = payload.Id; // keep it simple for now, ignore ChangeTypes
                hubContextInstance.Clients.All.refreshed(id);
            }
        }
Ejemplo n.º 12
0
        private void MemberCacheRefresherUpdated(MemberCacheRefresher sender, CacheRefresherEventArgs args)
        {
            switch (args.MessageType)
            {
            case MessageType.RefreshById:
                var c1 = _services.MemberService.GetById((int)args.MessageObject);
                if (c1 != null)
                {
                    ReIndexForMember(c1);
                }

                break;

            case MessageType.RemoveById:

                // This is triggered when the item is permanently deleted

                DeleteIndexForEntity((int)args.MessageObject, false);
                break;

            case MessageType.RefreshByInstance:
                if (args.MessageObject is IMember c3)
                {
                    ReIndexForMember(c3);
                }

                break;

            case MessageType.RemoveByInstance:

                // This is triggered when the item is permanently deleted

                if (args.MessageObject is IMember c4)
                {
                    DeleteIndexForEntity(c4.Id, false);
                }

                break;

            case MessageType.RefreshAll:
            case MessageType.RefreshByJson:
            default:
                //We don't support these, these message types will not fire for unpublished content
                break;
            }
        }
        /// <summary>
        /// Content cache was updated.
        /// </summary>
        private void ContentCacheRefresher_CacheUpdated(ContentCacheRefresher contentCacheRefresher,
                                                        CacheRefresherEventArgs e)
        {
            var kind = e.MessageType;

            if (kind == MessageType.RefreshById || kind == MessageType.RemoveById)
            {
                var id = e.MessageObject as int?;
                if (id.HasValue)
                {
                    var node = ContentService.GetById(id.Value);
                    if (node != null)
                    {
                        HandleChangedContent(new[] { node });
                    }
                }
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Content cache was updated.
        /// </summary>
        private void PageCacheRefresher_CacheUpdated(PageCacheRefresher sender,
                                                     CacheRefresherEventArgs e)
        {
            var kind = e.MessageType;

            if (kind == MessageType.RefreshById || kind == MessageType.RemoveById)
            {
                var id = e.MessageObject as int?;
                if (id.HasValue)
                {
                    var contentService = ApplicationContext.Current.Services.ContentService;
                    var node           = contentService.GetById(id.Value);
                    if (node != null)
                    {
                        HandleChangedContent(new[] { node });
                    }
                }
            }
        }
        private void OnCacheUpdated(DomainsCacheRefresher sender, CacheRefresherEventArgs args)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Skybrud Domains cache cleared by ICacheRefresher Event");
            sb.AppendLine("Type: " + args.MessageType);
            sb.AppendLine("Object: " + (args.MessageObject == null ? "NULL" : args.MessageObject + " (" + args.MessageObject.GetType() + ")") + "");
            sb.AppendLine();

            LogHelper.Info <DistributedCacheEventHandlers>(sb + "");

            switch (args.MessageType)
            {
            case MessageType.RefreshAll:
                DomainsRepository.Current.RebuildCache();
                break;

            case MessageType.RefreshById:
                if (args.MessageObject is Guid)
                {
                    DomainsRepository.Current.RefreshDomainInCache((Guid)args.MessageObject);
                }
                else if (args.MessageObject is Int32)
                {
                    DomainsRepository.Current.RefreshDomainInCache((int)args.MessageObject);
                }
                break;

            case MessageType.RemoveById:
                if (args.MessageObject is Guid)
                {
                    DomainsRepository.Current.RemoveDomainFromCache((Guid)args.MessageObject);
                }
                else if (args.MessageObject is Int32)
                {
                    DomainsRepository.Current.RemoveDomainFromCache((int)args.MessageObject);
                }
                break;
            }
        }
Ejemplo n.º 16
0
        private void ContentService_RepublishAll(PageCacheRefresher sender, CacheRefresherEventArgs e)
        {
            if (e.MessageType == MessageType.RefreshAll && AllowProcessing)
            {
                var contents       = new Stack <Umbraco.Core.Models.IContent>();
                var contentService = ApplicationContext.Current.Services.ContentService;
                var found          = new List <Umbraco.Core.Models.IContent>();

                foreach (var content in contentService.GetChildren(Umbraco.Core.Constants.System.Root))
                {
                    if (content.Published)
                    {
                        contents.Push(content);
                    }
                }

                while (contents.Count != 0)
                {
                    var content = contents.Pop();
                    foreach (var child in contentService.GetChildren(content.Id))
                    {
                        if (child.Published)
                        {
                            contents.Push(child);
                        }
                    }
                    found.Add(content);
                    if (found.Count == BatchSize)
                    {
                        Sync(found.Select(x => x.Key), found);
                        found.Clear();
                        System.Threading.Thread.Sleep(BatchSleep);
                    }
                }

                Sync(found.Select(x => x.Key), found);
            }
        }
Ejemplo n.º 17
0
        private void LanguageCacheRefresherUpdated(LanguageCacheRefresher sender, CacheRefresherEventArgs e)
        {
            if (!(e.MessageObject is LanguageCacheRefresher.JsonPayload[] payloads))
            {
                return;
            }

            if (payloads.Length == 0)
            {
                return;
            }

            var removedOrCultureChanged = payloads.Any(x =>
                                                       x.ChangeType == LanguageCacheRefresher.JsonPayload.LanguageChangeType.ChangeCulture ||
                                                       x.ChangeType == LanguageCacheRefresher.JsonPayload.LanguageChangeType.Remove);

            if (removedOrCultureChanged)
            {
                //if a lang is removed or it's culture has changed, we need to rebuild the indexes since
                //field names and values in the index have a string culture value.
                _backgroundIndexRebuilder.RebuildIndexes(false);
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Handles all cache 'requests', and checks to see if the current machine should respond (with another 'cache refresher')
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CacheRequester_Request(CacheRequester sender, CacheRefresherEventArgs e)
        {
            var rawPayLoad = (string)e.MessageObject;

            var payload = JsonConvert.DeserializeObject <CachedImagesRequest>(rawPayLoad);

            if (
                ApplicationContext.Current.Services.ServerRegistrationService.CurrentServerIdentity.InvariantEquals(
                    payload.ServerIdentity))
            {
                // THIS SERVER SHOULD RETURN DATA VIA CacheImagesResponder

                var cachedItems = Cache.GetCacheItemsByKeySearch <CachedImage>(AzureCDNToolkit.Constants.Keys.CachePrefix);

                var response = new CachedImagesResponse()
                {
                    RequestId    = payload.RequestId,
                    CachedImages = cachedItems
                };

                var json = JsonConvert.SerializeObject(response);
                DistributedCache.Instance.RefreshByJson(CacheResponder.Guid, json);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Updates indexes based on content type changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void ContentTypeCacheRefresherUpdated(ContentTypeCacheRefresher sender, CacheRefresherEventArgs args)
        {
            if (Suspendable.ExamineEvents.CanIndex == false)
            {
                return;
            }

            if (args.MessageType != MessageType.RefreshByPayload)
            {
                throw new NotSupportedException();
            }

            var changedIds = new Dictionary <string, (List <int> removedIds, List <int> refreshedIds, List <int> otherIds)>();

            foreach (var payload in (ContentTypeCacheRefresher.JsonPayload[])args.MessageObject)
            {
                if (!changedIds.TryGetValue(payload.ItemType, out var idLists))
                {
                    idLists = (removedIds : new List <int>(), refreshedIds : new List <int>(), otherIds : new List <int>());
                    changedIds.Add(payload.ItemType, idLists);
                }

                if (payload.ChangeTypes.HasType(ContentTypeChangeTypes.Remove))
                {
                    idLists.removedIds.Add(payload.Id);
                }
                else if (payload.ChangeTypes.HasType(ContentTypeChangeTypes.RefreshMain))
                {
                    idLists.refreshedIds.Add(payload.Id);
                }
                else if (payload.ChangeTypes.HasType(ContentTypeChangeTypes.RefreshOther))
                {
                    idLists.otherIds.Add(payload.Id);
                }
            }

            const int pageSize = 500;

            foreach (var ci in changedIds)
            {
                if (ci.Value.refreshedIds.Count > 0 || ci.Value.otherIds.Count > 0)
                {
                    switch (ci.Key)
                    {
                    case var itemType when itemType == typeof(IContentType).Name:
                        RefreshContentOfContentTypes(ci.Value.refreshedIds.Concat(ci.Value.otherIds).Distinct().ToArray());
                        break;

                    case var itemType when itemType == typeof(IMediaType).Name:
                        RefreshMediaOfMediaTypes(ci.Value.refreshedIds.Concat(ci.Value.otherIds).Distinct().ToArray());
                        break;

                    case var itemType when itemType == typeof(IMemberType).Name:
                        RefreshMemberOfMemberTypes(ci.Value.refreshedIds.Concat(ci.Value.otherIds).Distinct().ToArray());
                        break;
                    }
                }

                //Delete all content of this content/media/member type that is in any content indexer by looking up matched examine docs
                foreach (var id in ci.Value.removedIds)
                {
                    foreach (var index in _examineManager.Indexes.OfType <IUmbracoIndex>())
                    {
                        var searcher = index.GetSearcher();

                        var page  = 0;
                        var total = long.MaxValue;
                        while (page * pageSize < total)
                        {
                            //paging with examine, see https://shazwazza.com/post/paging-with-examine/
                            var results = searcher.CreateQuery().Field("nodeType", id.ToInvariantString()).Execute(maxResults: pageSize * (page + 1));
                            total = results.TotalItemCount;
                            var paged = results.Skip(page * pageSize);

                            foreach (var item in paged)
                            {
                                if (int.TryParse(item.Id, out var contentId))
                                {
                                    DeleteIndexForEntity(contentId, false);
                                }
                            }
                            page++;
                        }
                    }
                }
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Updates indexes based on content changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void ContentCacheRefresherUpdated(ContentCacheRefresher sender, CacheRefresherEventArgs args)
        {
            if (Suspendable.ExamineEvents.CanIndex == false)
            {
                return;
            }

            if (args.MessageType != MessageType.RefreshByPayload)
            {
                throw new NotSupportedException();
            }

            var contentService = _services.ContentService;

            foreach (var payload in (ContentCacheRefresher.JsonPayload[])args.MessageObject)
            {
                if (payload.ChangeTypes.HasType(TreeChangeTypes.Remove))
                {
                    // delete content entirely (with descendants)
                    //  false: remove entirely from all indexes
                    DeleteIndexForEntity(payload.Id, false);
                }
                else if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshAll))
                {
                    // ExamineEvents does not support RefreshAll
                    // just ignore that payload
                    // so what?!

                    // TODO: Rebuild the index at this point?
                }
                else // RefreshNode or RefreshBranch (maybe trashed)
                {
                    // don't try to be too clever - refresh entirely
                    // there has to be race conditions in there ;-(

                    var content = contentService.GetById(payload.Id);
                    if (content == null)
                    {
                        // gone fishing, remove entirely from all indexes (with descendants)
                        DeleteIndexForEntity(payload.Id, false);
                        continue;
                    }

                    IContent published = null;
                    if (content.Published && contentService.IsPathPublished(content))
                    {
                        published = content;
                    }

                    if (published == null)
                    {
                        DeleteIndexForEntity(payload.Id, true);
                    }

                    // just that content
                    ReIndexForContent(content, published != null);

                    // branch
                    if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshBranch))
                    {
                        var       masked   = published == null ? null : new List <int>();
                        const int pageSize = 500;
                        var       page     = 0;
                        var       total    = long.MaxValue;
                        while (page * pageSize < total)
                        {
                            var descendants = contentService.GetPagedDescendants(content.Id, page++, pageSize, out total,
                                                                                 //order by shallowest to deepest, this allows us to check it's published state without checking every item
                                                                                 ordering: Ordering.By("Path", Direction.Ascending));

                            foreach (var descendant in descendants)
                            {
                                published = null;
                                if (masked != null) // else everything is masked
                                {
                                    if (masked.Contains(descendant.ParentId) || !descendant.Published)
                                    {
                                        masked.Add(descendant.Id);
                                    }
                                    else
                                    {
                                        published = descendant;
                                    }
                                }

                                ReIndexForContent(descendant, published != null);
                            }
                        }
                    }
                }

                // NOTE
                //
                // DeleteIndexForEntity is handled by UmbracoContentIndexer.DeleteFromIndex() which takes
                //  care of also deleting the descendants
                //
                // ReIndexForContent is NOT taking care of descendants so we have to reload everything
                //  again in order to process the branch - we COULD improve that by just reloading the
                //  XML from database instead of reloading content & re-serializing!
                //
                // BUT ... pretty sure it is! see test "Index_Delete_Index_Item_Ensure_Heirarchy_Removed"
            }
        }
        /// <summary>
        /// Handle the cache refresher event to update the index
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void MediaCacheRefresherUpdated(MediaCacheRefresher sender, CacheRefresherEventArgs args)
        {
            if (args.MessageType != MessageType.RefreshByPayload)
            {
                throw new NotSupportedException();
            }

            foreach (var payload in (MediaCacheRefresher.JsonPayload[])args.MessageObject)
            {
                if (payload.ChangeTypes.HasType(TreeChangeTypes.Remove))
                {
                    _pdfIndexPopulator.RemoveFromIndex(payload.Id);
                }
                else if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshAll))
                {
                    // ExamineEvents does not support RefreshAll
                    // just ignore that payload
                    // so what?!
                }
                else // RefreshNode or RefreshBranch (maybe trashed)
                {
                    var media = _mediaService.GetById(payload.Id);
                    if (media == null)
                    {
                        // gone fishing, remove entirely
                        _pdfIndexPopulator.RemoveFromIndex(payload.Id);
                        continue;
                    }

                    if (media.Trashed)
                    {
                        _pdfIndexPopulator.RemoveFromIndex(payload.Id);
                    }
                    else
                    {
                        _pdfIndexPopulator.AddToIndex(media);
                    }

                    // branch
                    if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshBranch))
                    {
                        const int pageSize = 500;
                        var       page     = 0;
                        var       total    = long.MaxValue;
                        while (page * pageSize < total)
                        {
                            var descendants = _mediaService.GetPagedDescendants(media.Id, page++, pageSize, out total);
                            foreach (var descendant in descendants)
                            {
                                if (descendant.Trashed)
                                {
                                    _pdfIndexPopulator.RemoveFromIndex(descendant);
                                }
                                else
                                {
                                    _pdfIndexPopulator.AddToIndex(descendant);
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// This is used to refresh content indexers IndexData based on the DataService whenever a content type is changed since
        /// properties may have been added/removed, then we need to re-index any required data if aliases have been changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// See: http://issues.umbraco.org/issue/U4-4798, http://issues.umbraco.org/issue/U4-7833
        /// </remarks>
        static void ContentTypeCacheRefresherCacheUpdated(ContentTypeCacheRefresher sender, CacheRefresherEventArgs e)
        {
            if (Suspendable.ExamineEvents.CanIndex == false)
            {
                return;
            }

            var indexersToUpdated = ExamineManager.Instance.IndexProviderCollection.OfType <UmbracoContentIndexer>();

            foreach (var provider in indexersToUpdated)
            {
                provider.RefreshIndexerDataFromDataService();
            }

            if (e.MessageType == MessageType.RefreshByJson)
            {
                var contentTypesChanged = new HashSet <string>();
                var mediaTypesChanged   = new HashSet <string>();
                var memberTypesChanged  = new HashSet <string>();

                var payloads = ContentTypeCacheRefresher.DeserializeFromJsonPayload(e.MessageObject.ToString());
                foreach (var payload in payloads)
                {
                    if (payload.IsNew == false &&
                        (payload.WasDeleted || payload.AliasChanged || payload.PropertyRemoved || payload.PropertyTypeAliasChanged))
                    {
                        //if we get here it means that some aliases have changed and the indexes for those particular doc types will need to be updated
                        if (payload.Type == typeof(IContentType).Name)
                        {
                            //if it is content
                            contentTypesChanged.Add(payload.Alias);
                        }
                        else if (payload.Type == typeof(IMediaType).Name)
                        {
                            //if it is media
                            mediaTypesChanged.Add(payload.Alias);
                        }
                        else if (payload.Type == typeof(IMemberType).Name)
                        {
                            //if it is members
                            memberTypesChanged.Add(payload.Alias);
                        }
                    }
                }

                //TODO: We need to update Examine to support re-indexing multiple items at once instead of one by one which will speed up
                // the re-indexing process, we don't want to revert to rebuilding the whole thing!

                if (contentTypesChanged.Count > 0)
                {
                    foreach (var alias in contentTypesChanged)
                    {
                        var ctType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(alias);
                        if (ctType != null)
                        {
                            var contentItems = ApplicationContext.Current.Services.ContentService.GetContentOfContentType(ctType.Id);
                            foreach (var contentItem in contentItems)
                            {
                                ReIndexForContent(contentItem, contentItem.HasPublishedVersion && contentItem.Trashed == false);
                            }
                        }
                    }
                }
                if (mediaTypesChanged.Count > 0)
                {
                    foreach (var alias in mediaTypesChanged)
                    {
                        var ctType = ApplicationContext.Current.Services.ContentTypeService.GetMediaType(alias);
                        if (ctType != null)
                        {
                            var mediaItems = ApplicationContext.Current.Services.MediaService.GetMediaOfMediaType(ctType.Id);
                            foreach (var mediaItem in mediaItems)
                            {
                                ReIndexForMedia(mediaItem, mediaItem.Trashed == false);
                            }
                        }
                    }
                }
                if (memberTypesChanged.Count > 0)
                {
                    foreach (var alias in memberTypesChanged)
                    {
                        var ctType = ApplicationContext.Current.Services.MemberTypeService.Get(alias);
                        if (ctType != null)
                        {
                            var memberItems = ApplicationContext.Current.Services.MemberService.GetMembersByMemberType(ctType.Id);
                            foreach (var memberItem in memberItems)
                            {
                                ReIndexForMember(memberItem);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Handles index management for all unpublished content events - basically handling saving/copying/deleting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// This will execute on all servers taking part in load balancing
        /// </remarks>
        static void UnpublishedPageCacheRefresherCacheUpdated(UnpublishedPageCacheRefresher sender, CacheRefresherEventArgs e)
        {
            if (Suspendable.ExamineEvents.CanIndex == false)
            {
                return;
            }

            switch (e.MessageType)
            {
            case MessageType.RefreshById:
                var c1 = ApplicationContext.Current.Services.ContentService.GetById((int)e.MessageObject);
                if (c1 != null)
                {
                    ReIndexForContent(c1, false);
                }
                break;

            case MessageType.RemoveById:

                // This is triggered when the item is permanently deleted

                DeleteIndexForEntity((int)e.MessageObject, false);
                break;

            case MessageType.RefreshByInstance:
                var c3 = e.MessageObject as IContent;
                if (c3 != null)
                {
                    ReIndexForContent(c3, false);
                }
                break;

            case MessageType.RemoveByInstance:

                // This is triggered when the item is permanently deleted

                var c4 = e.MessageObject as IContent;
                if (c4 != null)
                {
                    DeleteIndexForEntity(c4.Id, false);
                }
                break;

            case MessageType.RefreshByJson:

                var jsonPayloads = UnpublishedPageCacheRefresher.DeserializeFromJsonPayload((string)e.MessageObject);
                if (jsonPayloads.Any())
                {
                    foreach (var payload in jsonPayloads)
                    {
                        switch (payload.Operation)
                        {
                        case UnpublishedPageCacheRefresher.OperationType.Deleted:

                            //permanently remove from all indexes

                            DeleteIndexForEntity(payload.Id, false);

                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }

                break;

            case MessageType.RefreshAll:
            default:
                //We don't support these, these message types will not fire for unpublished content
                break;
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Handles index management for all published content events - basically handling published/unpublished
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// This will execute on all servers taking part in load balancing
        /// </remarks>
        static void PublishedPageCacheRefresherCacheUpdated(PageCacheRefresher sender, CacheRefresherEventArgs e)
        {
            if (Suspendable.ExamineEvents.CanIndex == false)
            {
                return;
            }

            switch (e.MessageType)
            {
            case MessageType.RefreshById:
                var c1 = ApplicationContext.Current.Services.ContentService.GetById((int)e.MessageObject);
                if (c1 != null)
                {
                    ReIndexForContent(c1, true);
                }
                break;

            case MessageType.RemoveById:

                //This is triggered when the item has been unpublished or trashed (which also performs an unpublish).

                var c2 = ApplicationContext.Current.Services.ContentService.GetById((int)e.MessageObject);
                if (c2 != null)
                {
                    // So we need to delete the index from all indexes not supporting unpublished content.

                    DeleteIndexForEntity(c2.Id, true);

                    // We then need to re-index this item for all indexes supporting unpublished content

                    ReIndexForContent(c2, false);
                }
                break;

            case MessageType.RefreshByInstance:
                var c3 = e.MessageObject as IContent;
                if (c3 != null)
                {
                    ReIndexForContent(c3, true);
                }
                break;

            case MessageType.RemoveByInstance:

                //This is triggered when the item has been unpublished or trashed (which also performs an unpublish).

                var c4 = e.MessageObject as IContent;
                if (c4 != null)
                {
                    // So we need to delete the index from all indexes not supporting unpublished content.

                    DeleteIndexForEntity(c4.Id, true);

                    // We then need to re-index this item for all indexes supporting unpublished content

                    ReIndexForContent(c4, false);
                }
                break;

            case MessageType.RefreshAll:
            case MessageType.RefreshByJson:
            default:
                //We don't support these for examine indexing
                break;
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Handles index management for all media events - basically handling saving/copying/trashing/deleting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void MediaCacheRefresherCacheUpdated(MediaCacheRefresher sender, CacheRefresherEventArgs e)
        {
            if (Suspendable.ExamineEvents.CanIndex == false)
            {
                return;
            }

            switch (e.MessageType)
            {
            case MessageType.RefreshById:
                var c1 = ApplicationContext.Current.Services.MediaService.GetById((int)e.MessageObject);
                if (c1 != null)
                {
                    ReIndexForMedia(c1, c1.Trashed == false);
                }
                break;

            case MessageType.RemoveById:
                var c2 = ApplicationContext.Current.Services.MediaService.GetById((int)e.MessageObject);
                if (c2 != null)
                {
                    //This is triggered when the item has trashed.
                    // So we need to delete the index from all indexes not supporting unpublished content.

                    DeleteIndexForEntity(c2.Id, true);

                    //We then need to re-index this item for all indexes supporting unpublished content

                    ReIndexForMedia(c2, false);
                }
                break;

            case MessageType.RefreshByJson:

                var jsonPayloads = MediaCacheRefresher.DeserializeFromJsonPayload((string)e.MessageObject);
                if (jsonPayloads.Any())
                {
                    foreach (var payload in jsonPayloads)
                    {
                        switch (payload.Operation)
                        {
                        case MediaCacheRefresher.OperationType.Saved:
                            var media1 = ApplicationContext.Current.Services.MediaService.GetById(payload.Id);
                            if (media1 != null)
                            {
                                ReIndexForMedia(media1, media1.Trashed == false);
                            }
                            break;

                        case MediaCacheRefresher.OperationType.Trashed:

                            //keep if trashed for indexes supporting unpublished
                            //(delete the index from all indexes not supporting unpublished content)

                            DeleteIndexForEntity(payload.Id, true);

                            //We then need to re-index this item for all indexes supporting unpublished content
                            var media2 = ApplicationContext.Current.Services.MediaService.GetById(payload.Id);
                            if (media2 != null)
                            {
                                ReIndexForMedia(media2, false);
                            }

                            break;

                        case MediaCacheRefresher.OperationType.Deleted:

                            //permanently remove from all indexes

                            DeleteIndexForEntity(payload.Id, false);

                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }

                break;

            case MessageType.RefreshByInstance:
            case MessageType.RemoveByInstance:
            case MessageType.RefreshAll:
            default:
                //We don't support these, these message types will not fire for media
                break;
            }
        }
Ejemplo n.º 26
0
        private void ContentCacheUpdated(ContentCacheRefresher sender, CacheRefresherEventArgs e)
        {
            if (!IsConfigured())
            {
                return;
            }

            var jsonPayloads = e.MessageObject as ContentCacheRefresher.JsonPayload[];

            if (jsonPayloads == null || !jsonPayloads.Any())
            {
                return;
            }

            var jobs = new List <EnterspeedJob>();

            using (var context = _umbracoContextFactory.EnsureUmbracoContext())
            {
                var umb = context.UmbracoContext;
                foreach (var payload in jsonPayloads)
                {
                    var node      = umb.Content.GetById(payload.Id);
                    var savedNode = umb.Content.GetById(true, payload.Id);
                    if (node == null || savedNode == null)
                    {
                        continue;
                    }

                    var cultures = node.ContentType.VariesByCulture()
                        ? node.Cultures.Keys
                        : new List <string> {
                        GetDefaultCulture(context)
                    };

                    List <IPublishedContent> descendants = null;

                    foreach (var culture in cultures)
                    {
                        var publishedUpdateDate = node.CultureDate(culture);
                        var savedUpdateDate     = savedNode.CultureDate(culture);

                        if (savedUpdateDate > publishedUpdateDate)
                        {
                            // This means that the nodes was only saved, so we skip creating any jobs for this node and culture
                            continue;
                        }

                        var now = DateTime.UtcNow;
                        jobs.Add(new EnterspeedJob
                        {
                            ContentId = node.Id,
                            Culture   = culture,
                            JobType   = EnterspeedJobType.Publish,
                            State     = EnterspeedJobState.Pending,
                            CreatedAt = now,
                            UpdatedAt = now,
                        });

                        if (payload.ChangeTypes == TreeChangeTypes.RefreshBranch)
                        {
                            if (descendants == null)
                            {
                                descendants = node.Descendants("*").ToList();
                            }

                            foreach (var descendant in descendants)
                            {
                                var descendantCultures = descendant.ContentType.VariesByCulture()
                                    ? descendant.Cultures.Keys
                                    : new List <string> {
                                    GetDefaultCulture(context)
                                };

                                foreach (var descendantCulture in descendantCultures)
                                {
                                    jobs.Add(new EnterspeedJob
                                    {
                                        ContentId = descendant.Id,
                                        Culture   = descendantCulture,
                                        JobType   = EnterspeedJobType.Publish,
                                        State     = EnterspeedJobState.Pending,
                                        CreatedAt = now,
                                        UpdatedAt = now,
                                    });
                                }
                            }
                        }
                    }
                }
            }

            EnqueueJobs(jobs);
        }
Ejemplo n.º 27
0
 /// <summary>
 /// The media cache refresher cache updated event handler.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="CacheRefresherEventArgs"/> containing information about the event.</param>
 private void MemberCacheRefresherCacheUpdated(MemberCacheRefresher sender, CacheRefresherEventArgs e)
 {
     this.ClearCache();
 }
Ejemplo n.º 28
0
        /// <summary>
        /// When the page/content cache is refreshed, we'll check if any articulate root nodes were included in the refresh, if so we'll set a flag
        /// on the current request to rebuild the routes at the end of the request
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// This will also work for load balanced scenarios since this event executes on all servers
        /// </remarks>
        private void ContentCacheRefresher_CacheUpdated(ContentCacheRefresher sender, CacheRefresherEventArgs e)
        {
            switch (e.MessageType)
            {
            case MessageType.RefreshByPayload:
                //This is the standard case for content cache refresher
                foreach (var payload in (ContentCacheRefresher.JsonPayload[])e.MessageObject)
                {
                    if (payload.ChangeTypes.HasTypesAny(TreeChangeTypes.Remove | TreeChangeTypes.RefreshBranch | TreeChangeTypes.RefreshNode))
                    {
                        RefreshById(payload.Id, payload.ChangeTypes);
                    }
                }
                break;

            case MessageType.RefreshById:
            case MessageType.RemoveById:
                RefreshById((int)e.MessageObject, TreeChangeTypes.Remove);
                break;

            case MessageType.RefreshByInstance:
            case MessageType.RemoveByInstance:
                var content = e.MessageObject as IContent;
                if (content == null)
                {
                    return;
                }

                if (content.ContentType.Alias.InvariantEquals(ArticulateContentTypeAlias))
                {
                    //ensure routes are rebuilt
                    _appCaches.RequestCache.GetCacheItem(RefreshRoutesToken, () => true);
                }
                break;
            }
        }
Ejemplo n.º 29
0
 private void DomainCacheRefresher_CacheUpdated(DomainCacheRefresher sender, CacheRefresherEventArgs e)
 {
     //ensure routes are rebuilt
     _appCaches.RequestCache.GetCacheItem(RefreshRoutesToken, () => true);
 }
Ejemplo n.º 30
0
        private void SyncZeroValuePaymentProviderContinueUrl(ContentCacheRefresher sender, CacheRefresherEventArgs e)
        {
            var payloads = e.MessageObject as ContentCacheRefresher.JsonPayload[];

            if (payloads == null)
            {
                return;
            }

            using (var umbNew = _umbracoContextFactory.EnsureUmbracoContext())
            {
                foreach (var payload in payloads)
                {
                    if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshNode))
                    {
                        // Single node refresh

                        var node = umbNew.UmbracoContext.Content.GetById(payload.Id);
                        if (node != null && IsConfirmationPageType(node))
                        {
                            SyncZeroValuePaymentProviderContinueUrl(node);
                        }
                    }
                    else if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshBranch))
                    {
                        // Branch refresh

                        var rootNode = umbNew.UmbracoContext.Content.GetById(payload.Id);
                        if (rootNode != null)
                        {
                            var nodeType = umbNew.UmbracoContext.Content.GetContentType(VendrCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage);
                            if (nodeType == null)
                            {
                                continue;
                            }

                            var nodes = umbNew.UmbracoContext.Content.GetByContentType(nodeType);

                            foreach (var node in nodes?.Where(x => IsConfirmationPageType(x) && x.Path.StartsWith(rootNode.Path)))
                            {
                                SyncZeroValuePaymentProviderContinueUrl(node);
                            }
                        }
                    }
                    else if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshAll))
                    {
                        // All refresh

                        var nodeType = umbNew.UmbracoContext.Content.GetContentType(VendrCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage);
                        if (nodeType == null)
                        {
                            continue;
                        }

                        var nodes = umbNew.UmbracoContext.Content.GetByContentType(nodeType);

                        foreach (var node in nodes?.Where(x => IsConfirmationPageType(x)))
                        {
                            SyncZeroValuePaymentProviderContinueUrl(node);
                        }
                    }
                }
            }
        }