首页 > 解决方案 > 如何在 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.
}

标签: c#cachingconcurrencylockingdistributed-caching

解决方案


推荐阅读