ivopetkov / lock
Locks library for PHP
Fund package maintenance!
ivopetkov
www.paypal.me/ivopetkovcom
Installs: 12 582
Dependents: 1
Suggesters: 0
Security: 0
Stars: 7
Watchers: 3
Forks: 2
Open Issues: 0
Requires
- php: 7.0.*|7.1.*|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*|8.3.*
Requires (Dev)
- ivopetkov/docs-generator: 0.1.*
README
A simple locking library ... that is based on file locking (and that's awesome).
Why you need it
Imagine you have two tasks that you do not want to overlap (they modify the same data for example). Using this library will bring you peace of mind.
Keep in mind
The library uses file locking mechanism to ensure it works correctly. This means that it can be used only on applications/instances/processes that share a common file system. A website on one server does just that.
Install via Composer
composer require ivopetkov/lock
Usage
Using the following two methods (acquire() and release()) will ensure no two applications/instances/processes execute the code between them.
<?php require 'vendor/autoload.php'; use IvoPetkov\Lock; Lock::acquire('lock1'); // Acquires a lock and pauses other applications/instances/processes until the lock is released. // Do something awesome Lock::release('lock1'); // Releases the acquired lock.
Documentation
Full documentation is available as part of this repository.
Configuration
The default timeout to acquire a lock is 1.5 seconds. You can modify it by calling the following method:
Lock::setDefaultLockTimeout(2.5);
The temporary lock files needed by the library are stored in your OS temp dir. You can modify it by calling the following method:
Lock::setLocksDir('/some/other/temp/dir/');
If multiple applications use this library, you can call the following methods to prefix the keys provided. This way the different applications can use the same keys without interference.
Lock::setKeyPrefix('app1');
License
This project is licensed under the MIT License. See the license file for more information.
Contributing
Feel free to open new issues and contribute to the project. Let's make it awesome and let's do in a positive way.
Author
This library is created and maintained by Ivo Petkov (ivopetkov.com).