PHP code example of texthtml / php-lock-redis

1. Go to this page and download the library: Download texthtml/php-lock-redis library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

texthtml / php-lock-redis example snippets


use TH\RedisLock\RedisSimpleLockFactory;

$redisClient = new \Predis\Client;
$factory = new RedisSimpleLockFactory($redisClient);
$lock = $factory->create('lock identifier');

$lock->acquire();

// other processes that try to acquire a lock on 'lock identifier' will fail

// do some stuff

$lock->release();

// other processes can now acquire a lock on 'lock identifier'

use TH\RedisLock\RedisSimpleLockFactory;

function batch() {
    $redisClient = new \Predis\Client;
    $factory = new RedisSimpleLockFactory($redisClient);
    $lock = $factory->create('lock identifier');
    $lock->acquire();

    // lot of stuff
}

batch();

// the lock will be released here even if $lock->release() is not called in batch()
bash
composer