V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
TangMonk
V2EX  ›  问与答

把并行的 http 请求转换成顺序的,用 redis 的 atomic set get 行吗?

  •  1
     
  •   TangMonk · Jun 20, 2017 · 1426 views
    This topic created in 3239 days ago, the information mentioned may be changed or developed.
      app.get('/', async (req, res) => {
        const order_id = await redis.get(`order_id:${req.params.order_id}`)
        if(order_id === 'true'){
          return res.send({message: 'order is finished'})
        }else{
          await redis.set(`order_id:${order_id}`, 'true')
        }
    
        // do some operate below...
      })
    

    有没有更标准的做法?

    2 replies    2017-06-20 13:05:24 +08:00
    msg7086
        1
    msg7086  
       Jun 20, 2017   ❤️ 1
    随便用个锁就行了。redis 可以做吧。
    没看出来你这是什么语言,我就猜是 JS 吧,随便帮你搜了个 https://www.npmjs.com/package/redis-lock。
    fuxkcsdn
        2
    fuxkcsdn  
       Jun 20, 2017 via iPhone
    redis list
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2155 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 00:49 · PVG 08:49 · LAX 17:49 · JFK 20:49
    ♥ Do have faith in what you're doing.