/// <summary>Begins a new cache section.</summary> /// <param name="webGreaseSectionKey">The web Grease Section Key.</param> /// <param name="autoLoad">The auto Load.</param> /// <returns>The <see cref="ICacheSection"/>.</returns> public ICacheSection BeginSection(WebGreaseSectionKey webGreaseSectionKey, bool autoLoad = true) { return(this.currentCacheSection = CacheSection.Begin( this.context, webGreaseSectionKey.Category, webGreaseSectionKey.Value, this.CurrentCacheSection, autoLoad)); }
/// <summary> /// Executes the action for the cached section /// the boolean/success result of the action will determine if the action will be stored in cache. /// </summary> /// <param name="cachableSectionAction">The section action.</param> /// <returns>If all was successfull (being passed from within the actions).</returns> public bool Execute(Func <ICacheSection, bool> cachableSectionAction) { var id = WebGreaseContext.ToStringId(this.idParts); var webGreaseSectionKey = new WebGreaseSectionKey(this.context, id, this.cacheVarByContentItem, this.cacheVarBySetting, this.cacheVarByFileSet); var sectionLock = SectionLocks.GetOrAdd(webGreaseSectionKey.Value, new object()); return(Safe.Lock( sectionLock, this.cacheInfiniteWaitForLock ? Safe.MaxLockTimeout : Safe.DefaultLockTimeout, () => { var errorHasOccurred = false; EventHandler logOnErrorOccurred = delegate { errorHasOccurred = true; }; this.context.Log.ErrorOccurred += logOnErrorOccurred; var cacheSection = this.context.Cache.BeginSection(webGreaseSectionKey); try { if (this.context.TemporaryIgnore(this.cacheVarByFileSet, this.cacheVarByContentItem) && !errorHasOccurred) { cacheSection.Save(); return true; } cacheSection.Load(); if (this.cacheIsSkipable && cacheSection.CanBeSkipped()) { if (this.whenSkippedAction != null) { this.whenSkippedAction(cacheSection); } if (!errorHasOccurred) { return true; } } if (this.restoreFromCacheAction != null && cacheSection.CanBeRestoredFromCache()) { if (this.restoreFromCacheAction(cacheSection) && !errorHasOccurred) { return true; } } this.context.Measure.Start(this.isGroup, this.idParts); try { if (!cachableSectionAction(cacheSection) || errorHasOccurred) { return false; } cacheSection.Save(); return true; } finally { this.context.Measure.End(this.isGroup, this.idParts); } } finally { this.context.Log.ErrorOccurred -= logOnErrorOccurred; cacheSection.EndSection(); } })); }
/// <summary>Begins a new cache section.</summary> /// <param name="webGreaseSectionKey">The web Grease Section Key.</param> /// <param name="autoLoad">The auto Load.</param> /// <returns>The <see cref="ICacheSection"/>.</returns> public ICacheSection BeginSection(WebGreaseSectionKey webGreaseSectionKey, bool autoLoad = true) { return(EmptyCacheSection); }