Giter VIP home page Giter VIP logo

row-locker's Introduction

RowLocker plugin for the CakePHP ORM

This plugin offers a simple implementation of row locking by storing a timestamp in a field of the row and the name of the lock owner.

Row locking can be useful in CMS-like systems where many people try to change the same record at the same time. By locking the row you can prevent or alert the users from possible data overwrite.

Installation

You can install this plugin into your CakePHP application using composer.

composer require lorenzo/row-locker

Note: Above will install package compactible with CakePHP4. Please refer to Versions section to install package with CakePHP3.

And then enable the plugin:

bin/cake plugin load RowLocker

Configuration

Any table to which you wish to apply RowLocker needs to have the following columns:

  • locked_time: DATETIME
  • locked_by (optional) Can be of any type that identify your users (INT, VARCHAR, UUID...)
  • locked_session (optional) VARCHAR(100) Used for debugging purposes

Usage

To use RowLocker you first need to add the LockableInterface and LockableTrait to your entity:

use RowLocker\LockableInterface;
use RowLocker\LockableTrait;
...

class Article extends Entity implements LockableInterface
{
    use LockableTrait;

    ...
}

Finally, add the behavior to your Table class:

class ArticlesTable extends Table
{
    public function initialize()
    {
        ...
        $this->addBehavior('RowLocker.RowLocker');
    }
}

Locking Rows

To lock any row first load it and the call lock() on it. The lock will last for 5 minutes:

$article = $articlesTable->get($id);
$article->lock($userId, $sessionId); // both arguments are actaully optional
$articlesTable->save($article);

RowLocker provides a shortcut for doing the above for one or many rows, by using the autoLock finder:

$article = $articlesTable
    ->findById($id)
    ->find('autoLock', ['lockingUser' => $userId, 'lockingSession' => $sessionId])
    ->firstOrFail(); // This locks the row

$article->isLocked(); // return true

Unlocking a Row

Just call unlock() in the entity:

$article->unlock();
$articlesTable->save($article);

Finding Unlocked Rows

In order to find unlocked rows (or with locks owned by the same user), use the unlocked finder:

$firstUnlocked = $articlesTable
    ->find('unlocked', ['lockingUser' => $userId])
    ->firstOrFail();

Safely Locking Rows

In systems with high concurrency (many users trying to get a lock of the same row), it is highly recommended to use the provided lockingMonitor() function:

$safeLocker = $articlesTable->lockingMonitor();
// Safely lock the row
$safeLocker(function () use ($id, $userId, $sessionId) {
    $article = $articlesTable
        ->findById($id)
        ->find('autoLock', ['lockingUser' => $userId, 'lockingSession' => $sessionId])
        ->firstOrFail();
});

What the locking monitor does is running the inner callable inside a SERIALIZABLE transaction.

Versions

RowLocker has several releases, each compatible with different releases of CakePHP. Use the appropriate version by downloading a tag, or checking out the correct branch.

  • 1.x tags are compatible with CakePHP 3.x and greater.
  • 2.x tags is compatible with CakePHP 4.0.x and is stable to use.

row-locker's People

Contributors

hmic avatar ishan-biztech avatar lorenzo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

row-locker's Issues

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.