c# - MVC Net Core 将 MemoryCache 传递给所有控制器
问题描述
我正在建立一个在线商店网站。我想将 ProductCategory 显示为侧边栏(短列表类别,例如衣服、电子产品、家具、书籍等)。
最后,我想将 productcategory _cache 变量发送给所有控制器。这种方法看起来正确吗?另外,如何删除 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) ;
}
解决方案
这应该可以,谢谢你的帮助 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>();
推荐阅读
- apache-spark - Mesos 实现
- consul - 当 consul 节点在服务器模式下运行时,/v1/agent/services 将返回哪个端点?
- python - 将 str 拆分为 int 然后变成字典
- sorting - 无法在 ARM 中对数组进行排序。已排序,但有数字缺失和重复
- amazon-web-services - ECS Windows Optimized AMI 启用 IAM 任务角色
- sql - 表值函数和视图
- vuejs2 - VueJS 2 - 可以接受不同子级的可重用父组件
- phpmyadmin - phpmyadmin 安装后未捕获的错误
- python - Python中的增强赋值意外行为
- jsf - PrimeFaces 组件的自定义渲染器未注册