Skip to content

Commit 4f6975e

Browse files
author
ginnerpeace
committed
Upgrade improve for laravel/lumen all version
1 parent d57750b commit 4f6975e

File tree

6 files changed

+94
-44
lines changed

6 files changed

+94
-44
lines changed

README.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111

1212
### Install
1313
> Using composer.
14-
```bash
15-
# Laravel/Lumen < 5.4
16-
composer require "ginnerpeace/laravel-redis-lock:~1.1"
1714
18-
# Laravel/Lumen >= 5.4
19-
composer require "ginnerpeace/laravel-redis-lock:~2.1"
15+
```bash"
16+
composer require "ginnerpeace/laravel-redis-lock:~2.2"
2017
```
2118

2219
### Add service provider:
2320
> Normally.
21+
2422
```php
2523
<?php
2624
return [
@@ -39,6 +37,7 @@ return [
3937
```
4038

4139
> After Laravel 5.5, the package auto-discovery is supported.
40+
4241
```javascript
4342
{
4443
"providers": [
@@ -51,12 +50,14 @@ return [
5150
```
5251

5352
> Lumen
53+
5454
```php
5555
$app->register(RedisLock\Providers\LumenRedisLockServiceProvider::class);
5656
```
5757

5858
### Publish resources (laravel only)
5959
> Copied config to `config/redislock.php`.
60+
6061
```bash
6162
php artisan vendor:publish --provider="RedisLock\Providers\RedisLockServiceProvider"
6263
```
@@ -107,11 +108,29 @@ $payload = RedisLock::lock('key', 100000);
107108
// Return bool.
108109
RedisLock::unlock($payload);
109110

111+
// Determine a lock if it still effective.
112+
RedisLock::verify($payload);
113+
114+
// Delay a lock if it still effective.
115+
RedisLock::delay($payload);
116+
117+
/////////////////////
118+
// Special usages: //
119+
/////////////////////
120+
110121
// Try 5 times if can't get lock at first hit.
111-
// Default value: 3
112-
RedisLock::setRetry(5)->lock('key', 100000);
122+
// Default value is property: retryCount it from config('redislock.retry_count')
123+
RedisLock::lock('key', 100000, 5);
113124

114125
// Try once only.
115-
RedisLock::lock('key', 100000, false);
126+
RedisLock::lock('key', 100000, 1);
127+
// If value less than 1, will try at least once.
128+
// But not pretty...
129+
// RedisLock::lock('key', 100000, 0);
130+
// RedisLock::lock('key', 100000, -1);
131+
132+
// Change default retry delay.
133+
// Try 10 times & every retry be apart 500 ~ 1000 milliseconds.
134+
RedisLock::setRetryDelay(1000)->lock('key', 100000, 10);
116135

117136
```

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ginnerpeace/laravel-redis-lock",
33
"homepage": "https://github.com/ginnerpeace/laravel-redis-lock",
44
"description": "Simple mutex lock, redis edition.",
5-
"keywords": ["redis", "mutex lock", "laravel"],
5+
"keywords": ["redis", "mutex lock", "laravel", "lumen"],
66
"license": "MIT",
77
"authors": [
88
{
@@ -13,9 +13,9 @@
1313
],
1414
"require": {
1515
"php": "^7.0",
16-
"illuminate/redis": "^5.4",
17-
"illuminate/support": "^5.4",
18-
"predis/predis": "~1.0"
16+
"illuminate/redis": "^5.1",
17+
"illuminate/support": "^5.1",
18+
"predis/predis": "~1.1"
1919
},
2020
"autoload": {
2121
"psr-4": {

src/Processor.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RedisLock;
44

5-
use Predis\Client;
5+
use Predis\ClientInterface;
66
use RedisLock\LuaScripts;
77

88
/**
@@ -25,7 +25,7 @@ class Processor
2525
// Response string from redis cmd: set
2626
const LOCK_SUCCESS = 'OK';
2727

28-
// Response string from redis lua script: eval
28+
// Response int from redis lua script: redis.call('del', KEY)
2929
const UNLOCK_SUCCESS = 1;
3030

3131
// Params for cmd: set
@@ -52,39 +52,34 @@ class Processor
5252
private $expireType;
5353

5454
/**
55-
* How many times do you want to try again.
56-
* (milliseconds)
55+
* Number of retry times.
5756
*
58-
* @var integer
57+
* @var int
5958
*/
60-
private $retryDelay = 200;
59+
private $retryCount = 3;
6160

6261
/**
63-
* Number of retry times.
62+
* How many times do you want to try again.
63+
* (milliseconds)
6464
*
6565
* @var int
6666
*/
67-
private $retryCount = 3;
67+
private $retryDelay = 200;
6868

6969
/**
7070
* This params from service provider.
7171
*
72-
* @param Predis\Client $client
73-
* @param array|null $config
72+
* @param Predis\ClientInterface $client
73+
* @param int $retryCount
74+
* @param int $retryDelay
7475
*/
75-
public function __construct(Client $client, array $config = null)
76+
public function __construct(ClientInterface $client, int $retryCount, int $retryDelay)
7677
{
7778
$this->client = $client;
7879

79-
if (isset($config['retry'])) {
80-
$this->retryCount = $config['retry'];
81-
}
82-
83-
if (isset($config['delay'])) {
84-
$this->retryDelay = $config['delay'];
85-
}
86-
8780
$this->setExpireType(self::EXPIRE_TIME_MILLISECONDS);
81+
$this->setRetryDelay($retryDelay);
82+
$this->retryCount = $retryCount;
8883
}
8984

9085
/**
@@ -101,14 +96,13 @@ public function setExpireType(string $value): self
10196
}
10297

10398
/**
104-
* Set retry number of times.
99+
* Set retry delay time.
105100
*
106-
* @param int $retry
107-
* @return self
101+
* @param int $milliseconds
108102
*/
109-
public function setRetry(int $retry): self
103+
public function setRetryDelay(int $milliseconds): self
110104
{
111-
$this->retryCount = $retry;
105+
$this->retryDelay = $milliseconds;
112106

113107
return $this;
114108
}
@@ -118,17 +112,17 @@ public function setRetry(int $retry): self
118112
*
119113
* @param string $key
120114
* @param int $expire
121-
* @param bool $isWaitingMode
115+
* @param int $retry
122116
* @return array
123117
* - Not empty for getted lock.
124118
* - Empty for lock timeout.
125119
*/
126-
public function lock(string $key, int $expire, bool $isWaitingMode = true): array
120+
public function lock(string $key, int $expire, int $retry = null): array
127121
{
128-
$retry = $isWaitingMode ? $this->retryCount : 0;
122+
$retry = $retry ?? $this->retryCount ?? 0;
129123

130124
while (! $result = $this->hit($key, $expire)) {
131-
if (--$retry < 0) {
125+
if (--$retry < 1) {
132126
return $result;
133127
}
134128

src/Providers/LumenRedisLockServiceProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,20 @@ public function boot()
1212
$this->app->configure('redislock');
1313
$this->mergeConfigFrom(__DIR__ . '/../redislock.config.php', 'redislock');
1414
}
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
protected function versionCompare(string $compareVersion, string $operator)
20+
{
21+
// Lumen (5.8.12) (Laravel Components 5.8.*)
22+
$lumenVersion = $this->app->version();
23+
24+
if (preg_match('/Lumen \((\d\.\d\.\d{1,2})\)( \(Laravel Components (\d\.\d\.\*)\))?/', $lumenVersion, $matches)) {
25+
// Prefer Laravel Components version.
26+
$lumenVersion = isset($matches[3]) ? $matches[3] : $matches[1];
27+
}
28+
29+
return version_compare($lumenVersion, $compareVersion, $operator);
30+
}
1531
}

src/Providers/RedisLockServiceProvider.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ public function register()
2424
{
2525
$this->app->singleton(Processor::class, function($app) {
2626
$config = $app['config']['redislock'];
27+
28+
if ($this->versionCompare('5.4', '>=')) {
29+
$predisClient = $app['redis']->connection($config['connection'])->client();
30+
} else {
31+
$predisClient = $app['redis']->connection($config['connection']);
32+
}
33+
2734
return new Processor(
28-
$app['redis']->connection($config['connection'])->client(),
29-
$config
35+
$predisClient,
36+
$config['retry_count'],
37+
$config['retry_delay']
3038
);
3139
});
3240
}
@@ -46,4 +54,17 @@ public function provides()
4654
{
4755
return [Processor::class];
4856
}
57+
58+
/**
59+
* Compare illuminate component version.
60+
* - illuminate/redis 5.4 has a big upgrade.
61+
*
62+
* @param string $compareVersion
63+
* @param string $operator
64+
* @return bool|null
65+
*/
66+
protected function versionCompare(string $compareVersion, string $operator)
67+
{
68+
return version_compare($this->app->version(), $compareVersion, $operator);
69+
}
4970
}

src/redislock.config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
return [
44
'connection' => 'default',
5-
'retry' => 3,
6-
'delay' => 200,
5+
'retry_count' => 3,
6+
'retry_delay' => 200,
77
];

0 commit comments

Comments
 (0)