awheel / redis-lock
基于 Redis 实现的并发防穿透锁
0.1.2
2019-08-07 02:52 UTC
Requires
- ext-redis: *
This package is auto-updated.
Last update: 2024-10-29 03:48:24 UTC
README
Redis 锁
使用
- 继承
RedisLockAbstract
抽象类 - 实现
getInstance
方法, 且getInstance
方法必须返回一个标准的可用的Redis
实例
示例:
<?php
namespace awheel\RedisLock;
require __DIR__.'/../src/RedisLockAbstract.php';
use Redis;
class RedisLock extends RedisLockAbstract
{
/**
* 获取 Redis 对象
*
* @return Redis
*/
static public function getInstance()
{
self::$redis = new Redis();
self::$redis->connect('127.0.0.1');
return self::$redis;
}
}
$key = 'redis_lock_test';
if (RedisLock::lock($key)) {
printf("locked\n");
// do something
RedisLock::renew($key, 5);
$unlock = RedisLock::unlock($key);
$unlock && printf("unlocked\n");
}