Giter Club home page Giter Club logo

lock's Introduction

Lock

A simple locking library ... that is based on file locking (and that's awesome).

Build Status Latest Stable Version codecov.io License Codacy Badge

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

List of classes added by the library:

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. This project is released with a Contributor Covenant Code of Conduct. By participating in this project you agree to abide by its terms.

Authors

This library is created and maintained by Ivo Petkov (ivopetkov.com) and some awesome folks.

lock's People

Contributors

ivopetkov avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.