.net 如何获取Dotnet缓存以在API控制器调用之间保留值?
我有一个API,用于在前端UI和非常慢的后端API之间返回缓存用户配置文件数据。缓存需要能够从后端API获取数据,缓存数据,然后在前端需要时将其提供给前端 问题在于,当调用缓存API时,它会获取数据,将其添加到缓存中,并将数据返回到前端。但是,在后续调用中,缓存声明没有数据,并从慢速api重新获取数据。在Dotnet中是否有我必须启用的设置,以便强制缓存在对API的单独控制器调用之间保留数据?API的作用就像在每个控制器调用上创建了一个新的缓存一样,因此缓存在服务中总是空的 控制器.net 如何获取Dotnet缓存以在API控制器调用之间保留值?,.net,api,controller,memorycache,.net,Api,Controller,Memorycache,我有一个API,用于在前端UI和非常慢的后端API之间返回缓存用户配置文件数据。缓存需要能够从后端API获取数据,缓存数据,然后在前端需要时将其提供给前端 问题在于,当调用缓存API时,它会获取数据,将其添加到缓存中,并将数据返回到前端。但是,在后续调用中,缓存声明没有数据,并从慢速api重新获取数据。在Dotnet中是否有我必须启用的设置,以便强制缓存在对API的单独控制器调用之间保留数据?API的作用就像在每个控制器调用上创建了一个新的缓存一样,因此缓存在服务中总是空的 控制器 p
public async Task<IActionResult> getUser([FromBody] UserInput input)
{
try
{
UserProfileResponse returned = await _userService.GetUserProfile(input);
return Ok(returned);
}
catch (Exception ex)
{
return BadRequest($"Something went wrong. Message: {ex.Message}");
}
}
public异步任务getUser([FromBody]UserInput)
{
尝试
{
UserProfileResponse返回=wait\u userService.GetUserProfile(输入);
返回Ok(已返回);
}
捕获(例外情况除外)
{
return-BadRequest($“出现问题。消息:{ex.Message}”);
}
}
服务
public async Task<UserProfileResponse> GetUserProfile(UserInput input)
{
string key = Constants.USER_CACHE_PREFIX + "_" + input.Inputs.ID;
_entryOptions = new MemoryCacheEntryOptions().SetPriority(CacheItemPriority.NeverRemove);
// Check if the information is in the cache !!! The value is never found in the cache on subsequent calls !!!
_ = this._cache.TryGetValue(key, out UserProfileResponse value);
if (value != null)
{
return value;
}
// Prepare Endpoint Call
var Config = _configuration.GetSection(Constants.Config);
var endpoint = Config [Constants.endpoint];
var function = Constants.function;
// Call slow API service to get user profile
UserProfileResponse result = await service.SendRequest<UserProfileResponse>(input, endpoint, function);
if (result!= null)
{
this._cache.Set<UserProfileResponse>(key, result, _entryOptions);
}
return result;
}
公共异步任务GetUserProfile(UserInput)
{
string key=Constants.USER\u CACHE\u PREFIX+“\u”+input.Inputs.ID;
_entryOptions=new MemoryCacheEntryOptions().SetPriority(CacheItemPriority.NeverRemove);
//检查信息是否在缓存中!!!在后续调用的缓存中从未找到该值!!!
_=this.\u cache.TryGetValue(key,out UserProfileResponse值);
if(值!=null)
{
返回值;
}
//准备端点调用
var Config=_configuration.GetSection(Constants.Config);
var endpoint=Config[Constants.endpoint];
var函数=常数。函数;
//调用慢速API服务以获取用户配置文件
UserProfileResponse结果=等待服务.SendRequest(输入、端点、函数);
如果(结果!=null)
{
此.u cache.Set(键、结果、\u入口选项);
}
返回结果;
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//... Code before
services.AddHttpClient();
services.AddControllers();
services.AddMemoryCache();
services.AddScoped<IMemoryCache, MemoryCache>();
services.AddScoped<IDeserializeJson, DeserializeJson>();
services.AddScoped<IUserService, UserService>();
//... Code after
}
public void配置服务(IServiceCollection服务)
{
//…之前的代码
services.AddHttpClient();
services.AddControllers();
services.AddMemoryCache();
services.addScope();
services.addScope();
services.addScope();
//…后面的代码
}
当您调用AddMemoryCache()
时,这应该足以设置依赖项注入容器。您稍后对addScope()
的调用将吹走服务描述符并覆盖它
删除对addScope()
的调用应使其正常运行
AddScoped
意味着用每个作用域重新创建对象。对于ASP.NET内核,这意味着每个请求都会获得一个新的内存缓存(缓存项集为空)。AddMemoryCache()
方法将其设置为单例,这是本例中的正确配置
您可以看到
AddMemoryCache()
的代码,这解决了问题。谢谢你的解释@durazno33很高兴它有帮助!如果这回答了您的问题,您介意将其标记为正确答案吗?我会的,但我对堆栈溢出还比较陌生,所以系统会在我有足够的声誉之前阻止它。