1

如何在 C# 中对 MongoDB 进行异步插入/更新?惰性持久性的术语是什么?

  • 后写
4

2 回答 2

1

MongoDB 插入默认是异步的,因为它是即发即弃的。错误检查是一项显式操作,或者您必须在驱动程序级别启用安全模式。如果您需要真正的异步操作:使用消息队列。

于 2011-04-11T12:15:47.617 回答
0

在缓存世界中,“惰性持久性”将被称为后写。看看这个:缓存/维基百科

可能最简单的方法是使用 C# 异步方法调用。这将告诉您如何:

代码看起来像:

  • 定义你自己的委托:

        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);
    

希望有帮助。

于 2011-04-11T12:22:11.590 回答