在 ConcurrentDictionary AddOrUpdate 中为更新部分添加什么
我正在尝试使用 Dictionary 重写一些代码以使用 ConcurrentDictionary。我已经查看了一些示例,但在实现 AddOrUpdate 函数时仍然遇到问题。这是原始代码:
dynamic a = HttpContext;
Dictionary<int, string> userDic = this.HttpContext.Application["UserSessionList"] as Dictionary<int, String>;
if (userDic != null)
{
if (useDic.ContainsKey(authUser.UserId))
{
userDic.Remove(authUser.UserId);
}
}
else
{
userDic = new Dictionary<int,string>();
}
userDic.Add(authUser.UserId, a.Session.SessionID.ToString());
this.HttpContext.Application["UserDic"] = userDic;
我不知道要为更新部分添加什么:
userDic.AddOrUpdate(authUser.UserId,
a.Session.SessionID.ToString(),
/*** what to add here? ***/);
任何指针将不胜感激。
I am trying to re-write some code using Dictionary to use ConcurrentDictionary. I have reviewed some examples but I am still having trouble implementing the AddOrUpdate function. This is the original code:
dynamic a = HttpContext;
Dictionary<int, string> userDic = this.HttpContext.Application["UserSessionList"] as Dictionary<int, String>;
if (userDic != null)
{
if (useDic.ContainsKey(authUser.UserId))
{
userDic.Remove(authUser.UserId);
}
}
else
{
userDic = new Dictionary<int,string>();
}
userDic.Add(authUser.UserId, a.Session.SessionID.ToString());
this.HttpContext.Application["UserDic"] = userDic;
I don't know what to add for the update portion:
userDic.AddOrUpdate(authUser.UserId,
a.Session.SessionID.ToString(),
/*** what to add here? ***/);
Any pointers would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要传递一个
Func
,它返回要在更新时存储在字典中的值。我想在您的情况下(因为您不区分添加和更新)这将是:即
Func
始终返回 sessionId,以便添加和更新都设置相同的值。顺便说一句:MSDN 页面上有一个示例。
You need to pass a
Func
which returns the value to be stored in the dictionary in case of an update. I guess in your case (since you don't distinguish between add and update) this would be:I.e. the
Func
always returns the sessionId, so that both Add and Update set the same value.BTW: there is a sample on the MSDN page.
我希望我没有错过你问题中的任何内容,但为什么不就这样呢?它更简单、原子且线程安全(见下文)。
(请参阅:http://blogs.msdn.com/b/pfxteam/archive/2010/01/ 08/9945809.aspx)
索引器也是原子的。如果您传递一个函数,它可能不是:
,请参阅:http:// /blogs.msdn.com/b/pfxteam/archive/2010/01/08/9945809.aspx
I hope, that I did not miss anything in your question, but why not just like this? It is easier, atomic and thread-safe (see below).
(See: http://blogs.msdn.com/b/pfxteam/archive/2010/01/08/9945809.aspx)
The indexer is atomic, too. If you pass a function instead, it might not be:
See: http://blogs.msdn.com/b/pfxteam/archive/2010/01/08/9945809.aspx
我最终实现了一个扩展方法:
I ended up implementing an extension method:
对于那些感兴趣的人,我目前正在实现一个案例,这是使用“oldValue”又名现有值而不是强制使用新值的一个很好的例子(我个人不喜欢“oldValue”一词,因为它不是那个)旧的,当它是在几个处理器时钟周期前从并行线程内创建的)。
For those who are interested in, I am currently implementing a case which is a great example for using the "oldValue" aka existing value instead of forcing a new one (personally I don't like the term "oldValue" as it is not that old when it was created just a few processor ticks ago from within a parallel thread).
最简单的 2023 年解决方案,无需引入额外的新变量:
尽情享受!
PS:了解丢弃变量在 MSDN 上。
Easiest 2023 solution without introducing additional new variables:
Enjoy!
PS: learn about the discard variable on MSDN.