C# MVC网络核心将MemoryCache传递给所有控制器

C# MVC网络核心将MemoryCache传递给所有控制器,c#,memory,asp.net-core,memorycache,C#,Memory,Asp.net Core,Memorycache,我正在建立一个网上商店网站。我想将ProductCategory显示为边栏(短列表类别,如衣服、电子产品、家具、书籍等) 最后,我想向所有控制器发送productcategory\u缓存变量。这种方法正确吗?此外,如何删除Home Controller中不必要的memorycache参数?在Scheduled stuff中创建一个方法来检索它 DI解决方案: public class HomeController : Controller { private readonly IM

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

最后,我想向所有控制器发送productcategory\u缓存变量。这种方法正确吗?此外,如何删除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>();
公共类HomeController:控制器
{    
专用只读IMemoryCache\u缓存;
公共IScheduledStuff;
公共HomeController(IMemoryCache缓存,isScheduledStuff scheduledstuff)
{
_缓存=缓存;
_scheduledstuff=scheduledstuff;
_scheduledstuff.ScheduleItemsExecute();
}
公共IActionResult索引()
{
ViewBag.ProductCategoryList=_cache.Get(“Teststore”);
返回视图();
}
}
公共类ProductCategoryRepository:IPProductCategoryRepository
{
public IEnumerable GetAllProductCategory()
{
return_context.ProductCategory.ToList();
}
}
公共产品类别()
{
public int ProductCategoryId{get;set;}
公共字符串ProductCategoryName{get;set;}
公共字符串ProductCategoryDescription{get;set;}
}
公共类ScheduledStuff:IsScheduledStuff
{
私有只读数据库上下文\u上下文;
IMemoryCache-MemCache;
公共IPProductCategoryRepository productcategoryrepository;
公共ScheduledStuff(数据库上下文,IMemoryCache memCache)
{
_上下文=上下文;
MemCache=MemCache;
productcategoryrepository=新productcategoryrepository(_上下文);
}
公共无效ScheduleItemsExecute()
{
var testdata=productcategoryrepository.GetAllProductCategory();
Set(“Teststore”,testdata);
}
}
公营创业
{
公共IPProductCategoryRepository productcategoryrepository;
公共IMemoryCache memorycache;
public void配置服务(IServiceCollection服务)
{
services.AddMemoryCache();
services.AddSingleton();

解决方案静态:持续接收空异常错误,有人能帮助修复此问题吗

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) ;
     }
公共静态类MemoryCacheStatic
{
公共静态IMemoryCache MemCacheStatic;
公共静态i产品类别存储产品类别存储静态;

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

我用一个静态CacheHelper类解决了这个问题。你可以从任何控制器调用这个静态类。你可以获得更多想法关于如何在这里实现它:

这应该可以,感谢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>();
公共类HomeController:控制器
{    
专用只读IMemoryCache\u缓存;
公共IScheduledStuff;
公共HomeController(IMemoryCache缓存,isScheduledStuff scheduledstuff)
{
_缓存=缓存;
_scheduledstuff=scheduledstuff;
_scheduledstuff.ScheduleItemsExecute();
}
公共IActionResult索引()
{
ViewBag.ProductCategoryList=_cache.Get(“Teststore”);
返回视图();
}
}
公共类ProductCategoryRepository:IPProductCategoryRepository
{
public IEnumerable GetAllProductCategory()
{
return_context.ProductCategory.ToList();
}
}
公共产品类别()
{
public int ProductCategoryId{get;set;}
公共字符串ProductCategoryName{get;set;}
公共字符串ProductCategoryDescription{get;set;}
}
公共类ScheduledStuff:IsScheduledStuff
{
私有只读数据库上下文\u上下文;
IMemoryCache-MemCache;
公共IPProductCategoryRepository productcategoryrepository;
公共ScheduledStuff(数据库上下文,IMemoryCache memCache)
{
_上下文=上下文;
MemCache=MemCache;
productcategoryrepository=新productcategoryrepository(_上下文);
}
公共无效ScheduleItemsExecute()
{
var testdata=productcategoryrepository.GetAllProductCategory();
Set(“Teststore”,testdata);
}
}
公营创业
{
公共IPProductCategoryRepository productcategoryrepository;
公共IMemoryCache memorycache;
public void配置服务(IServiceCollection服务)
{
services.AddMemoryCache();
services.AddSingleton();

另一种解决方案:然而,这似乎不正确。
您能解释一下吗?嗯,我似乎有很多列表要加载(5+),我应该将它们全部放在ConfigureServices中,还是使它们成为全局的?AspNet Core是围绕DI构建的,您已经使用构造函数注入。这就是您所需要的。而且您的代码示例完全无效,不可编译。这使您很难理解您的问题。只是使用它是另一回事,仍然尝试让配置服务编译处理它,只是想确保我在正确的路径上,所以您建议configureservices路径我假设您不能在方法中声明公共字段/属性。是的,按照流程进行。工作量越少,错误越少。因此,为了澄清这一点,选项一-全局模式,不要将所有内容都放在configureservices?一个包含对MemoryCache静态实例的引用。但是,为了可测试性,允许通过setter设置MemoryCache的实例。或者,正如@David在他的回答中所说的。在net core 2中似乎不起作用,你能试试吗?我认为一般模式不会随特定框架而改变。只需创建一个“公共静态类CacheHelper”初始化并生成一个“GetFromCache”函数,您可以从任何控制器调用该函数。在“GetFromCache”函数中,您可以从缓存请求对象,如果对象不存在,则可以从数据库和