[英]MVC Net Core Pass MemoryCache to All Controllers
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显示为边栏(短类别,例如衣服,电子产品,家具,书籍等)。
At the end, I want to send around productcategory _cache variable to all controllers.Does this methodology seem correct? 最后,我想将productcategory _cache变量发送给所有控制器,这种方法似乎正确吗? Additionally, how do I remove unnecessary memorycache parameter in Home Controller? 此外,如何在Home Controller中删除不必要的memorycache参数? Make a method in Scheduled stuff to retrieve it? 在计划的资料中创建方法来检索它?
DI Solution: 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 Core: Use memory cache outside controller Asp.Net Core:在控制器外部使用内存缓存
Solution Static: Keep receiving Null exception error, can someone help fix this? 解决方案静态:不断收到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) ;
}
This should work, thanks for the help Henk- 这应该有效,感谢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>();
Cache is one of those few things that are good candidates for the Singleton design pattern. 高速缓存是可用于Singleton设计模式的少数几项之一。 Plus, it is thread-safe. 另外,它是线程安全的。 Just make a global cache object that is accessible to all that need it. 只需创建一个全局缓存对象,所有需要它的对象都可以访问它。
I solved this issue with a static CacheHelper class. 我使用静态CacheHelper类解决了此问题。 You can call this static class from any controller . 您可以从任何控制器调用此静态类。 You can get more ideas about how to implement it here: How to cache data in a MVC application 您可以在此处获得有关如何实现它的更多想法: 如何在MVC应用程序中缓存数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.