paysera/lib-lock-bundle

Provides utilities to organize Locks in your system

Installs: 15 183

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 4

Forks: 7

Open Issues: 2

Type:symfony-bundle

2.0.2 2024-04-02 08:10 UTC

This package is auto-updated.

Last update: 2024-11-02 09:25:00 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