MVC Net Core 将 MemoryCache 传递给所有控制器

MVC Net Core Pass MemoryCache to All Controllers

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

最后,我想将 productcategory _cache 变量发送给所有 controllers.Does 这种方法似乎正确吗?此外,如何删除 Home Controller 中不必要的 memorycache 参数?在 Scheduled stuff 中创建一个方法来检索它?

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

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

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

缓存是为数不多的适合单例设计模式的东西之一。另外,它是线程安全的。只需创建一个可供所有需要它的人访问的全局缓存对象。

我用静态 CacheHelper class 解决了这个问题。您可以从任何控制器调用此静态 class。您可以在此处获得有关如何实施它的更多想法:How to cache data in a MVC application

这应该可以,感谢 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>();