private void PurgeCloudflareCache(IContent content) { var urls = new List <string>(); try { //Check to see if the page has cache purging on publish disabled. if (content.HasProperty("cloudflareDisabledOnPublish") && content.GetValue <bool>("cloudflareDisabledOnPublish")) { //it was disabled so just continue; return; } } catch (Exception ex) { //ignore _logger.Error <ContentPublishedComponent>(ex); } urls.AddRange(_domainManager.GetUrlsForNode(content.Id, false)); var results = _cloudflareManager.PurgePages(urls); if (results.Any() && results.Where(x => !x.Success).Any()) { string errorMessage = "We could not purge the Cloudflare cache. \n \n" + _cloudflareManager.PrintResultsSummary(results); _logger.Error <ContentPublishedComponent>(errorMessage + " " + string.Join(",", urls)); } else { _logger.Debug <ContentPublishedComponent>($"purged urls {string.Join(",",urls)}"); } }
/// <summary> /// Gets the urls of every content item in the content section and caches the results. Is there a faster way to do this? /// </summary> /// <returns></returns> private IEnumerable <string> GetAllContentUrls() { if (this._contentIdToUrlCache != null && this._contentIdToUrlCache.Any()) { //just return the cache return(this._contentIdToUrlCache.SelectMany(x => x.Value)); } Dictionary <int, IEnumerable <string> > cache = new Dictionary <int, IEnumerable <string> >(); List <string> urls = new List <string>(); //Id like to use UmbracoContext.Current.ContentCache.GetByRoute() somehow but you cant always guarantee that urls //will be in hierarchical order because of rewriteing, etc. IEnumerable <IPublishedContent> roots = Enumerable.Empty <IPublishedContent>(); using (var contextReference = umbracoContextFactory.EnsureUmbracoContext()) { contextReference.UmbracoContext.Content.GetAtRoot(); } foreach (IPublishedContent content in roots) { IEnumerable <string> contentUrls = domainManager.GetUrlsForNode(content.Id, false); cache.Add(content.Id, contentUrls); urls.AddRange(contentUrls); foreach (IPublishedContent childContent in content.Descendants()) { IEnumerable <string> childContentUrls = domainManager.GetUrlsForNode(childContent.Id, false); cache.Add(childContent.Id, childContentUrls); urls.AddRange(childContentUrls); } } this._contentIdToUrlCache = cache; //Add to the cache HttpRuntime.Cache.Insert(CACHE_KEY, cache, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); return(urls); }