neovg/phpredis-lock

Simple mutex locking class with support for TTL using PHPRedis as backend.

1.0.4 2021-12-03 17:18 UTC

This package is auto-updated.

Last update: 2024-03-29 04:19:20 UTC


README

Simple mutex locking class with support for TTL using PHPRedis as backend.

Installation

composer require neovg/phpredis-lock

Setup

\NeoVg\PhpRedisLock\Lock::getInstance()->setConfig(
    (new \NeoVg\PhpRedisLock\ConfigStruct())
        ->withHost('127.0.0.1')
        ->withPort(6379)
        ->withDatabase(11)
);

Usage

Acquire and release

if (!\NeoVg\PhpRedisLock\Lock::getInstance()->acquire('name')) {
    echo 'could not acquire lock';
}

if (!\NeoVg\PhpRedisLock\Lock::getInstance()->release('name')) {
    echo 'could not release lock';
}

Non blocking acquire

\NeoVg\PhpRedisLock\Lock::getInstance()->acquire('name', 0);

Custom wait time (120 seconds)

\NeoVg\PhpRedisLock\Lock::getInstance()->acquire('name', 120);

TTL (60 seconds)

\NeoVg\PhpRedisLock\Lock::getInstance()->acquire('name', null, 60);

Check if already acquired (by this process)

\NeoVg\PhpRedisLock\Lock::getInstance()->isAcquired('name');

Check if already locked (by another process)

\NeoVg\PhpRedisLock\Lock::getInstance()->isLocked('name');

Get info about existing lock

$lockInfo = \NeoVg\PhpRedisLock\Lock::getInstance()->get('name;);