PHP code example of webman-tech / symfony-lock

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

    

webman-tech / symfony-lock example snippets




namespace support\facade;

use Symfony\Component\Lock\LockInterface;

/**
 * @method static LockInterface order(?string $orderId = null, ?float $ttl = null, ?bool $autoRelease = null, ?string $prefix = null)
 * @method static LockInterface changeCash(?string $userId = null, ?float $ttl = null, ?bool $autoRelease = null, ?string $prefix = null)
 */
class Locker extends \WebmanTech\SymfonyLock\Locker
{
}



namespace app\controller;

use support\facade\Locker;

class Cash {
    public function changeCash()
    {
        $lock = Locker::cash($currentUserId);
        if (!$lock->acquire()) {
            throw new \Exception('操作太频繁,请稍后再试');
        }
        try {
            // 修改用户金额
        } finally {
            $lock->release();
        }
        
        return 'ok';
    }
}