Is ConcurrentDictionary.GetOrAdd() guaranteed to invoke valueFactoryMethod only once per key?

前端 未结 3 1719
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 16:29

Problem: I need to implement object cache. The cache need to be thread-safe and need to populate values on demand(lazy loading). The values are retrieved vi

3条回答
  •  日久生厌
    2021-01-07 16:51

    Is value factory invoked only once per key?

    No, it isn't. The docs say:

    If you call GetOrAdd simultaneously on different threads, valueFactory may be invoked multiple times, but its key/value pair might not be added to the dictionary for every call.

提交回复
热议问题