In-Memory .NET EF Core 缓存抛出错误
In-Memory Cache with .NET EF Core throws error
我有一个带有 EF 的 .Net Core 应用程序,其中 returns 一个名为 Customer 的页面作为主页。但是当用户试图在假期访问应用程序时(假期列表存储在 table)而不是客户页面,我需要显示一个应用程序不可用页面(AppNotAvialable.cshmtl)。我尝试使用 In-Memory 缓存,而不是每次访问主页时都查询假期列表 table,这样我就可以将查询响应存储在缓存中并在控制器中使用它们。我是第一次实现缓存,以下是我尝试过的方法
public class CustomersController : Controller
{
private readonly SurplusMouseContext _context;
private readonly IMemoryCache memoryCache;
.......................
public async Task<IActionResult> Index(string sortOrder, string searchString,
int? pageNumber, string currentFilter)
{
int holidaycheck;
var timeUtc = DateTime.UtcNow;
var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var todayDt = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);
bool isExist = memoryCache.TryGetValue("HolidayWk", out holidaycheck);
if (!isExist)
{
holidaycheck = (from hc in _context.HolidayWeeks
where hc.HolidateDate.Date == todayDt.Date
select hc).Count();
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(2));
memoryCache.Set("HolidayWk", holidaycheck, cacheEntryOptions);
}
if (holidaycheck != 0)
{
return View("/Views/Customers/AppNotAvailable.cshtml");
}
else
{
当我尝试调试应用程序时,它抛出如下错误
任何人都可以建议我是否在这里实施内存缓存 correct/missing 任何东西以及我应该在哪里做 cacheMemory.Remove()
非常感谢任何帮助
第一步检查:
在您的问题中,您可能没有在构造函数中初始化 MemoryCache
对象。
有个例子MemoryCache
。
1 - 您应该在这样的服务中注册 MemoryCache
:
services.AddMemoryCache();
2 - 在控制器中注入 IMemoryCache
接口并初始化它。
public class YourController : Controller
{
private readonly IMemoryCache _memoryCache;
public YourController(IMemoryCache memoryCache)
{
_memoryCache = memoryCache
}
}
3 - 使用内存缓存:
public async Task<IActionResult> Index(string sortOrder, string searchString,
int? pageNumber, string currentFilter)
{
bool isExist = _memoryCache.TryGetValue("HolidayWk", out holidaycheck);
}
我有一个带有 EF 的 .Net Core 应用程序,其中 returns 一个名为 Customer 的页面作为主页。但是当用户试图在假期访问应用程序时(假期列表存储在 table)而不是客户页面,我需要显示一个应用程序不可用页面(AppNotAvialable.cshmtl)。我尝试使用 In-Memory 缓存,而不是每次访问主页时都查询假期列表 table,这样我就可以将查询响应存储在缓存中并在控制器中使用它们。我是第一次实现缓存,以下是我尝试过的方法
public class CustomersController : Controller
{
private readonly SurplusMouseContext _context;
private readonly IMemoryCache memoryCache;
.......................
public async Task<IActionResult> Index(string sortOrder, string searchString,
int? pageNumber, string currentFilter)
{
int holidaycheck;
var timeUtc = DateTime.UtcNow;
var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var todayDt = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);
bool isExist = memoryCache.TryGetValue("HolidayWk", out holidaycheck);
if (!isExist)
{
holidaycheck = (from hc in _context.HolidayWeeks
where hc.HolidateDate.Date == todayDt.Date
select hc).Count();
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(2));
memoryCache.Set("HolidayWk", holidaycheck, cacheEntryOptions);
}
if (holidaycheck != 0)
{
return View("/Views/Customers/AppNotAvailable.cshtml");
}
else
{
当我尝试调试应用程序时,它抛出如下错误
任何人都可以建议我是否在这里实施内存缓存 correct/missing 任何东西以及我应该在哪里做 cacheMemory.Remove()
非常感谢任何帮助
第一步检查:
在您的问题中,您可能没有在构造函数中初始化 MemoryCache
对象。
有个例子MemoryCache
。
1 - 您应该在这样的服务中注册 MemoryCache
:
services.AddMemoryCache();
2 - 在控制器中注入 IMemoryCache
接口并初始化它。
public class YourController : Controller
{
private readonly IMemoryCache _memoryCache;
public YourController(IMemoryCache memoryCache)
{
_memoryCache = memoryCache
}
}
3 - 使用内存缓存:
public async Task<IActionResult> Index(string sortOrder, string searchString,
int? pageNumber, string currentFilter)
{
bool isExist = _memoryCache.TryGetValue("HolidayWk", out holidaycheck);
}