.net-core - 如何通过 API 调用中提供的键检索 IMemoryCache 中的值
问题描述
我一直在使用 .Net Core Web API,并一直在尝试获取存储在缓存中的键和值。我使用以下语句将其存储在缓存中:
if (!cache.TryGetValue<string>(store.id, out string failureString))
{
_cache.Set<string>(store.id, store.name);
}
return CreatedAtAction("Get", new { store.id, store.name });
使用 JSON:
{
"id":1,
"name":"Tesco"
}
但是我不确定我需要在下面的方法中放入什么才能根据 api 调用中使用的参数返回 id 和 value?
[HttpGet("{id}")]
public IActionResult Get(int id)
{
}
这是可能吗?
提前致谢。
解决方案
我设法让它与以下工作:
[HttpGet("{id}")]
public IActionResult Get(int id)
{
return Ok(_cache.Get(id));
}
推荐阅读
- php - 如何根据选择框值填充 Twitter Typeahead?
- reactjs - 处理子组件中的数据加载
- c# - 第一行作为 C# 中的列名导出到 Excel 文件
- javascript - 如何调用 catch() 方法“尝试捕获”promise 的异常?
- android - 为Android编译boringssl问题
- c++ - “最小对齐”和“首选对齐”有什么区别?
- reactjs - 如何将组件的 ID 传递给它的 prop 函数?
- lisp - 一个独立于其本地范围的变量
- c# - 有没有办法阻止这种情况发生在带有图像的按钮上?
- elasticsearch - 使用较新版本的 Elasticsearch 索引