Click here to Skip to main content
15,883,990 members
Articles / Web Development / ASP.NET

ASP.NET Core 2.0 MVC Distributed Cache Tag Helper

Rate me:
Please Sign up or sign in to vote.
3.19/5 (5 votes)
27 Aug 2017CPOL 5.7K   2   2
How to use Distributed Cache Tag Helper in ASP.NET Core MVC. Continue reading...

Problem

How to use Distributed Cache Tag Helper in ASP.NET Core MVC.

Solution

In an empty project, update Startup class to add services and middleware for MVC and Distributed Cache:

C#
public void ConfigureServices(
            IServiceCollection services)
        {
            services.AddDistributedRedisCache(options =>
            {
                options.Configuration = "...";
            });

            services.AddMvc();
        }

        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env)
        {
            app.UseMvcWithDefaultRoute();
        }

Add a controller:

C#
public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }

Add a _ViewImports.cshtml page in Views folder. Add directive to import tag helpers:

C#
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Add a razor page (Index.cshtml):

HTML
<p>
    Current Time: @DateTime.Now.ToString("hh:mm:ss")
</p>
<p>
    <distributed-cache name="fiver-one"
                       expires-after="@TimeSpan.FromMinutes(1)">
        Cached Time: @DateTime.Now.ToString("hh:mm:ss")
    </distributed-cache>
</p>

Run the sample, keep refreshing and you’ll notice that Cached Time doesn’t refresh (until cache expires):

Discussion

Cache and Distributed Cache Tag Helper help improve performance of your application by caching view’s content, either in-memory or in a distributed cache (e.g. Redis).

Distributed Cache Tag Helper uses IDistributedCache to store contents in a distributed cache. To learn more about distributed caching, please refer to an earlier post here.

Attributes

Cache Tag Helper has the following attributes:

Expires-…

Expiry related attributes control length of time cache is valid for. You could specify an absolute or sliding date/time.

Vary-by-…

You could invalidate the cache if HTTP header, query string, routing data, cookie value or custom values.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 2 Pin
GerVenson28-Aug-17 1:26
professionalGerVenson28-Aug-17 1:26 
Could you please add some Technical info in the "Technical blog"? What is the Technologie behind that cache
GeneralRe: My vote of 2 Pin
Tahir Naushad28-Aug-17 1:39
professionalTahir Naushad28-Aug-17 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.