Skip to content

Commit 0b26ef0

Browse files
committed
improve caching
1 parent 5f0dec2 commit 0b26ef0

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

src/components/BlockManager.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use nullref\cms\models\Block as BlockModel;
66
use yii\base\Component;
77
use yii\base\InvalidConfigException;
8+
use yii\caching\TagDependency;
89
use yii\helpers\ArrayHelper;
910
use Yii;
1011

@@ -39,14 +40,14 @@ public function register($id, $namespace)
3940
* @param $id
4041
* @param $config
4142
* @return \nullref\cms\components\Widget
42-
* @throws \yii\base\InvalidConfigException
43+
* @throws \yii\base\InvalidConfigException | \Exception
4344
*/
4445
public function getWidget($id, $config = [])
4546
{
4647
/** @var BlockModel $block */
4748
$block = BlockModel::getDb()->cache(function () use ($id) {
4849
return BlockModel::find()->where(['id' => $id])->one();
49-
});
50+
}, null, new TagDependency(['tags' => 'cms.block.' . $id]));
5051
if ($block) {
5152
$config = ArrayHelper::merge($config, $block->getData());
5253
$config['class'] = $this->getList()[$block->class_name] . self::CLASS_WIDGET;
@@ -56,6 +57,7 @@ public function getWidget($id, $config = [])
5657
'id' => $id,
5758
];
5859
}
60+
/** @var \nullref\cms\components\Widget $widget */
5961
$widget = Yii::createObject($config);
6062
if (method_exists($widget, 'setBlock')) {
6163
/** @var Block $blockObj */
@@ -67,6 +69,9 @@ public function getWidget($id, $config = [])
6769
return $widget;
6870
}
6971

72+
/**
73+
* @return array
74+
*/
7075
public function getList()
7176
{
7277
return array_merge($this->blocks, $this->_blocks, [
@@ -81,11 +86,13 @@ public function getList()
8186

8287
/**
8388
* @param $id
84-
* @return \nullref\cms\components\Block
89+
* @param array $params
90+
* @return Block
8591
* @throws \yii\base\InvalidConfigException
8692
*/
8793
public function getBlock($id, $params = [])
8894
{
95+
/** @var \nullref\cms\components\Block $object */
8996
$object = Yii::createObject($this->getList()[$id] . self::CLASS_BLOCK);
9097
Yii::configure($object, $params);
9198
return $object;

src/controllers/admin/BlockController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use nullref\cms\models\PageHasBlock;
77
use nullref\core\interfaces\IAdminController;
88
use Yii;
9+
use yii\caching\TagDependency;
910
use yii\data\ActiveDataProvider;
1011
use yii\filters\VerbFilter;
1112
use yii\web\Controller;
@@ -52,6 +53,8 @@ public function actionConfig($id = null)
5253
if ($block->load(Yii::$app->request->post()) && ($block->validate())) {
5354
$model->setData($block);
5455
$model->save();
56+
TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $model->id);
57+
TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $model->oldAttributes['id']);
5558
Yii::$app->session->remove('new-block');
5659

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

158161
if ($model->load(Yii::$app->request->post()) && $model->save()) {
159162
$pageId = Yii::$app->request->get('page_id');
163+
TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $model->id);
164+
TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $model->oldAttributes['id']);
160165
return $this->redirect(['config', 'id' => $model->id, 'page_id' => $pageId]);
161166
} else {
162167
return $this->render('update', [
@@ -174,6 +179,7 @@ public function actionUpdate($id)
174179
public function actionDelete($id)
175180
{
176181
$this->findModel($id)->delete();
182+
TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $id);
177183

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

src/controllers/admin/PageController.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
class PageController extends Controller implements IAdminController
1818
{
19+
/**
20+
* @return array
21+
*/
1922
public function behaviors()
2023
{
2124
return [
@@ -120,11 +123,19 @@ public function actionUpdate($id)
120123
*/
121124
public function actionDelete($id)
122125
{
123-
$this->findModel($id)->delete();
126+
$model = $this->findModel($id);
127+
$model->delete();
128+
TagDependency::invalidate(Yii::$app->cache, 'cms.page.' . $model->route);
124129

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

133+
/**
134+
* //very WIP
135+
*
136+
* @param $id
137+
* @return string
138+
*/
128139
public function actionWysiwyg($id)
129140
{
130141
$model = $this->findModel($id);

src/migrations/m000000_000003_create_cms_tables.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function up()
5050
], $this->getTableOptions());
5151

5252
$this->addPrimaryKey('block_PK', $this->tables['block'], 'id');
53+
5354
/**
5455
* Create pageHasBlock table
5556
*/

src/models/Block.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -152,36 +152,12 @@ public function getPages()
152152
return $this->hasMany(Page::className(), ['id' => 'page_id'])->viaTable(PageHasBlock::tableName(), ['block_id' => 'id']);
153153
}
154154

155-
public function isPublic()
156-
{
157-
return $this->visibility === self::VISIBILITY_PUBLIC;
158-
}
159-
160155
/**
161-
* Invalidate cache when update model
162-
* @param bool $insert
163-
* @param array $changedAttributes
156+
* @return bool
164157
*/
165-
public function afterSave($insert, $changedAttributes)
158+
public function isPublic()
166159
{
167-
if (!$insert) {
168-
$cmd = self::find()->where(['id' => $this->id])->createCommand();
169-
170-
$cacheKey = [
171-
Command::className(),
172-
'fetch',
173-
null,
174-
self::getDb()->dsn,
175-
self::getDb()->username,
176-
$cmd->rawSql,
177-
];
178-
if (Yii::$app instanceof Application) {
179-
if (Yii::$app->cache->exists($cacheKey)) {
180-
Yii::$app->cache->delete($cacheKey);
181-
}
182-
}
183-
}
184-
parent::afterSave($insert, $changedAttributes);
160+
return $this->visibility === self::VISIBILITY_PUBLIC;
185161
}
186162

187163
/**

0 commit comments

Comments
 (0)