leruge / redlock
thinkphp redis悲观锁和乐观锁
1.0.2
2026-02-28 06:26 UTC
Requires
- php: ^8.0
- topthink/framework: ^8.0
This package is auto-updated.
Last update: 2026-02-28 06:27:30 UTC
README
thinkphp8 redis锁,需要添加redis信息到缓存配置文件
安装
composer require leruge/redlock
使用前配置
- 在
config/cache.php中添加redis链接,参考官方文档即可'redis' => [ 'type' => 'redis', 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => '', 'tag_prefix' => 'tag:', 'serialize' => [], 'fail_delete' => false, ]
提供方法
- 获取锁
- 释放锁
获取锁
- 实例化
(new \leruge\Lock())->getLock($lockKey, $requestId, $isNegative = true, $expireTime = 15000)
释放锁
- 实例化
(new \leruge\Lock())->releaseLock($lockKey, $requestId)
参数说明
$lockKey锁标识$requestId锁标识值,建议使用唯一值,比如uuid$isNegative是否悲观锁,默认true$expireTime锁过期时间,默认15000,15秒