c# - 将数据存储在范围服务(按请求)中以提高性能的缺点是什么
问题描述
服务如下所示:
public class TestService
{
private ExternalService _externalService;
private Dictionary<int, string> _data = new Dictionary<int, string>();
public TestService(ExternalService externalService)
{
_externalService = externalService;
}
public string GetItem(int id)
{
if (_data.TryGetValue(id, out var item))
{
return item;
}
var result = _externalService.GetItem(id);
this._data.Add(id, result);
return result;
}
}
和注册:
services.AddScoped<TestService>();
不会从多个线程调用此服务。
这种解决方案有什么缺点?字典中可能有数百或数千个项目。mb 它会导致一些内存问题吗?
解决方案
正如你所写的那样,缺点是内存。另一个缺点是你可以获得脏值。
我会使用标准的缓存系统,比如 Redis。然后,您还可以阅读有关到期时间的信息。
带有缓存的模式本身就是数百万其他网站用来保存到不同地方的往返行程的方法。
推荐阅读
- matplotlib - 根据列中的值使用 matplotlib 生成图例
- ms-access - 比较 Access 查询中的 2 个以上字段
- c++ - C ++中线程中带有while循环的条件变量
- android - 动态获取 XML 中的应用程序 ID
- reactjs - 如何将文件附件按钮更改为发送消息“重置”的重置按钮
- python - 查找与文档中单词出现相关的单词频率
- sql-server - 获取状态变化数据
- xamarin - 检查本地通知的 NSDictionary 中是否存在值
- php - 无法创建 index.html 文件 - TimThumb
- azure - 恢复“访问用户的个人资料”API 访问