예제 #1
0
        private void MergeCacheContexts(CacheContext into, CacheContext from)
        {
            into.AddContext(from.Contexts.ToArray());
            into.AddTag(from.Tags.ToArray());

            var offset = GetMostRestrictiveDateTimeOffset(into.ExpiresOn, from.ExpiresOn);

            if (offset.HasValue)
            {
                into.WithExpiryOn(offset.Value);
            }

            var slidingExpiration = GetMostRestrictiveTimespan(into.ExpiresSliding, from.ExpiresSliding);

            if (slidingExpiration.HasValue)
            {
                into.WithExpirySliding(slidingExpiration.Value);
            }

            var duration = GetMostRestrictiveTimespan(into.ExpiresAfter, from.ExpiresAfter);

            if (duration.HasValue)
            {
                into.WithExpiryAfter(duration.Value);
            }
        }
        /// <inheritdoc />
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            IHtmlContent content;

            if (Enabled)
            {
                var cacheContext = new CacheContext(CacheId);

                if (!String.IsNullOrEmpty(VaryBy))
                {
                    cacheContext.AddContext(VaryBy.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries));
                }

                if (!String.IsNullOrEmpty(Dependencies))
                {
                    cacheContext.AddTag(Dependencies.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries));
                }

                var hasEvictionCriteria = false;

                if (ExpiresOn.HasValue)
                {
                    hasEvictionCriteria = true;
                    cacheContext.WithExpiryOn(ExpiresOn.Value);
                }

                if (ExpiresAfter.HasValue)
                {
                    hasEvictionCriteria = true;
                    cacheContext.WithExpiryAfter(ExpiresAfter.Value);
                }

                if (ExpiresSliding.HasValue)
                {
                    hasEvictionCriteria = true;
                    cacheContext.WithExpirySliding(ExpiresSliding.Value);
                }

                if (!hasEvictionCriteria)
                {
                    cacheContext.WithExpirySliding(DefaultExpiration);
                }

                _cacheScopeManager.EnterScope(cacheContext);

                try
                {
                    content = await output.GetChildContentAsync();
                }
                finally
                {
                    _cacheScopeManager.ExitScope();
                }

                content = await ProcessContentAsync(output, cacheContext);
            }
            else
            {
                content = await output.GetChildContentAsync();
            }

            // Clear the contents of the "cache" element since we don't want to render it.
            output.SuppressOutput();

            output.Content.SetHtmlContent(content);
        }