msheng/yii2-redlock

Class for easier Redlock integration

1.0.1 2016-08-09 03:15 UTC

This package is not auto-updated.

Last update: 2024-05-11 23:21:03 UTC


README

A redlock for Yii2, distributed locks with Redis

Based on Redlock-rb by Salvatore Sanfilippo and signe redlock-php

Installation

To install run:

    composer require "msheng/yii2-redlock:~1.0.0"

Or add this line to require section of composer.json:

    "msheng/yii2-redlock": "~1.0.0"

project

Your project need to be an yii2-app-advanced , and here is the guide

main.php

In common/config/main.php or main-local.php

<?php
'components' => [
    'redLock' => [
        'class' => 'msheng\RedLock\RedLock',
        'servers' => [
            [
                'hostname' => '127.0.0.1',
                'port' => 6379,
                'timeout' => 0.5,
            ],
            [
                'hostname' => '127.0.0.1',
                'port' => 6389,
                'timeout' => 0.05,
            ],
            [
                'hostname' => '127.0.0.1',
                'port' => 6399,
                'timeout' => 0.05,
            ]
        ]
    ]
],

Usage

<?php
$lock = Yii::$app->redLock->lock('hello');
if($lock){
    sleep(10);
    Yii::$app->redLock->unlock($lock);
}