c# - 如何在 C# 中为类似的用户 ID 请求锁定?
问题描述
我有一个从 API 获取值然后在缓存中更新的函数。所有用户都使用此功能,但我不想多次更新该值以缓存。如何避免多次写入?如何为多个用户获取锁(如果可能的话)?如果用户使用锁,则不应阻止A
用户并继续更新值并使用自己的锁。B
所有请求A
都应该只更新一次到缓存中。
Void foo(Guid userId){
bool isFound = GetfromCache(userId);
if(!isFound)
var val = GetValueFromAPI();
UpdateInsideCache(val, userId); // Here we could have multiple requests updating the same entry, since it is high scale system ~ Assume 100 Million request per day.
}
解决方案
推荐阅读
- webpack - webpack-dev-server 热加载器不工作
- php - 使用 ssh 隧道连接拒绝用户 'root'@'localhost' 的访问(使用密码:YES)
- python - 将标题添加到scrapy?
- react-native - 在 refreshcontrol 触发后 React Native Stop ScrollView 事件
- javascript - 运行 SQL 查询,将响应对象作为数组返回。我需要知道如何创建循环来读取返回的每条记录
- python - Django:禁止(未设置 CSRF cookie。),为什么会这样,以及如何解决它
- javascript - 将内容加载到 Kendo UI KendoWindow 时处理错误
- python - 用另一个数据框 pandas 重命名每 2 列
- python - 在 python 中使用 Espeak 和 os
- podman - 使用 Podman 在 RHEL8 上运行 OpenMapTilesServer