Skip to content

Commit

Permalink
Merge branch '1.0.0'
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
  • Loading branch information
titrxw committed Oct 17, 2019
2 parents 80c4016 + 6e0eff6 commit 0496ba5
Show file tree
Hide file tree
Showing 27 changed files with 838 additions and 2,068 deletions.
71 changes: 36 additions & 35 deletions composer.json
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"
]
}
}
}
21 changes: 21 additions & 0 deletions config/model-cache.php
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' => ''
];
83 changes: 83 additions & 0 deletions src/CacheModelProvider.php
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 = [];
});
}
}
187 changes: 0 additions & 187 deletions src/Caches/BatchCache.php

This file was deleted.

Loading

0 comments on commit 0496ba5

Please sign in to comment.