コード例 #1
0
        internal string BuildCacheKey(string prefix)
        {
            var segments = new List <object> {
                prefix
            };

            if (VaryBy == null)
            {
                return(string.Join("_", segments));
            }

            segments.AddRange(VaryBy.Flatten());

            return(string.Join("_", segments));
        }
コード例 #2
0
        /// <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);
        }