同步和异步(test)

同步的意思就是这件事必须做完才会给你结果

异步就是答应了这件事,又把这件事分给别人去做,存在中间商。

异步的实现需要用到中间件,一般像积分,扣库存以及订单写入都会放置在中间件里面

所以中间件也叫消息队列,常见的有kafka这是比较大型的,还有mq

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MongoDB C++ driver支持同步异步操作。下面分别给出同步异步增删改查接口的C++代码示例: 同步操作: ```c++ #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> #include <mongocxx/options/find.hpp> #include <mongocxx/stdx.hpp> #include <bsoncxx/json.hpp> mongocxx::instance instance{}; // 实例化驱动 mongocxx::client client{mongocxx::uri{}}; // 连接MongoDB数据库 mongocxx::database db = client["test"]; // 选择数据库 mongocxx::collection coll = db["test_collection"]; // 选择集合 bsoncxx::builder::stream::document document{}; document << "name" << "John Doe" << "age" << 42; coll.insert_one(document.view()); // 插入一条文档 auto builder = bsoncxx::builder::stream::document{}; auto query = builder << "name" << "John Doe" << bsoncxx::builder::stream::finalize; auto update = builder << "$set" << bsoncxx::builder::stream::open_document << "age" << 43 << bsoncxx::builder::stream::close_document << bsoncxx::builder::stream::finalize; coll.update_one(query.view(), update.view()); // 更新一条文档 auto delete_result = coll.delete_one(query.view()); // 删除一条文档 ``` 异步操作: ```c++ #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> #include <mongocxx/pool.hpp> #include <mongocxx/options/find.hpp> #include <mongocxx/stdx.hpp> #include <bsoncxx/json.hpp> mongocxx::instance instance{}; // 实例化驱动 mongocxx::pool pool{mongocxx::uri{}}; // 连接池 mongocxx::database db = pool.acquire().database("test"); // 从连接池中获取连接并选择数据库 mongocxx::collection coll = db["test_collection"]; // 选择集合 auto insert_result = coll.insert_one( bsoncxx::builder::stream::document{} << "name" << "John Doe" << "age" << 42 << bsoncxx::builder::stream::finalize, mongocxx::options::insert{}); // 插入一条文档 auto update_result = coll.update_one( bsoncxx::builder::stream::document{} << "name" << "John Doe" << bsoncxx::builder::stream::finalize, bsoncxx::builder::stream::document{} << "$set" << bsoncxx::builder::stream::open_document << "age" << 43 << bsoncxx::builder::stream::close_document << bsoncxx::builder::stream::finalize, mongocxx::options::update{}); // 更新一条文档 auto delete_result = coll.delete_one( bsoncxx::builder::stream::document{} << "name" << "John Doe" << bsoncxx::builder::stream::finalize, mongocxx::options::delete{}); // 删除一条文档 ``` 需要注意的是,异步操作需要使用连接池,而同步操作不需要。此外,异步操作需要使用回调函数处理结果,但这里没有给出回调函数的示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值