Skip to content

Commit

Permalink
缓存使用文件依赖配合发布系统
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavle Lee committed May 6, 2017
1 parent aede09f commit 3d785de
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/Hprose/Yii/DiscoveryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
namespace Hprose\Yii;

use Yii;
use yii\caching\TagDependency;
use yii\caching\Dependency;
use yii\caching\FileDependency;
use yii\helpers\Inflector;
use yii\helpers\Url;
use yii\rest\Controller;
Expand All @@ -30,6 +31,24 @@ class DiscoveryController extends Controller
*/
public $cacheKey = 'rpc-services';

/**
* @var string
*/
public $cacheFilePath = '@runtime/hprose_cache';

/**
* @inheritdoc
*/
public function init()
{
parent::init();

$dependFile = Yii::getAlias($this->cacheFilePath);
if (!file_exists($dependFile)) {
touch($dependFile);
}
}

/**
* @return string
*/
Expand All @@ -40,6 +59,7 @@ public function actionIndex()
$dependency = $this->createDependency();

return Yii::$app->cache->getOrSet($this->cacheKey, function () {
Yii::trace('Dynamic Find');
$services = [];
$commands = $this->getCommandDescriptions();
foreach ($commands as $controller => $actions) {
Expand Down Expand Up @@ -150,30 +170,13 @@ protected function validateControllerClass($controllerClass)
}
}

/**
* 刷新缓存
*/
public function actionFlush()
{
$this->invalidateDependency();
}

/**
* 生成缓存依赖
* @return TagDependency
* @return Dependency
*/
protected function createDependency()
{
return new TagDependency(['tags' => $this->cacheKey]);
}

/**
* @return bool
*/
protected function invalidateDependency()
{
TagDependency::invalidate(Yii::$app->cache, $this->cacheKey);

return true;
$dependFile = Yii::getAlias($this->cacheFilePath);
return new FileDependency(['fileName' => $dependFile]);
}
}

0 comments on commit 3d785de

Please sign in to comment.