paysera / lib-lock-bundle
提供用于组织系统中的锁的实用工具
2.0.2
2024-04-02 08:10 UTC
Requires
- php: >=7.1
- symfony/config: ^3.0 || ^4.0 || ^5.0
- symfony/console: ^4.4 || ^5.3 || ^6.0
- symfony/dependency-injection: ^3.0 || ^4.0 || ^5.0
- symfony/http-kernel: ^3.0 || ^4.0 || ^5.0
- symfony/lock: ^v3.4 || ^4.4 || ^5.4
- symfony/yaml: ^4.0 || ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^6.0 || ^7.0 || ^8.0
- predis/predis: ^1.1
- snc/redis-bundle: ^2.1 || ^3.0 || ^4.0
Suggests
- predis/predis: To store locks in Redis
This package is auto-updated.
Last update: 2024-09-02 08:59:08 UTC
README
提供与symfony/lock
的快速集成
安装
- 安装包
composer require paysera/lib-lock-bundle
- 启用包
class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new Paysera\Bundle\LockBundle\PayseraLockBundle(), ]; // ... } }
配置
paysera_lock: ttl: 5 # integer, optional redis_client: # service id, required
ttl
- 锁的TTL时间(秒),默认5秒。
redis_client
- 由symfony/lock支持的任何Redis
客户端服务ID
使用方法
LockManager::createLock($identifier)
- 创建锁但不会获取它LockManager::acquire($lock)
- 获取锁或在失败时抛出LockAcquiringException
LockManager::createAcquired($identifier)
- 创建已获取的锁或在失败时抛出LockAcquiringException
LockManager::release($lock)
- 释放锁
示例
$lock = $this->lockManager->createLock($identifier); try { $this->lockManager->acquire($lock); // do something after aquiring lock } catch (LockAcquiringException $exception) { throw new Exception('...'); } finally { $lock->release(); }
或者
try { $lock = $this->lockManager->createAcquired($identifier); } catch (LockAcquiringException $exception) { throw new Exception('...'); } // do rest