Skip to content

Commit

Permalink
improve caching
Browse files Browse the repository at this point in the history
  • Loading branch information
ZAYEC77 committed Sep 24, 2016
1 parent 5f0dec2 commit 0b26ef0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
13 changes: 10 additions & 3 deletions src/components/BlockManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use nullref\cms\models\Block as BlockModel;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\caching\TagDependency;
use yii\helpers\ArrayHelper;
use Yii;

Expand Down Expand Up @@ -39,14 +40,14 @@ public function register($id, $namespace)
* @param $id
* @param $config
* @return \nullref\cms\components\Widget
* @throws \yii\base\InvalidConfigException
* @throws \yii\base\InvalidConfigException | \Exception
*/
public function getWidget($id, $config = [])
{
/** @var BlockModel $block */
$block = BlockModel::getDb()->cache(function () use ($id) {
return BlockModel::find()->where(['id' => $id])->one();
});
}, null, new TagDependency(['tags' => 'cms.block.' . $id]));
if ($block) {
$config = ArrayHelper::merge($config, $block->getData());
$config['class'] = $this->getList()[$block->class_name] . self::CLASS_WIDGET;
Expand All @@ -56,6 +57,7 @@ public function getWidget($id, $config = [])
'id' => $id,
];
}
/** @var \nullref\cms\components\Widget $widget */
$widget = Yii::createObject($config);
if (method_exists($widget, 'setBlock')) {
/** @var Block $blockObj */
Expand All @@ -67,6 +69,9 @@ public function getWidget($id, $config = [])
return $widget;
}

/**
* @return array
*/
public function getList()
{
return array_merge($this->blocks, $this->_blocks, [
Expand All @@ -81,11 +86,13 @@ public function getList()

/**
* @param $id
* @return \nullref\cms\components\Block
* @param array $params
* @return Block
* @throws \yii\base\InvalidConfigException
*/
public function getBlock($id, $params = [])
{
/** @var \nullref\cms\components\Block $object */
$object = Yii::createObject($this->getList()[$id] . self::CLASS_BLOCK);
Yii::configure($object, $params);
return $object;
Expand Down
6 changes: 6 additions & 0 deletions src/controllers/admin/BlockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use nullref\cms\models\PageHasBlock;
use nullref\core\interfaces\IAdminController;
use Yii;
use yii\caching\TagDependency;
use yii\data\ActiveDataProvider;
use yii\filters\VerbFilter;
use yii\web\Controller;
Expand Down Expand Up @@ -52,6 +53,8 @@ public function actionConfig($id = null)
if ($block->load(Yii::$app->request->post()) && ($block->validate())) {
$model->setData($block);
$model->save();
TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $model->id);
TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $model->oldAttributes['id']);
Yii::$app->session->remove('new-block');

/** Create relation when path page_id parameter */
Expand Down Expand Up @@ -157,6 +160,8 @@ public function actionUpdate($id)

if ($model->load(Yii::$app->request->post()) && $model->save()) {
$pageId = Yii::$app->request->get('page_id');
TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $model->id);
TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $model->oldAttributes['id']);
return $this->redirect(['config', 'id' => $model->id, 'page_id' => $pageId]);
} else {
return $this->render('update', [
Expand All @@ -174,6 +179,7 @@ public function actionUpdate($id)
public function actionDelete($id)
{
$this->findModel($id)->delete();
TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $id);

return $this->redirect(['index']);
}
Expand Down
13 changes: 12 additions & 1 deletion src/controllers/admin/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class PageController extends Controller implements IAdminController
{
/**
* @return array
*/
public function behaviors()
{
return [
Expand Down Expand Up @@ -120,11 +123,19 @@ public function actionUpdate($id)
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
$model = $this->findModel($id);
$model->delete();
TagDependency::invalidate(Yii::$app->cache, 'cms.page.' . $model->route);

return $this->redirect(['index']);
}

/**
* //very WIP
*
* @param $id
* @return string
*/
public function actionWysiwyg($id)
{
$model = $this->findModel($id);
Expand Down
1 change: 1 addition & 0 deletions src/migrations/m000000_000003_create_cms_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function up()
], $this->getTableOptions());

$this->addPrimaryKey('block_PK', $this->tables['block'], 'id');

/**
* Create pageHasBlock table
*/
Expand Down
30 changes: 3 additions & 27 deletions src/models/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,36 +152,12 @@ public function getPages()
return $this->hasMany(Page::className(), ['id' => 'page_id'])->viaTable(PageHasBlock::tableName(), ['block_id' => 'id']);
}

public function isPublic()
{
return $this->visibility === self::VISIBILITY_PUBLIC;
}

/**
* Invalidate cache when update model
* @param bool $insert
* @param array $changedAttributes
* @return bool
*/
public function afterSave($insert, $changedAttributes)
public function isPublic()
{
if (!$insert) {
$cmd = self::find()->where(['id' => $this->id])->createCommand();

$cacheKey = [
Command::className(),
'fetch',
null,
self::getDb()->dsn,
self::getDb()->username,
$cmd->rawSql,
];
if (Yii::$app instanceof Application) {
if (Yii::$app->cache->exists($cacheKey)) {
Yii::$app->cache->delete($cacheKey);
}
}
}
parent::afterSave($insert, $changedAttributes);
return $this->visibility === self::VISIBILITY_PUBLIC;
}

/**
Expand Down

0 comments on commit 0b26ef0

Please sign in to comment.