【问题标题】:how use memorycache in this method?在这种方法中如何使用内存缓存?
【发布时间】:2022-06-17 23:06:55
【问题描述】:

如何在这种方法中添加内存缓存? 这是我的代码的一部分,我想在上面设置内存缓存。

 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));
        }

【问题讨论】:

  • 为什么你不能像这里的文档中描述的那样添加内存缓存:docs.microsoft.com/en-us/aspnet/core/performance/caching/…
  • 您能告诉我您要缓存哪些数据吗?客户端返回的数据还是其他?我们首先需要知道您为什么要存储哪些数据以及要存储哪些数据,然后我们才能向您推荐正确的缓存方法。

标签: asp.net-core memorycache


【解决方案1】:
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));
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    相关资源
    最近更新 更多