Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Core - Using responseCache with ViewComponent

here's my ViewComponent:

public class Categories: ViewComponent
{
    private readonly ICategoryService _categories;

    public Categories(ICategoryService categories)
    {
        _categories = categories;
    }      


    public IViewComponentResult Invoke()
    {
        var cat = _categories.GetCategories();
        return View(viewName: "Default", model: cat);
    }        
}

and here's how I use it in my view:

@await Component.InvokeAsync("Categories")

I tried use caching by putting:

[ResponseCache(Duration = 99999999)]

above invoke method but didn't work.

Is it possible to use caching with view components? and how?

like image 961
farhang67 Avatar asked Jun 29 '17 22:06

farhang67


1 Answers

You can cache View Components by wrapping them in a cache tag helper, like this:

<cache expires-after="@TimeSpan.FromMinutes(5)">
    @await Component.InvokeAsync("Categories")
</cache>
like image 131
Will Ray Avatar answered Sep 17 '22 18:09

Will Ray