Хранить перечислимый или общий список в MemoryCache - PullRequest
0 голосов
/ 29 сентября 2018

Как мне сохранить Enumerable или List в MemoryCache?Я только начал программировать и пытался понять из статьи здесь:

Было бы это?

IMemoryCache _cache;
var testdata = productcategoryrepository.GetAllProductCategory();
_cache.Set("Teststore", testdata);

Кэширование памяти

Мой код:

public IEnumerable<ProductCategory> GetAllProductCategory()
{
    return _context.ProductCategory.ToList();
}

public partial class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ImageLocation { get; set; }

    public int? ProductCategoryId { get; set; }
    public virtual ProductCategory ProductCategory { get; set; }
}

1 Ответ

0 голосов
/ 29 сентября 2018

Это похоже на работу:

public class HomeController : Controller
{
    private readonly IMemoryCache _cache;

    public HomeController(IMemoryCache cache)
    {
        _cache = cache;
    }
    public IActionResult Index()
    {
        var testdata = productcategoryrepository.GetAllProductCategory();
        _cache.Set("Teststore", testdata);

        return View();
    }
}
...