Skip to content

Commit

Permalink
迁移项目从Github:https://github.com/Latrell/Lock.git
Browse files Browse the repository at this point in the history
  • Loading branch information
latrell committed Apr 5, 2016
0 parents commit 5bfcd0c
Show file tree
Hide file tree
Showing 15 changed files with 1,295 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/vendor
composer.phar
composer.lock
.DS_Store
/.settings
/.buildpath
/.project
45 changes: 45 additions & 0 deletions README.md
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 ) {
// ...
}
});
```
24 changes: 24 additions & 0 deletions composer.json
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"
}
18 changes: 18 additions & 0 deletions phpunit.xml
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>
66 changes: 66 additions & 0 deletions src/Latrell/Lock/Console/ClearCommand.php
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!");
}
}
18 changes: 18 additions & 0 deletions src/Latrell/Lock/Facades/Lock.php
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';
}
}
Loading

0 comments on commit 5bfcd0c

Please sign in to comment.