PHP code example of jzh / lock

1. Go to this page and download the library: Download jzh/lock 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/ */

    

jzh / lock example snippets




namespace mine\facade;

use Symfony\Component\Lock\LockInterface;

/**
 * 业务锁
 * @package mine\facade
 * @method static LockInterface Order(string $key, float $ttl = null, bool $autoRelease = null, string $prefix = null) 创建订单锁
 * @method static LockInterface Payment(string $key, float $ttl = null, bool $autoRelease = null, string $prefix = null) 创建支付锁
 */
class Locker extends \Jzh\Lock\Locker
{

}



namespace app\controller;

use Jzh\Lock\Locker;

class Cash {
    public function changeCash()
    {
        $lock = Locker::lock($key);
        if (!$lock->acquire()) {
            throw new \Exception('操作太频繁,请稍后再试');
        }
        try {
            // 业务逻辑
        } finally {
            $lock->release();
        }
        
        return 'ok';
    }
}