RdbLock
in package
A redis lock is just a key with a token.
use mutexes
Essentially, this provides a mechanism to wait until a key is deleted.
If the thread dies or you forget to call release() the object __destroy()
will release the lock. If not, the timeout eventually kicks in.
Inspired by: https://github.com/cheprasov/php-redis-lock
Table of Contents
Properties
Methods
- __destruct() : mixed
- acquire() : self|null
- A new lock!
- extend() : bool
- Extend the timeout for this lock by X seconds.
- isLocked() : bool
- Is the lock acquired?
- release() : void
- Release this lock.
- __construct() : mixed
- createToken() : string
- Something unique so we can figure out the key is truly locked by _this_ lock.
Properties
$key
public
string
$key
$rdb
public
Rdb
$rdb
$token
public
string
$token
Methods
__destruct()
public
__destruct() : mixed
Tags
acquire()
A new lock!
public
static acquire(Rdb $rdb, string $key[, int|float $wait = 0 ][, int|float $timeout = 60 ]) : self|null
This will block for $wait seconds until the lock is released.
It returns an instance if successful, otherwise null.
$lock = $rdb->lock($key, 1);
if ($lock) {
// use resource.
$lock->release();
}
else {
// resource is busy.
}
Parameters
- $rdb : Rdb
- $key : string
- $wait : int|float = 0
-
seconds
- $timeout : int|float = 60
-
seconds
Return values
self|nullextend()
Extend the timeout for this lock by X seconds.
public
extend([int|float $timeout = 60 ]) : bool
Parameters
- $timeout : int|float = 60
Return values
bool —False if expired
isLocked()
Is the lock acquired?
public
isLocked() : bool
This should always be 'true' after obtaining a fresh lock.
Reasons why this could be false:
- you released the lock
- someone deleted the key, flushdb, etc
- the TTL expired
Return values
boolrelease()
Release this lock.
public
release() : void
Call this when you're done and happy.
__construct()
protected
__construct(Rdb $rdb, string $key, string $token) : mixed
Parameters
- $rdb : Rdb
- $key : string
- $token : string
createToken()
Something unique so we can figure out the key is truly locked by _this_ lock.
protected
static createToken() : string