问题标签 [concurrentdictionary]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
700 浏览

c# - 使用 C# 从一个 ConcurrentDictionary 中的数组输入和获取值

到目前为止,我一直在使用简单的数组来输入和获取必要的信息。

第一个例子如下:

第二个例子:

但是现在我正在尝试为每个数组分配一个标识号,这样我就可以在一个 ConcurrentDictionary 中识别多个不同的数组。

如何从字典中的数组中输入和获取信息?

看,我们有两个标识符和两个字典:

我在想象这样的事情:

PS:上面的代码不起作用(因为它是自制的)..那么通过ID将信息输入到ConcurrentDictionary中的string []和string [] []的正确方法是什么?(并把它拿出来)

0 投票
2 回答
7390 浏览

c# - 如何告诉“ConcurrentDictionary.GetOrAdd”不添加值?

我有几种ConcurrentDictionary<TKey, TValue>用于缓存值的情况,但通常我需要对值进行验证以决定是否使用ConcurrentDictionary<TKey, TValue>.GetOrAdd(TKey, Func<TKey, TValue>).

通常遵循以下原则:

我今天处理这个问题的方法是抛出一个似乎可以正常工作的异常,但我想要一种更简洁的方法(可能是我可以设置的标志或我可以将返回值设置为的特定值)来取消GetOrAdd从lambda(尽管它实际上可以被一个完整的方法取代)。

根据我对其他类似 LINQ 的方法的经验,返回null将导致值被添加而不被检查(并且读取 ILGetOrAdd看起来会导致同样的问题),所以我不认为'会工作的。

有什么方法可以避免使用异常来取消添加使用GetOrAdd

0 投票
2 回答
2252 浏览

c# - C#并发字典不保存我的列表页面加载之间的值

自从我被难住以来已经有一段时间了。疯狂的是,我已经在我的代码的其他区域多次这样做了,所以它几乎是完整的复制和粘贴,但除了这段代码不能正常工作。所以我不知何故错过了一些非常明显的东西。

上面的类在 Application_Start() global.asax.cs 中实例化,如下所示:

所以现在在页面加载之间,我的字典没有保留我在调用 UpdateRoomOnlineTraderList 时添加到列表中的值。当我通过时,列表就在那里。下次我加载页面时它就消失了,我是 100% 没有其他东西从字典中删除这个值。

我的字典如何在页面加载之间不保留值?密钥仍然保留,但价值消失了。我很困惑。

0 投票
1 回答
1059 浏览

dapper - dapper.net,如何刷新 ConcurrentDictionary?

我是 dapper 的新手,并计划在我的新项目中使用它。读完之后,似乎我唯一可能遇到的问题是 ConcurrentDictionary。

Dapper 缓存有关它运行的每个查询的信息,这允许它快速实现对象并快速处理参数。当前实现将此信息缓存在 ConcurrentDictionary 对象中。它存储的对象永远不会被刷新。如果您在不使用参数的情况下即时生成 SQL 字符串,您可能会遇到内存问题。我们可以将字典转换为 LRU 缓存。

我该如何避免这个问题?有人可以告诉我一些代码告诉我如何以及何时刷新它吗?

0 投票
2 回答
2133 浏览

c# - 关于 ConcurrentDictionary 使用的一些问题

我目前正在编写一个 C# 应用程序。我是使用 ConcurrentDictionary 的新手,所以对它的线程安全有一些疑问。首先,这是我的字典:

我在我的类中实例化它并使用它来跟踪我所有实现 ITask 的对象。我想确保我的设置能够在多线程环境中正常工作。

如果多个线程想要获取 ConcurrentDictionary 中的项数的计数,是否需要锁定它?

如果我想从字典中获取特定的键,获取该键的对象并调用它的方法,我需要锁定它吗?例如:

请记住,多个线程可以调用 Run 方法来调用 ITask 的 Execute 方法。我的目标是让所有东西都线程安全并尽可能高效。

0 投票
3 回答
664 浏览

.net - System.Collections.Concurrent 集合上的扩展方法是线程安全的吗?

例如,在 ConcurrentDictionary 类上有几个扩展方法,因为它实现了 IEnumerable 接口。这些方法(例如 First、Sum、Take 等)是否本质上是线程安全的?

0 投票
3 回答
5550 浏览

c# - Need an efficient in-memory cache that can process 4k to 7k lookups or writes per second

I have an efficient C# application that receives 80 bytes of data at a rate of 5k to 10k records per second on a multi threaded CPU.

I need to now set up a in memory-cache to detect and filter duplicate records so I can suppress them from travelling further in the pipeline.

Cache specs (maximum thresholds)

  • 80 bytes of data
  • 10,000 records / second
  • 60 Seconds of cache = Key quantity = 60,000
  • (sub total 48000000 bytes = 48Mb )
  • Ideal cache size = 5 minutes (or 240Mb)
  • Acceptable runtime cache size bloat = 1 GB

Question

What is the best way to set up an in-memory cache, dictionary, hashtable, array, etc that will allow the most efficient lookups, purging of old cache data, and prevent expiration of data that is hit.

I looked at ASP.Net Cache, System.Runtime.MemoryCache but think I need something more lightweight and customized to achieve correct throughput. I'm also looking at the System.Collections.Concurrent as an alternative and this related whitepaper.

Does anyone have suggestions on what the best approach would be?

0 投票
1 回答
2022 浏览

c# - 防御 System.Collections.Concurrent.ConcurrentDictionary 中的竞争条件

.NET ConcurrentDictionary 容易受到可能导致意外数据的竞争条件的影响,如本 MSDN 文章底部所述。 我假设有几个因素需要考虑。

问:我应该如何编写不易受到可能导致数据丢失的竞争条件的代码?

在我的场景中,我有一个输入流,它的索引总是增加(n++)。我的想法是,如果出现竞争条件并重新发送它,我可以检测到丢失的数据。另一方面,我不知道可能有更好的方法来做到这一点。

0 投票
2 回答
643 浏览

c# - 我应该如何保持 2 ConcurrentDictionaries 同步?

我喜欢ConcurrentDictionary的无锁操作,并在两个对象中使用它:

这两个对象需要保持同步。这样做的唯一方法是使用锁,从而避免并发字典的所有好处吗?

0 投票
2 回答
4348 浏览

c# - 如何在 BlockingCollection 中包装 ConcurrentDictionary?

我尝试通过将 ConcurrentDictionary 包装在 BlockingCollection 中来实现它,但似乎没有成功。

我了解一个变量声明与 BlockingCollection 一起使用,例如ConcurrentBag<T>,ConcurrentQueue<T>等。

因此,要创建一个包装在 BlockingCollection 中的 ConcurrentBag,我会像这样声明和实例化:

但是如何为 ConcurrentDictionary 做呢?我需要 BlockingCollection 在生产者和消费者端的阻塞功能。