{"action":"created","comment":{"url":"https://api.github.com/repos/aspnet/Mvc/pulls/comments/53733363","id":53733363,"diff_hunk":"@@ -0,0 +1,219 @@\n+﻿// Copyright (c) .NET Foundation. All rights reserved.\n+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\n+\n+using System;\n+using System.IO;\n+using System.Text;\n+using System.Text.Encodings.Web;\n+using System.Threading.Tasks;\n+using Microsoft.AspNetCore.Html;\n+using Microsoft.Extensions.Caching.Distributed;\n+using Microsoft.Extensions.Caching.Memory;\n+using Microsoft.Extensions.DependencyInjection;\n+\n+namespace Microsoft.AspNetCore.Mvc.TagHelpers.Cache\n+{\n+    /// <summary>\n+    /// This implementation of <see cref=\"IHtmlFragmentCache\"/> is able to switch from \n+    /// <see cref=\"IMemoryCache\"/> to <see cref=\"IDistributedCache\"/> registered\n+    /// services using the <code>options-distributed</code> attribute.\n+    /// </summary>\n+    public class HybridHtmlFragmentCache : IHtmlFragmentCache\n+    {\n+        private const string PriorityOptionName = \"priority\";\n+        private const string DistributedOptionName = \"distributed\";\n+\n+        private readonly IMemoryCache _memoryCache;\n+        private readonly IServiceProvider _serviceProvider;\n+\n+        public HybridHtmlFragmentCache(IMemoryCache memoryCache, IServiceProvider serviceProvider)\n+        {\n+            _serviceProvider = serviceProvider;\n+            _memoryCache = memoryCache;\n+        }\n+\n+        public Task<IHtmlContent> SetAsync(string key, Func<Task<IHtmlContent>> renderContent, HtmlFragmentCacheContext context)\n+        {\n+            IDistributedCache distributedCache;\n+\n+            // is the distributed option set?\n+            if (EnsureDistributedService(context, out distributedCache))\n+            {\n+                return SetDistributedAsync(key, renderContent, context, distributedCache);\n+            }\n+            else\n+            {\n+                return SetMemoryAsync(key, renderContent, context);\n+            }\n+        }\n+\n+        private async Task<IHtmlContent> SetMemoryAsync(string key, Func<Task<IHtmlContent>> renderContent, HtmlFragmentCacheContext context)\n+        {\n+            var options = new MemoryCacheEntryOptions();\n+\n+            if (context.ExpiresOn != null)\n+            {\n+                options.SetAbsoluteExpiration(context.ExpiresOn.Value);\n+            }\n+\n+            if (context.ExpiresAfter != null)\n+            {\n+                options.SetAbsoluteExpiration(context.ExpiresAfter.Value);\n+            }\n+\n+            if (context.ExpiresSliding != null)\n+            {\n+                options.SetSlidingExpiration(context.ExpiresSliding.Value);\n+            }\n+\n+            if (context.HasOptions && context.Options.ContainsKey(PriorityOptionName))\n+            {\n+                CacheItemPriority priority;\n+                if (Enum.TryParse(context.Options[PriorityOptionName], out priority))\n+                {\n+                    options.SetPriority(priority);\n+                }\n+            }\n+\n+            // Create an entry link scope and flow it so that any tokens related to the cache entries\n+            // created within this scope get copied to this scope.\n+            using (var link = _memoryCache.CreateLinkingScope())\n+            {\n+                var content = await renderContent();\n+\n+                options.AddEntryLink(link);\n+\n+                var stringBuilder = new StringBuilder();\n+                using (var writer = new StringWriter(stringBuilder))\n+                {\n+                    content.WriteTo(writer, context.HtmlEncoder);\n+                }\n+\n+                var result = new StringBuilderHtmlContent(stringBuilder);\n+\n+                return _memoryCache.Set(key, result, options) as IHtmlContent;\n+            }\n+        }\n+\n+        public async Task<IHtmlContent> SetDistributedAsync(string key, Func<Task<IHtmlContent>> renderContent, HtmlFragmentCacheContext context, IDistributedCache cache)\n+        {\n+            var options = new DistributedCacheEntryOptions();\n+\n+            if (context.ExpiresOn != null)\n+            {\n+                options.SetAbsoluteExpiration(context.ExpiresOn.Value);\n+            }\n+\n+            if (context.ExpiresAfter != null)\n+            {\n+                options.SetAbsoluteExpiration(context.ExpiresAfter.Value);\n+            }\n+\n+            if (context.ExpiresSliding != null)\n+            {\n+                options.SetSlidingExpiration(context.ExpiresSliding.Value);\n+            }\n+\n+            var content = await renderContent();\n+\n+            var stringBuilder = new StringBuilder();\n+            using (var writer = new StringWriter(stringBuilder))\n+            {\n+                content.WriteTo(writer, context.HtmlEncoder);\n+            }\n+\n+            var serialized = Encoding.UTF8.GetBytes(stringBuilder.ToString());\n+\n+            await cache.SetAsync(key, serialized, options);\n+\n+            return content;\n+        }\n+\n+        public Task<IHtmlContent> GetValueAsync(string key, HtmlFragmentCacheContext context)\n+        {\n+            IDistributedCache distributedCache;\n+\n+            // is the distributed option set?\n+            if (EnsureDistributedService(context, out distributedCache))\n+            {\n+                return GetDistributedValueAsync(key, distributedCache);\n+            }\n+            else\n+            {\n+                IHtmlContent value;\n+                if(GetMemoryValue(key, out value))\n+                {\n+                    return Task.FromResult(value);\n+                }\n+                else\n+                {\n+                    return Task.FromResult<IHtmlContent>(null);\n+                }\n+            }\n+        }\n+\n+        public bool GetMemoryValue(string key, out IHtmlContent value)\n+        {\n+            return _memoryCache.TryGetValue(key, out value);\n+        }\n+\n+        public async Task<IHtmlContent> GetDistributedValueAsync(string key, IDistributedCache cache)\n+        {\n+            var encoded = await cache.GetAsync(key);\n+\n+            if(encoded == null)\n+            {\n+                return null;\n+            }\n+\n+            var content = Encoding.UTF8.GetString(encoded);\n+            return new HtmlEncodedString(content);\n+        }\n+\n+        private bool EnsureDistributedService(HtmlFragmentCacheContext context, out IDistributedCache distributedCache)\n+        {\n+            bool distributed;\n+            if (context.Options.ContainsKey(DistributedOptionName)\n+                && bool.TryParse(context.Options[DistributedOptionName], out distributed))","path":"src/Microsoft.AspNetCore.Mvc.TagHelpers/Cache/HybridHtmlFragmentCache.cs","position":177,"original_position":177,"commit_id":"c6e1db1f00d94f01a36d053ce0fb03826bb7908f","original_commit_id":"c6e1db1f00d94f01a36d053ce0fb03826bb7908f","user":{"login":"pranavkm","id":174281,"avatar_url":"https://avatars.githubusercontent.com/u/174281?v=3","gravatar_id":"","url":"https://api.github.com/users/pranavkm","html_url":"https://github.com/pranavkm","followers_url":"https://api.github.com/users/pranavkm/followers","following_url":"https://api.github.com/users/pranavkm/following{/other_user}","gists_url":"https://api.github.com/users/pranavkm/gists{/gist_id}","starred_url":"https://api.github.com/users/pranavkm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pranavkm/subscriptions","organizations_url":"https://api.github.com/users/pranavkm/orgs","repos_url":"https://api.github.com/users/pranavkm/repos","events_url":"https://api.github.com/users/pranavkm/events{/privacy}","received_events_url":"https://api.github.com/users/pranavkm/received_events","type":"User","site_admin":false},"body":"`&&` on previous line.","created_at":"2016-02-23T03:47:57Z","updated_at":"2016-02-23T03:47:57Z","html_url":"https://github.com/aspnet/Mvc/pull/4120#discussion_r53733363","pull_request_url":"https://api.github.com/repos/aspnet/Mvc/pulls/4120","_links":{"self":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/comments/53733363"},"html":{"href":"https://github.com/aspnet/Mvc/pull/4120#discussion_r53733363"},"pull_request":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/4120"}}},"pull_request":{"url":"https://api.github.com/repos/aspnet/Mvc/pulls/4120","id":60028142,"html_url":"https://github.com/aspnet/Mvc/pull/4120","diff_url":"https://github.com/aspnet/Mvc/pull/4120.diff","patch_url":"https://github.com/aspnet/Mvc/pull/4120.patch","issue_url":"https://api.github.com/repos/aspnet/Mvc/issues/4120","number":4120,"state":"open","locked":false,"title":"[Fixes #3867] Abstracting Cache tag helper storage","user":{"login":"sebastienros","id":1165805,"avatar_url":"https://avatars.githubusercontent.com/u/1165805?v=3","gravatar_id":"","url":"https://api.github.com/users/sebastienros","html_url":"https://github.com/sebastienros","followers_url":"https://api.github.com/users/sebastienros/followers","following_url":"https://api.github.com/users/sebastienros/following{/other_user}","gists_url":"https://api.github.com/users/sebastienros/gists{/gist_id}","starred_url":"https://api.github.com/users/sebastienros/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sebastienros/subscriptions","organizations_url":"https://api.github.com/users/sebastienros/orgs","repos_url":"https://api.github.com/users/sebastienros/repos","events_url":"https://api.github.com/users/sebastienros/events{/privacy}","received_events_url":"https://api.github.com/users/sebastienros/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-02-20T00:34:34Z","updated_at":"2016-02-23T03:47:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ed3acdd1f9448eed37417188c936888bafb374ba","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/aspnet/Mvc/pulls/4120/commits","review_comments_url":"https://api.github.com/repos/aspnet/Mvc/pulls/4120/comments","review_comment_url":"https://api.github.com/repos/aspnet/Mvc/pulls/comments{/number}","comments_url":"https://api.github.com/repos/aspnet/Mvc/issues/4120/comments","statuses_url":"https://api.github.com/repos/aspnet/Mvc/statuses/c6e1db1f00d94f01a36d053ce0fb03826bb7908f","head":{"label":"aspnet:sebros/cachetaghelper","ref":"sebros/cachetaghelper","sha":"c6e1db1f00d94f01a36d053ce0fb03826bb7908f","user":{"login":"aspnet","id":6476660,"avatar_url":"https://avatars.githubusercontent.com/u/6476660?v=3","gravatar_id":"","url":"https://api.github.com/users/aspnet","html_url":"https://github.com/aspnet","followers_url":"https://api.github.com/users/aspnet/followers","following_url":"https://api.github.com/users/aspnet/following{/other_user}","gists_url":"https://api.github.com/users/aspnet/gists{/gist_id}","starred_url":"https://api.github.com/users/aspnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspnet/subscriptions","organizations_url":"https://api.github.com/users/aspnet/orgs","repos_url":"https://api.github.com/users/aspnet/repos","events_url":"https://api.github.com/users/aspnet/events{/privacy}","received_events_url":"https://api.github.com/users/aspnet/received_events","type":"Organization","site_admin":false},"repo":{"id":15126429,"name":"Mvc","full_name":"aspnet/Mvc","owner":{"login":"aspnet","id":6476660,"avatar_url":"https://avatars.githubusercontent.com/u/6476660?v=3","gravatar_id":"","url":"https://api.github.com/users/aspnet","html_url":"https://github.com/aspnet","followers_url":"https://api.github.com/users/aspnet/followers","following_url":"https://api.github.com/users/aspnet/following{/other_user}","gists_url":"https://api.github.com/users/aspnet/gists{/gist_id}","starred_url":"https://api.github.com/users/aspnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspnet/subscriptions","organizations_url":"https://api.github.com/users/aspnet/orgs","repos_url":"https://api.github.com/users/aspnet/repos","events_url":"https://api.github.com/users/aspnet/events{/privacy}","received_events_url":"https://api.github.com/users/aspnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/aspnet/Mvc","description":"Model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor.","fork":false,"url":"https://api.github.com/repos/aspnet/Mvc","forks_url":"https://api.github.com/repos/aspnet/Mvc/forks","keys_url":"https://api.github.com/repos/aspnet/Mvc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aspnet/Mvc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aspnet/Mvc/teams","hooks_url":"https://api.github.com/repos/aspnet/Mvc/hooks","issue_events_url":"https://api.github.com/repos/aspnet/Mvc/issues/events{/number}","events_url":"https://api.github.com/repos/aspnet/Mvc/events","assignees_url":"https://api.github.com/repos/aspnet/Mvc/assignees{/user}","branches_url":"https://api.github.com/repos/aspnet/Mvc/branches{/branch}","tags_url":"https://api.github.com/repos/aspnet/Mvc/tags","blobs_url":"https://api.github.com/repos/aspnet/Mvc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aspnet/Mvc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aspnet/Mvc/git/refs{/sha}","trees_url":"https://api.github.com/repos/aspnet/Mvc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aspnet/Mvc/statuses/{sha}","languages_url":"https://api.github.com/repos/aspnet/Mvc/languages","stargazers_url":"https://api.github.com/repos/aspnet/Mvc/stargazers","contributors_url":"https://api.github.com/repos/aspnet/Mvc/contributors","subscribers_url":"https://api.github.com/repos/aspnet/Mvc/subscribers","subscription_url":"https://api.github.com/repos/aspnet/Mvc/subscription","commits_url":"https://api.github.com/repos/aspnet/Mvc/commits{/sha}","git_commits_url":"https://api.github.com/repos/aspnet/Mvc/git/commits{/sha}","comments_url":"https://api.github.com/repos/aspnet/Mvc/comments{/number}","issue_comment_url":"https://api.github.com/repos/aspnet/Mvc/issues/comments{/number}","contents_url":"https://api.github.com/repos/aspnet/Mvc/contents/{+path}","compare_url":"https://api.github.com/repos/aspnet/Mvc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aspnet/Mvc/merges","archive_url":"https://api.github.com/repos/aspnet/Mvc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aspnet/Mvc/downloads","issues_url":"https://api.github.com/repos/aspnet/Mvc/issues{/number}","pulls_url":"https://api.github.com/repos/aspnet/Mvc/pulls{/number}","milestones_url":"https://api.github.com/repos/aspnet/Mvc/milestones{/number}","notifications_url":"https://api.github.com/repos/aspnet/Mvc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aspnet/Mvc/labels{/name}","releases_url":"https://api.github.com/repos/aspnet/Mvc/releases{/id}","deployments_url":"https://api.github.com/repos/aspnet/Mvc/deployments","created_at":"2013-12-12T03:54:00Z","updated_at":"2016-02-23T03:46:07Z","pushed_at":"2016-02-23T01:52:40Z","git_url":"git://github.com/aspnet/Mvc.git","ssh_url":"git@github.com:aspnet/Mvc.git","clone_url":"https://github.com/aspnet/Mvc.git","svn_url":"https://github.com/aspnet/Mvc","homepage":"","size":28824,"stargazers_count":2760,"watchers_count":2760,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1092,"mirror_url":null,"open_issues_count":269,"forks":1092,"open_issues":269,"watchers":2760,"default_branch":"dev"}},"base":{"label":"aspnet:dev","ref":"dev","sha":"b39f4dfe298bf255a0c5fef3b7d749d0cffd861b","user":{"login":"aspnet","id":6476660,"avatar_url":"https://avatars.githubusercontent.com/u/6476660?v=3","gravatar_id":"","url":"https://api.github.com/users/aspnet","html_url":"https://github.com/aspnet","followers_url":"https://api.github.com/users/aspnet/followers","following_url":"https://api.github.com/users/aspnet/following{/other_user}","gists_url":"https://api.github.com/users/aspnet/gists{/gist_id}","starred_url":"https://api.github.com/users/aspnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspnet/subscriptions","organizations_url":"https://api.github.com/users/aspnet/orgs","repos_url":"https://api.github.com/users/aspnet/repos","events_url":"https://api.github.com/users/aspnet/events{/privacy}","received_events_url":"https://api.github.com/users/aspnet/received_events","type":"Organization","site_admin":false},"repo":{"id":15126429,"name":"Mvc","full_name":"aspnet/Mvc","owner":{"login":"aspnet","id":6476660,"avatar_url":"https://avatars.githubusercontent.com/u/6476660?v=3","gravatar_id":"","url":"https://api.github.com/users/aspnet","html_url":"https://github.com/aspnet","followers_url":"https://api.github.com/users/aspnet/followers","following_url":"https://api.github.com/users/aspnet/following{/other_user}","gists_url":"https://api.github.com/users/aspnet/gists{/gist_id}","starred_url":"https://api.github.com/users/aspnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspnet/subscriptions","organizations_url":"https://api.github.com/users/aspnet/orgs","repos_url":"https://api.github.com/users/aspnet/repos","events_url":"https://api.github.com/users/aspnet/events{/privacy}","received_events_url":"https://api.github.com/users/aspnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/aspnet/Mvc","description":"Model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor.","fork":false,"url":"https://api.github.com/repos/aspnet/Mvc","forks_url":"https://api.github.com/repos/aspnet/Mvc/forks","keys_url":"https://api.github.com/repos/aspnet/Mvc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aspnet/Mvc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aspnet/Mvc/teams","hooks_url":"https://api.github.com/repos/aspnet/Mvc/hooks","issue_events_url":"https://api.github.com/repos/aspnet/Mvc/issues/events{/number}","events_url":"https://api.github.com/repos/aspnet/Mvc/events","assignees_url":"https://api.github.com/repos/aspnet/Mvc/assignees{/user}","branches_url":"https://api.github.com/repos/aspnet/Mvc/branches{/branch}","tags_url":"https://api.github.com/repos/aspnet/Mvc/tags","blobs_url":"https://api.github.com/repos/aspnet/Mvc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aspnet/Mvc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aspnet/Mvc/git/refs{/sha}","trees_url":"https://api.github.com/repos/aspnet/Mvc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aspnet/Mvc/statuses/{sha}","languages_url":"https://api.github.com/repos/aspnet/Mvc/languages","stargazers_url":"https://api.github.com/repos/aspnet/Mvc/stargazers","contributors_url":"https://api.github.com/repos/aspnet/Mvc/contributors","subscribers_url":"https://api.github.com/repos/aspnet/Mvc/subscribers","subscription_url":"https://api.github.com/repos/aspnet/Mvc/subscription","commits_url":"https://api.github.com/repos/aspnet/Mvc/commits{/sha}","git_commits_url":"https://api.github.com/repos/aspnet/Mvc/git/commits{/sha}","comments_url":"https://api.github.com/repos/aspnet/Mvc/comments{/number}","issue_comment_url":"https://api.github.com/repos/aspnet/Mvc/issues/comments{/number}","contents_url":"https://api.github.com/repos/aspnet/Mvc/contents/{+path}","compare_url":"https://api.github.com/repos/aspnet/Mvc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aspnet/Mvc/merges","archive_url":"https://api.github.com/repos/aspnet/Mvc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aspnet/Mvc/downloads","issues_url":"https://api.github.com/repos/aspnet/Mvc/issues{/number}","pulls_url":"https://api.github.com/repos/aspnet/Mvc/pulls{/number}","milestones_url":"https://api.github.com/repos/aspnet/Mvc/milestones{/number}","notifications_url":"https://api.github.com/repos/aspnet/Mvc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aspnet/Mvc/labels{/name}","releases_url":"https://api.github.com/repos/aspnet/Mvc/releases{/id}","deployments_url":"https://api.github.com/repos/aspnet/Mvc/deployments","created_at":"2013-12-12T03:54:00Z","updated_at":"2016-02-23T03:46:07Z","pushed_at":"2016-02-23T01:52:40Z","git_url":"git://github.com/aspnet/Mvc.git","ssh_url":"git@github.com:aspnet/Mvc.git","clone_url":"https://github.com/aspnet/Mvc.git","svn_url":"https://github.com/aspnet/Mvc","homepage":"","size":28824,"stargazers_count":2760,"watchers_count":2760,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1092,"mirror_url":null,"open_issues_count":269,"forks":1092,"open_issues":269,"watchers":2760,"default_branch":"dev"}},"_links":{"self":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/4120"},"html":{"href":"https://github.com/aspnet/Mvc/pull/4120"},"issue":{"href":"https://api.github.com/repos/aspnet/Mvc/issues/4120"},"comments":{"href":"https://api.github.com/repos/aspnet/Mvc/issues/4120/comments"},"review_comments":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/4120/comments"},"review_comment":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/4120/commits"},"statuses":{"href":"https://api.github.com/repos/aspnet/Mvc/statuses/c6e1db1f00d94f01a36d053ce0fb03826bb7908f"}}}}