-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
迁移项目从Github:https://github.com/Latrell/Lock.git
- Loading branch information
0 parents
commit 5bfcd0c
Showing
15 changed files
with
1,295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/vendor | ||
composer.phar | ||
composer.lock | ||
.DS_Store | ||
/.settings | ||
/.buildpath | ||
/.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Lock | ||
====== | ||
|
||
这是一个支持 Laravel 5 的并发锁拓展包。 | ||
|
||
## 安装 | ||
|
||
``` | ||
composer require latrell/lock dev-master | ||
``` | ||
|
||
使用 ```composer update``` 更新包列表,或使用 ```composer install``` 安装。 | ||
|
||
找到 `config/app.php` 配置文件中的 `providers` 键,注册服务提供者。 | ||
|
||
```php | ||
'providers' => [ | ||
// ... | ||
Latrell\Lock\LockServiceProvider::class, | ||
] | ||
``` | ||
|
||
找到 `config/app.php` 配置文件中的 `aliases` 键,注册别名。 | ||
|
||
```php | ||
'aliases' => [ | ||
// ... | ||
'Lock' => Latrell\Lock\Facades\Lock::class, | ||
] | ||
``` | ||
|
||
运行 `php artisan vendor:publish` 命令,发布配置文件到你的项目中。 | ||
|
||
## 使用 | ||
|
||
```php | ||
// 防止商品超卖。 | ||
$key = 'Goods:' . $goods_id; | ||
Lock::granule($key, function() use($goods_id) { | ||
$goods = Goods::find($goods_id); | ||
if ( $goods->stock > 0 ) { | ||
// ... | ||
} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "latrell/lock", | ||
"description": "支持Redis与Memcached的并发锁。", | ||
"keywords": ["Lock", "L5", "Laravel 5", "Laravel"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Latrell Chan", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.5.9" | ||
}, | ||
"require-dev": { | ||
"illuminate/support": "5.*" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"Latrell\\Lock\\": "src/" | ||
} | ||
}, | ||
"minimum-stability": "stable" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
namespace Latrell\Lock\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use Latrell\Lock\LockManager; | ||
|
||
class ClearCommand extends Command | ||
{ | ||
|
||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'lock:clear {--timeout=300 : 超时时间,默认五分钟。} {--store= : 清理的仓库名称。}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = '清理过期的死锁。'; | ||
|
||
/** | ||
* The lock manager instance. | ||
* | ||
* @var \Latrell\Lock\LockManager | ||
*/ | ||
protected $lock; | ||
|
||
/** | ||
* Create a new lock clear command instance. | ||
* | ||
* @param \Latrell\Lock\LockManager $lock | ||
* @return void | ||
*/ | ||
public function __construct(LockManager $lock) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->lock = $lock; | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
$timeout = $this->option('timeout'); | ||
$store_name = $this->option('store'); | ||
|
||
$this->laravel['events']->fire('lock:clearing', [ | ||
$store_name | ||
]); | ||
|
||
$num = $this->lock->store($store_name)->clear($timeout); | ||
|
||
$this->laravel['events']->fire('lock:cleared', [ | ||
$store_name | ||
]); | ||
|
||
$this->info("cleared {$num} lock!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
namespace Latrell\Lock\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class Lock extends Facade | ||
{ | ||
|
||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'lock'; | ||
} | ||
} |
Oops, something went wrong.