IMemoryCache get return null after setting a value asp.net core 2.2
IMemoryCache get return null after setting a value asp.net core 2.2
我正在尝试在我的 ASP.NET CORE 2.2 Web API 应用程序中使用 IMemoryCache 服务。我在启动时的 ConfigureServices 函数中添加了服务(.AddMemoryCache())。
在开始使用它之前,我决定测试一下,看看我是否可以成功地设置一个值并使用该服务检索它。
不幸的是,每当我设置一个值然后尝试检索它时,API 响应 return 204 找不到内容。
我做错了什么,请帮忙。
谢谢。
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddMvc();
}
然后在我的 TestController
namespace JwtTut.Controllers
{
[Route("api")]
public class TestController : Controller
{
private readonly ILogger _logger;
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityUser> userManager;
private readonly RoleManager<IdentityRole> roleManager;
private readonly IIdentityService _identityService;
private IMemoryCache _memoryCache;
public TestController(ILogger<TestController> logger, ApplicationDbContext context,
UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager,
IIdentityService identityService, IMemoryCache memoryCache)
{
_logger = logger;
_context = context;
this.userManager = userManager;
this.roleManager = roleManager;
_identityService = identityService;
_memoryCache = memoryCache;
}
[HttpGet("user-claims")]
public IActionResult GetUserClaims()
{
var randomList = new List<string>();
for (int i = 0; i < 20; i++)
{
randomList.Add($"{i}");
}
_memoryCache.Set("randomLIst", randomList);
var fromCache = _memoryCache.Get<string>("randomList");
return Ok(fromCache);
}
}
}
我正在使用 api/user-claims 来测试 IMemoryCache 服务
看下面两行。您用于设置缓存的密钥(randomLIst)与您用于获取缓存的密钥(randomList)不同:
_memoryCache.Set("randomLIst", randomList);
var fromCache = _memoryCache.Get<string>("randomList");
更正:
_memoryCache.Set("randomList", randomList);
var fromCache = _memoryCache.Get<string>("randomList");
我建议您为缓存键制作一个单独的实用程序 class 以避免印刷错误。
我正在尝试在我的 ASP.NET CORE 2.2 Web API 应用程序中使用 IMemoryCache 服务。我在启动时的 ConfigureServices 函数中添加了服务(.AddMemoryCache())。 在开始使用它之前,我决定测试一下,看看我是否可以成功地设置一个值并使用该服务检索它。 不幸的是,每当我设置一个值然后尝试检索它时,API 响应 return 204 找不到内容。 我做错了什么,请帮忙。 谢谢。
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddMvc();
}
然后在我的 TestController
namespace JwtTut.Controllers
{
[Route("api")]
public class TestController : Controller
{
private readonly ILogger _logger;
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityUser> userManager;
private readonly RoleManager<IdentityRole> roleManager;
private readonly IIdentityService _identityService;
private IMemoryCache _memoryCache;
public TestController(ILogger<TestController> logger, ApplicationDbContext context,
UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager,
IIdentityService identityService, IMemoryCache memoryCache)
{
_logger = logger;
_context = context;
this.userManager = userManager;
this.roleManager = roleManager;
_identityService = identityService;
_memoryCache = memoryCache;
}
[HttpGet("user-claims")]
public IActionResult GetUserClaims()
{
var randomList = new List<string>();
for (int i = 0; i < 20; i++)
{
randomList.Add($"{i}");
}
_memoryCache.Set("randomLIst", randomList);
var fromCache = _memoryCache.Get<string>("randomList");
return Ok(fromCache);
}
}
}
我正在使用 api/user-claims 来测试 IMemoryCache 服务
看下面两行。您用于设置缓存的密钥(randomLIst)与您用于获取缓存的密钥(randomList)不同:
_memoryCache.Set("randomLIst", randomList);
var fromCache = _memoryCache.Get<string>("randomList");
更正:
_memoryCache.Set("randomList", randomList);
var fromCache = _memoryCache.Get<string>("randomList");
我建议您为缓存键制作一个单独的实用程序 class 以避免印刷错误。