-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathActiveQueryCacheHelper.php
141 lines (124 loc) · 3.72 KB
/
ActiveQueryCacheHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace sitkoru\cache\ar;
use yii\db\ActiveRecord;
use yii\db\Exception;
use yii\db\Query;
/**
* Class ActiveQueryCacheHelper
*
* @package sitkoru\cache\ar
*
*/
class ActiveQueryCacheHelper extends CacheHelper
{
private static $inited = false;
public static $shaCache = '86bda7598e8952af3ca5aa2f23eedc54a5a11414';
public static $shaInvalidate = '8cc3d1f5ba2ec9b0ceee2925dcdf516d67e18d70';
private static $jsonOptions = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR;
/**
*
*/
public static function initialize()
{
if (!self::$inited) {
if (!ActiveQueryCacheHelper::scriptExists(self::$shaCache)) {
$path = __DIR__ . DIRECTORY_SEPARATOR . 'lua' . DIRECTORY_SEPARATOR . 'cache.lua';
ActiveQueryCacheHelper::loadScript($path);
}
if (!ActiveQueryCacheHelper::scriptExists(self::$shaInvalidate)) {
$path = __DIR__ . DIRECTORY_SEPARATOR . 'lua' . DIRECTORY_SEPARATOR . 'invalidate.lua';
ActiveQueryCacheHelper::loadScript($path);
}
self::$inited = true;
}
}
private static $cacheTTL = 7200; //two hours by default
/**
* @param $ttl
*/
public static function setTTL($ttl)
{
self::$cacheTTL = (int)$ttl;
}
/**
* @return int
*/
public static function getTTL()
{
return self::$cacheTTL;
}
/**
* @param $className
* @param $condition
* @param $params
*/
public static function dropCachesForCondition($className, $condition, $params)
{
list($pkName, $results) = self::getModelsToDelete($className, $condition, $params);
if ($results) {
$model = new $className;
foreach ($results as $row) {
$model->$pkName = $row[$pkName];
self::dropCaches($model);
}
}
}
/**
* @param ActiveRecord $className
* @param $condition
* @param $params
*
* @return array
*/
protected static function getModelsToDelete($className, $condition, $params)
{
/**
* @var ActiveRecord $className
*/
$pks = $className::primaryKey();
$pkName = reset($pks);
$query = (new Query())->select($pkName)->from($className::tableName())->where(
$condition,
$params
);
try {
$results = $query->createCommand($className::getDb())->queryAll();
} catch (Exception $ex) {
$results = [];
}
return [$pkName, $results];
}
/**
* @param ActiveRecord $model
* @param array $changedAttributes
*/
public static function dropCaches($model, array $changedAttributes = [])
{
self::initialize();
$attrs = $model->getAttributes();
$changed = [];
if ($changedAttributes) {
$attrNames = array_keys($changedAttributes);
foreach ($attrNames as $attrName) {
if (array_key_exists($attrName, $attrs)) {
$changed[$attrName] = $attrs[$attrName];
}
}
}
$args = [
$model->tableName(),
json_encode($attrs, self::$jsonOptions),
json_encode($changed, self::$jsonOptions)
];
CacheHelper::evalSHA(self::$shaInvalidate, $args, 0);
}
public static function dropCachesForCreateEvent($model, $param = null, $value = null)
{
if ($param) {
$model->$param = $value;
self::dropCaches($model, [$param => $value]);
} else {
self::dropCaches($model);
}
}
}