1

how can i add memorycache in this method ? this is a section of my code that i want to set memory cache on it.

 public IActionResult Index(int pageId = 1, string filter = "",
           int startPrice = 0, int endPrice = 0, string getType = "", string orderByType = "date",
             List<int> selectedGroups = null, List<int> selectedBrand = null, List<int> selectedTags = null
            , List<int> selectedsize = null , string Discount = "")
        {

            ViewBag.selectedGroups = selectedGroups;
            ViewBag.selectedTags = selectedTags;
            ViewBag.selectedsize = selectedsize;
            ViewBag.Discount = Discount;
            ViewBag.getType = getType;
            ViewBag.Groups = _productService.GetAllGroup();
            ViewBag.Tags = _productService.GetTags().Where(c => c.ActiveRow).ToList();
            ViewBag.size = _productService.GetSizes().ToList();
            ViewBag.pageId = pageId;
            return View(_productService.GetProducttype(pageId, filter, startPrice, endPrice, getType, orderByType, selectedGroups, selectedBrand, 24, selectedTags, selectedsize, Discount));
        }
2
  • Why can't you add the memory cache as described in the docs here: learn.microsoft.com/en-us/aspnet/core/performance/caching/… ? Commented Jan 22, 2022 at 18:11
  • Could you please tell me which data you want to cache? The data which the client has returned or else? We need firstly know why and which data you want to store then we could suggest the right cache method to you. Commented Jan 24, 2022 at 6:07

1 Answer 1

0
private readonly IMemoryCache _memoryCache;

public Constructor (IMemoryCache memoryCache)
{
    _memoryCache = memoryCache;
}

public IActionResult Index(int pageId = 1, string filter = "",
       int startPrice = 0, int endPrice = 0, string getType = "", string orderByType = "date",
         List<int> selectedGroups = null, List<int> selectedBrand = null, List<int> selectedTags = null
        , List<int> selectedsize = null , string Discount = "")
    {

        ViewBag.selectedGroups = selectedGroups;
        ViewBag.selectedTags = selectedTags;
        ViewBag.selectedsize = selectedsize;
        ViewBag.Discount = Discount;
        ViewBag.getType = getType;
        
        var groups = new List<Group>();
        if (_memoryCache.TryGetValue("groups", out groups)
        {
            ViewBag.Groups = groups;
        }
        else 
        {
            groups = _productService.GetAllGroup(); 
            _memoryCache.Set("groups", groups);
            ViewBag.Groups = groups;    
        }
        
        var tags = new List<Tag>();
        if (_memoryCache.TryGetValue("tags", out tags)
        {
            ViewBag.Tags = tags;
        }
        else 
        {
            tags = _productService.GetTags().Where(c => c.ActiveRow).ToList();
            _memoryCache.Set("tags", tags);
            ViewBag.Tags = tags;
        }

        var sizes = new List<Size>();
        if (_memoryCache.TryGetValue("sizes", out sizes)
        {
            ViewBag.size = sizes;
        }
        else 
        {
            sizes = _productService.GetSizes().ToList();
            _memoryCache.Set("sizes", sizes);
            ViewBag.size = sizes;
        }           
        

        var pageId = null;
        if (_memoryCache.TryGetValue("pageId", out pageId))
        {
            ViewBag.pageId = pageId;
        }
        else 
        {
            _memoryCache.Set("pageId", pageId);
            ViewBag.pageId = pageId;
        }
                    
        return View(_productService.GetProducttype(pageId, filter, startPrice, endPrice, getType, orderByType, selectedGroups, selectedBrand, 24, selectedTags, selectedsize, Discount));
    }
Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.