4
4
use webvimark \helpers \LittleBigHelper ;
5
5
use webvimark \helpers \Singleton ;
6
6
use webvimark \image \Image ;
7
+ use yii \caching \TagDependency ;
7
8
use yii \db \ActiveRecord ;
8
9
use Yii ;
9
10
use yii \db \Query ;
15
16
16
17
class BaseActiveRecord extends ActiveRecord
17
18
{
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
+
18
31
// ================= Timestamps config starts =================
19
32
20
33
/**
@@ -71,7 +84,39 @@ class BaseActiveRecord extends ActiveRecord
71
84
'small ' => [50 , 50 ]
72
85
];
73
86
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
+ }
74
96
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
+ }
75
120
76
121
/**
77
122
* @inheritdoc
@@ -270,7 +315,7 @@ private function _findI18NAttributes()
270
315
{
271
316
if ( $ this ->_i18n_enabled )
272
317
{
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 ) )
274
319
{
275
320
$ translations = $ this ->mlGetTranslations ();
276
321
@@ -398,6 +443,7 @@ private function _getI18NAttributes()
398
443
*/
399
444
public static function deleteIfExists ($ condition )
400
445
{
446
+ /** @var BaseActiveRecord $model */
401
447
$ model = static ::findOne ($ condition );
402
448
403
449
if ( $ model )
@@ -761,6 +807,11 @@ public function afterDelete()
761
807
{
762
808
$ this ->_deleteI18NAttributes ();
763
809
810
+ if ( $ this ->_enable_common_cache )
811
+ {
812
+ TagDependency::invalidate (Yii::$ app ->cache , static ::getCacheTag ());
813
+ }
814
+
764
815
parent ::afterDelete ();
765
816
}
766
817
@@ -771,6 +822,11 @@ public function afterSave($insert, $changedAttributes)
771
822
{
772
823
$ this ->_saveI18NAttributes ($ insert );
773
824
825
+ if ( $ this ->_enable_common_cache )
826
+ {
827
+ TagDependency::invalidate (Yii::$ app ->cache , static ::getCacheTag ());
828
+ }
829
+
774
830
parent ::afterSave ($ insert , $ changedAttributes );
775
831
}
776
832
0 commit comments