首页 > 解决方案 > 如何通过 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)
    {

    }

这是可能吗?

提前致谢。

标签: .net-core

解决方案


我设法让它与以下工作:

 [HttpGet("{id}")]
    public IActionResult Get(int id)
    {
        return Ok(_cache.Get(id));
    }

推荐阅读