How to create a distributed lock with Redis?

前端 未结 5 1234
广开言路
广开言路 2020-12-28 17:37

On the redis documentation, I found a primitive lock can be implemented via SETNX:

http://redis.io/commands/setnx

  • C4 sends SETNX lo

5条回答
  •  温柔的废话
    2020-12-28 18:28

    Use SET instead of SETNX. SET accepts arguments for expiration time in seconds and milliseconds instead of UNIX timestamp value.

    The old SETNX based pattern is documented only for historical reasons.

    From SETNX description:

    NOTE: Starting with Redis 2.6.12 it is possible to create a much simpler locking primitive using the SET command to acquire the lock, and a simple Lua script to release the lock. The pattern is documented in the SET command page.

提交回复
热议问题