MVC Net Core将MemoryCache传递给所有控制器 [英] MVC Net Core Pass MemoryCache to All Controllers

查看:126
本文介绍了MVC Net Core将MemoryCache传递给所有控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个在线商店网站.我想将ProductCategory显示为侧边栏(短列表类别,例如衣服,电子产品,家具,书籍等).

I am building a online shop website. I want to display ProductCategory as a Sidebar (short list categories, Eg Clothes, Electronics, Furniture, Books, etc).

最后,我想将productcategory _cache变量发送给所有控制器.这种方法似乎正确吗?此外,如何在Home Controller中删除不必要的memorycache参数?在计划的东西中建立一种方法来检索它?

At the end, I want to send around productcategory _cache variable to all controllers.Does this methodology seem correct? Additionally, how do I remove unnecessary memorycache parameter in Home Controller? Make a method in Scheduled stuff to retrieve it?

DI解决方案:

public class HomeController : Controller
{    
   private readonly IMemoryCache _cache;
   public IScheduledStuff _scheduledstuff;
   public HomeController(IMemoryCache cache, IScheduledStuff scheduledstuff)
   {
        _cache = cache;
        _scheduledstuff = scheduledstuff;
        _scheduledstuff.ScheduleItemsExecute();
    }
    public IActionResult Index()
    {
        ViewBag.ProductCategoryList = _cache.Get<IEnumerable<ProductCategory>>("Teststore");
        return View();
    }
}

public class ProductCategoryRepository : IProductCategoryRepository<ProductCategory>
{
    public IEnumerable<ProductCategory> GetAllProductCategory()
    {
        return _context.ProductCategory.ToList();
    }
}

public ProductCategory()
{
    public int ProductCategoryId { get; set; }
    public string ProductCategoryName { get; set; }
    public string ProductCategoryDescription { get; set; }
}

public class ScheduledStuff : IScheduledStuff
{
    private readonly DatabaseContext _context;
    IMemoryCache MemCache;
    public IProductCategoryRepository<ProductCategory> productcategoryrepository;

    public ScheduledStuff(DatabaseContext context, IMemoryCache memCache)
    {
        _context = context;
        MemCache = memCache;
        productcategoryrepository = new ProductCategoryRepository(_context);
    }

    public void ScheduleItemsExecute()
    {
        var testdata = productcategoryrepository.GetAllProductCategory();
        MemCache.Set("Teststore", testdata);
    }
}


public class Startup
{
    public IProductCategoryRepository<ProductCategory> productcategoryrepository;
    public IMemoryCache memorycache;

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMemoryCache();
        services.AddSingleton<ScheduledStuff>();

Asp.Net核心:在控制器外部使用内存缓存

静态解决方案::不断收到Null异常错误,有人可以帮助解决此问题吗?

Solution Static: Keep receiving Null exception error, can someone help fix this?

public static class MemoryCacheStatic
{
    public static IMemoryCache MemCacheStatic;
    public static IProductCategoryRepository<ProductCategory> productcategoryrepositorystatic;

    // Error on this line: <--- NullReferenceException: Object reference not set to an instance of an object.
    public static IEnumerable<ProductCategory> ProductCategoryStatic = productcategoryrepositorystatic.GetAllProductCategory();   


     public static void SetValues()
     {
        ProductCategoryStatic = productcategoryrepositorystatic.GetAllProductCategory();
        MemCacheStatic.Set("Teststore", ProductCategoryStatic) ;
     }

推荐答案

这应该有效,感谢Henk-

This should work, thanks for the help Henk-

public class HomeController : Controller
{    
   private readonly IMemoryCache _cache;
   public IScheduledStuff _scheduledstuff;
   public HomeController(IMemoryCache cache, IScheduledStuff scheduledstuff)
   {
        _cache = cache;
        _scheduledstuff = scheduledstuff;
        _scheduledstuff.ScheduleItemsExecute();
    }
    public IActionResult Index()
    {
        ViewBag.ProductCategoryList = _cache.Get<IEnumerable<ProductCategory>>("Teststore");
        return View();
    }
}

public class ProductCategoryRepository : IProductCategoryRepository<ProductCategory>
{
    public IEnumerable<ProductCategory> GetAllProductCategory()
    {
        return _context.ProductCategory.ToList();
    }
}

public ProductCategory()
{
    public int ProductCategoryId { get; set; }
    public string ProductCategoryName { get; set; }
    public string ProductCategoryDescription { get; set; }
}

public class ScheduledStuff : IScheduledStuff
{
    private readonly DatabaseContext _context;
    IMemoryCache MemCache;
    public IProductCategoryRepository<ProductCategory> productcategoryrepository;

    public ScheduledStuff(DatabaseContext context, IMemoryCache memCache)
    {
        _context = context;
        MemCache = memCache;
        productcategoryrepository = new ProductCategoryRepository(_context);
    }

    public void ScheduleItemsExecute()
    {
        var testdata = productcategoryrepository.GetAllProductCategory();
        MemCache.Set("Teststore", testdata);
    }
}


public class Startup
{
    public IProductCategoryRepository<ProductCategory> productcategoryrepository;
    public IMemoryCache memorycache;

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMemoryCache();
        services.AddSingleton<ScheduledStuff>();

这篇关于MVC Net Core将MemoryCache传递给所有控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆