在预订系统中,只有 5 个不同的用户可以创建预订。如果 100 个用户同时调用预订 api,如何使用锁定处理并发。我正在使用nodejs和mongodb。我浏览了 mongo 并发文章和 mongodb 中的事务,但找不到任何带有锁定的示例编码解决方案。
我已经通过乐观并发控制实现了解决方案(当资源争用较低时 - 这可以使用 versionNumber 或 timeStamp 字段轻松实现)。
提前感谢您向我建议锁定解决方案。
现在的算法是:
步骤 1:从 userSettings 集合中获取 userAllowedNumber。
//Query
db.getCollection('userSettings').find({})
//Response Data
{ "userAllowedNumber": 5 }
Run Code Online (Sandbox Code Playgroud)
步骤2,从预订集合中获取当前的bookedCount。
//Query
db.getCollection('bookings').count({ })
//Response Data
2
Run Code Online (Sandbox Code Playgroud)
第 3 步,如果bookedCount <= userAllowedNumber则插入预订。
//Query
db.getCollection('bookings').create({ user_id: "usr_1" })
Run Code Online (Sandbox Code Playgroud) 我使用 url: URL.createObjectURL(file[0]) 创建了 PDF 文件的对象 url。
\n\n例如 url = blob: http://0.0.0.0:5002/e468fb70-d597-4b17-bb3a-ec6272f2d7fe。
\n\n现在我想做的是, \n从 url=blob: http://0.0.0.0:5002/e468fb70-d597-4b17-bb3a-ec6272f2d7fe读取 pdf并创建文件对象,例如
\n\nFile(61)\xc2\xa0\n{\n name: "ICT_zbbihfc_dummy.pdf", \n lastModified: 1548148972779, \n lastModifiedDate: Tue Jan 22 2019 17:22:52 GMT+0800 (Singapore Standard Time), \n webkitRelativePath: "", \n size: 61,\n type: "application/pdf",\n webkitRelativePath: ""\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是 JavaScript 中的。
\n