paysera/lib-lock-bundle

Provides utilities to organize Locks in your system

Installs: 7 372

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 4

Forks: 3

Open Issues: 1

Type:symfony-bundle

1.0.2 2021-01-07 09:51 UTC

This package is auto-updated.

Last update: 2021-06-07 10:44:41 UTC


README

Provides quick integration with symfony/lock

Installation

  • Install package
composer require paysera/lib-lock-bundle
  • Enable bundle
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new Paysera\Bundle\LockBundle\PayseraLockBundle(),
        ];

        // ...
    }
}

Configuration

paysera_lock:
    ttl: 5 # integer, optional
    redis_client: # service id, required

ttl - time for locks TTL in seconds, default 5 seconds.

redis_client - service id for any Redis client service supported by symfony/lock

Usage

  • LockManager::createLock($identifier) - creates lock but not acquires it
  • LockManager::acquire($lock) - acquires lock or throws LockAcquiringException on failure
  • LockManager::createAccuired($identifier) - creates acquired lock or throws LockAcquiringException
  • LockManager::release($lock) - releases lock

Example

$lock = $this->lockManager->createLock($identifier);
try {
    $this->lockManager->acquire($lock);
    
    // do something after aquiring lock
} catch (LockAcquiringException $exception) {
    throw new Exception('...');
} finally {
    $lock->release();
}

OR

try {
    $lock = $this->lockManager->createAcquired($identifier);
} catch (LockAcquiringException $exception) {
    throw new Exception('...');
}

// do rest