Documentation

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

$key  : string
$rdb  : Rdb
$token  : string

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

Methods

__destruct()

public __destruct() : mixed
Tags
inheritdoc

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|null

extend()

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
bool

release()

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
Return values
string
On this page

Search results