Skip to content

Commit d4d6dc2

Browse files
committed
common cache and cli multilanguage fix
1 parent 782cb6e commit d4d6dc2

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

BaseActiveRecord.php

+57-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use webvimark\helpers\LittleBigHelper;
55
use webvimark\helpers\Singleton;
66
use webvimark\image\Image;
7+
use yii\caching\TagDependency;
78
use yii\db\ActiveRecord;
89
use Yii;
910
use yii\db\Query;
@@ -15,6 +16,18 @@
1516

1617
class BaseActiveRecord extends ActiveRecord
1718
{
19+
/**
20+
* For how long store cache in mainCache() function
21+
*/
22+
const COMMON_CACHE_TIME = 2592000; // 1 month
23+
24+
/**
25+
* If true, than afterSave() and afterDelete() cache dependency with static::getCacheTag() will be invalidated
26+
*
27+
* @var bool
28+
*/
29+
protected $_enable_common_cache = false;
30+
1831
// ================= Timestamps config starts =================
1932

2033
/**
@@ -71,7 +84,39 @@ class BaseActiveRecord extends ActiveRecord
7184
'small' => [50, 50]
7285
];
7386

87+
/**
88+
* Used in flush cache after save and delete. So you can always can be sure that your cache is valid
89+
*
90+
* @return string
91+
*/
92+
public static function getCacheTag()
93+
{
94+
return get_called_class() . '_common_cache';
95+
}
7496

97+
/**
98+
* Reset class-specific cache by tag
99+
*/
100+
public static function resetCache()
101+
{
102+
TagDependency::invalidate(Yii::$app->cache, static::getCacheTag());
103+
}
104+
105+
/**
106+
* @param callable $callable
107+
* @param array $tags
108+
*
109+
* @return mixed
110+
*/
111+
public static function mainCache(callable $callable, $tags = [])
112+
{
113+
if ( $tags === [] )
114+
{
115+
$tags = static::getCacheTag();
116+
}
117+
118+
return static::getDb()->cache($callable, static::COMMON_CACHE_TIME, new TagDependency(['tags'=>$tags]));
119+
}
75120

76121
/**
77122
* @inheritdoc
@@ -270,7 +315,7 @@ private function _findI18NAttributes()
270315
{
271316
if ( $this->_i18n_enabled )
272317
{
273-
if ( in_array(Yii::$app->requestedRoute, $this->_i18n_admin_routes) )
318+
if ( php_sapi_name() === 'cli' || in_array(Yii::$app->requestedRoute, $this->_i18n_admin_routes) )
274319
{
275320
$translations = $this->mlGetTranslations();
276321

@@ -398,6 +443,7 @@ private function _getI18NAttributes()
398443
*/
399444
public static function deleteIfExists($condition)
400445
{
446+
/** @var BaseActiveRecord $model */
401447
$model = static::findOne($condition);
402448

403449
if ( $model )
@@ -761,6 +807,11 @@ public function afterDelete()
761807
{
762808
$this->_deleteI18NAttributes();
763809

810+
if ( $this->_enable_common_cache )
811+
{
812+
TagDependency::invalidate(Yii::$app->cache, static::getCacheTag());
813+
}
814+
764815
parent::afterDelete();
765816
}
766817

@@ -771,6 +822,11 @@ public function afterSave($insert, $changedAttributes)
771822
{
772823
$this->_saveI18NAttributes($insert);
773824

825+
if ( $this->_enable_common_cache )
826+
{
827+
TagDependency::invalidate(Yii::$app->cache, static::getCacheTag());
828+
}
829+
774830
parent::afterSave($insert, $changedAttributes);
775831
}
776832

0 commit comments

Comments
 (0)