-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # composer.json
- Loading branch information
Showing
27 changed files
with
838 additions
and
2,068 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 |
---|---|---|
@@ -1,39 +1,40 @@ | ||
{ | ||
"name": "w7/rangine-cache-model", | ||
"description": "对Laravel框架的Model扩展缓存支持", | ||
"type": "library", | ||
"license": "Apache-2.0", | ||
"authors": [ | ||
{ | ||
"name": "RenChao", | ||
"email": "donknap@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"php": "7.*", | ||
"illuminate/database": "~5.8", | ||
"illuminate/cache": "~5.8", | ||
"illuminate/contracts": "~5.8", | ||
"psr/simple-cache" : "*" | ||
}, | ||
"require-dev": { | ||
"symfony/thanks": "^1.0", | ||
"phpunit/phpunit": "^7.2" | ||
}, | ||
"autoload": { | ||
"name": "w7/rangine-cache-model", | ||
"description": "对Model扩展缓存支持", | ||
"type": "library", | ||
"license": "Apache-2.0", | ||
"authors": [ | ||
{ | ||
"name": "GordenSong", | ||
"email": "94581976@qq.com" | ||
} | ||
], | ||
"require": { | ||
"php": "7.*", | ||
"genealabs/laravel-model-caching": "^0.6.2" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.2", | ||
"w7/php-cs-fixer": "^1.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"W7\\CacheModel\\": "src/" | ||
}, | ||
"classmap": [ | ||
|
||
], | ||
"psr-4": { | ||
"W7\\Laravel\\CacheModel\\": "src/" | ||
}, | ||
"files": [ | ||
"src/helper.php" | ||
] | ||
}, | ||
"src/Traits/Buildable.php" | ||
] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"W7\\Laravel\\CacheModel\\Tests\\": "tests/" | ||
} | ||
} | ||
"psr-4": { | ||
"W7\\CacheModel\\Tests\\": "tests/" | ||
} | ||
}, | ||
"extra": { | ||
"rangine": { | ||
"providers": [ | ||
"W7\\CacheModel\\CacheModelProvider" | ||
] | ||
} | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
/** | ||
* Rangine Model Cache | ||
* | ||
* (c) We7Team 2019 <https://www.rangine.com/> | ||
* | ||
* document http://s.w7.cc/index.php?c=wiki&do=view&id=317&list=2284 | ||
* | ||
* visited https://www.rangine.com/ for more details | ||
*/ | ||
|
||
return [ | ||
'cache-prefix' => '', | ||
|
||
'enabled' => true, | ||
|
||
'use-database-keying' => true, | ||
|
||
'channel' => '' | ||
]; |
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,83 @@ | ||
<?php | ||
|
||
/** | ||
* Rangine Model Cache | ||
* | ||
* (c) We7Team 2019 <https://www.rangine.com/> | ||
* | ||
* document http://s.w7.cc/index.php?c=wiki&do=view&id=317&list=2284 | ||
* | ||
* visited https://www.rangine.com/ for more details | ||
*/ | ||
|
||
namespace W7\CacheModel; | ||
|
||
use Illuminate\Cache\CacheManager; | ||
use Illuminate\Container\Container; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Events\TransactionCommitted; | ||
use Illuminate\Database\Events\TransactionRolledBack; | ||
use W7\Core\Provider\ProviderAbstract; | ||
use Illuminate\Config\Repository; | ||
use W7\CacheModel\Store\CacheStore; | ||
|
||
class CacheModelProvider extends ProviderAbstract { | ||
public function register() { | ||
$this->publishConfig('model-cache.php'); | ||
$this->registerConfig('model-cache.php', 'model-cache'); | ||
$this->registerCommand('model:cache'); | ||
|
||
Container::getInstance()->singleton('config', function () { | ||
$config = $this->config->getUserConfig('model-cache'); | ||
$config['store'] = 'icache'; | ||
|
||
return new Repository([ | ||
'laravel-model-caching' => $config, | ||
'cache.stores.icache' => [ | ||
'driver' => 'icache' | ||
]]); | ||
}); | ||
Container::getInstance()->singleton('db', function () { | ||
return idb(); | ||
}); | ||
Container::getInstance()->singleton('cache', function ($app) { | ||
return new CacheManager($app); | ||
}); | ||
} | ||
|
||
public function boot() { | ||
$config = $this->config->getUserConfig('model-cache'); | ||
Container::getInstance()->make('cache')->extend('icache', function ($app) use ($config) { | ||
return Container::getInstance()->make('cache')->repository( | ||
new CacheStore( | ||
$config['cache-prefix'], | ||
$config['channel'] | ||
) | ||
); | ||
}); | ||
|
||
//放在boot的原因是在boot执行的时候,数据库需要的条件才会准备好 | ||
$this->registerListener(); | ||
} | ||
|
||
private function registerListener() { | ||
//处理在事物中使用缓存问题 | ||
Model::getEventDispatcher()->listen(TransactionCommitted::class, function (TransactionCommitted $instance) { | ||
if ($instance->connection->transactionLevel() !== 0) { | ||
return false; | ||
} | ||
$callbacks = $instance->connection->transCallback; | ||
$instance->connection->transCallback = []; | ||
foreach ((array)$callbacks as $callback) { | ||
is_callable($callback) && $callback(); | ||
} | ||
}); | ||
Model::getEventDispatcher()->listen(TransactionRolledBack::class, function (TransactionRolledBack $instance) { | ||
if ($instance->connection->transactionLevel() !== 0) { | ||
return false; | ||
} | ||
|
||
$instance->connection->transCallback = []; | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.