C#でMongoDBへの非同期挿入/更新を行うにはどうすればよいですか?怠惰な永続性の用語は何ですか?
- 後書き
MongoDB の挿入は、ファイア アンド フォーゲットであるため、デフォルトでは一種の非同期です。エラー チェックは明示的な操作であるか、ドライバー レベルでセーフ モードを有効にする必要があります。真の非同期操作が必要な場合: メッセージ キューを使用します。
キャッシングの世界では、「遅延永続性」は後書きと呼ばれます。これをチェックしてください:キャッシュ/ウィキペディア
おそらく最も簡単な方法は、C# の async メソッド呼び出しを使用することです。これにより、次の方法がわかります。
コードは次のようになります。
独自のデリゲートを定義します。
private delegate void InsertDelegate(BsonDocument doc);
これを使って
MongoCollection<BsonDocument> books = database.GetCollection<BsonDocument>("books");
BsonDocument book = new BsonDocument {
{ "author", "Ernest Hemingway" },
{ "title", "For Whom the Bell Tolls" }
};
var insert = new InsertDelegate(books.Insert);
// invoke the method asynchronously
IAsyncResult result = insert.BeginInvoke(book, null, null);
// DO YOUR OWN WORK HERE
// get the result of that asynchronous operation
insert.EndInvoke(result);
それが役立つことを願っています。