3

ファイルのリストがあり、各ファイルにはFooデータのリストが含まれています。現在、同じ Foo データ (例: Id = 1) が複数のファイルに存在する可能性がありますが、より新しいデータが既存のデータを上書きします。

各データをメモリ内コレクションに読み込んでいます。

if !cache.HasKey(foo.Id) then Add    
else cache[foo.Id].UpdatedOn < foo.UpdatedOn then Update  
else do nothing

私がファイルを読んでいるとき(emがいくつかあるため)、私も使用していますParallel.ForEach(files, file => { .. });

どうやってこれを行うのかわかりません。

を使用することを考えていましたが、where句を使用するConcurrentDictionary方法がわかりませんでした。AddOrUpdate

助言がありますか?

4

2 に答える 2

0

メソッドの興味深い動作ConcurrentDictionary.AddOrUpdate:

class Program
{
    static void Main( string[] args )
    {
        var cd = new System.Collections.Concurrent.ConcurrentDictionary<int, int>();

        var a = 0;
        var b = 1;
        var c = 2;

        cd[ 1 ] = a;

        Task.WaitAll(
            Task.Factory.StartNew( () => cd.AddOrUpdate( 1, b, ( key, existingValue ) =>
                {
                    Console.WriteLine( "b" );
                    if( existingValue < b )
                    {
                        Console.WriteLine( "b update" );
                        System.Threading.Thread.Sleep( 2000 );
                        return b;
                    }
                    else
                    {
                        Console.WriteLine( "b no change" );
                        return existingValue;
                    }
                } ) ),

            Task.Factory.StartNew( () => cd.AddOrUpdate( 1, c, ( key, existingValue ) =>
            {
                Console.WriteLine( "c start" );
                if( existingValue < c )
                {
                    Console.WriteLine( "c update" );
                    return c;
                }
                else
                {
                    Console.WriteLine( "c no change" );
                    return existingValue;
                }
            } ) ) );

        Console.WriteLine( "Value: {0}", cd[ 1 ] );

        var input = Console.ReadLine();
    }
}

結果:

ConcurrentDictionary.AddOrUpdate テスト出力

于 2014-02-19T11:36:03.627 に答える