simple-as-fuck/laravel-lock

There is no license information available for the latest version (0.2.15) of this package.

Laravel integration for symfony/lock.

0.2.15 2025-03-09 15:21 UTC

README

Laravel integration for symfony/lock.

Installation

composer require simple-as-fuck/laravel-lock

Support

If any PHP platform requirements in composer.json ends with security support, consider package version as unsupported except last version.

PHP supported versions.

Configuration

Add into your .env_example and configure your environment on server.

LOCK_STORE=semaphore
LOCK_PREFIX=null
#LOCK_PGSQL_STORE_CONNECTION=null

Supported symfony lock store are only with native blocking lock, because it is fucking effective.

  • LOCK_STORE=semaphore SemaphoreStore recommended for simple production without application server replication (lock are stored in local ram)

  • LOCK_STORE=flock FlockStore recommended for local development, (lock are stored in local filesystem, so it should work everywhere)

  • LOCK_STORE=pgsql PostgreSqlStore recommended for big production with application server replication (lock are stored remotely by postgres database), you can use special database for locks using setting laravel database connection name LOCK_PGSQL_STORE_CONNECTION=some_postgre_sql_connection_name, by default is used default database connection

  • LOCK_PREFIX=null package will use 'app.name' config value as lock keys prefix

  • LOCK_PREFIX=empty or LOCK_PREFIX="" env sets prefix to empty string, simply turn off lock keys prefixing

  • LOCK_PREFIX="some_prefix" env sets custom prefix and override prefix from application name

⚠ Changing lock configuration is dangerous operation. ⚠

If you need change lock store, you can use environment variables with OLD_ prefix.

OLD_LOCK_STORE=semaphore
OLD_LOCK_PREFIX=null
#OLD_LOCK_PGSQL_STORE_CONNECTION=null
  • For save configuration change copy your current lock configuration and prefix all environment keys with OLD_ prefix, change basic environment variables with usage of new lock configuration.
  • Keep your application running with both configurations for a while.
  • After all processes with unchanged configuration ends or dies, remove all environment keys with OLD_ prefix.

⚠ If your change of multiple environment variables is not atomic operation, you should change variables in specific order. First create variables with OLD_ prefix, OLD_LOCK_STORE create as last one, second if you need, prepare specific variables for specific store, third if you need, change LOCK_PREFIX at last if you need, change LOCK_STORE. While you cleaning OLD_ prefix, remove OLD_LOCK_STORE as first.

Is not recommended run application with old configuration for long time because locking with old store is less effective.

Usage

/** @var \SimpleAsFuck\LaravelLock\Service\LockManager $lockManager */
$lockManager = app()->make(\SimpleAsFuck\LaravelLock\Service\LockManager::class);

$lock = $lockManager->acquire('some_lock_key');
try {
    //happy run some critical code synchronously
} finally {
    $lock->release();
}