-
Notifications
You must be signed in to change notification settings - Fork 2
/
CachedActiveQuery.php
179 lines (163 loc) · 7.16 KB
/
CachedActiveQuery.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace undefinedor\yii2;
use Yii;
use yii\db\ActiveQuery;
use yii\db\Connection;
/**
* CachedActiveQuery represents a ActiveQuery with cache.
*/
class CachedActiveQuery extends ActiveQuery
{
/**
* @var int $duration the number of seconds that query results can remain valid in the cache. If this is
* not set, the value of [[queryCacheDuration]] will be used instead.
*/
public $duration=null;
/**
* @var \yii\caching\Dependency|array $dependency the cache dependency associated with the cached query results.
*/
public $dependency=null;
/**
* init
*/
public function init()
{
parent::init();
if (is_array($this->dependency)) {
$this->dependency = Yii::createObject($this->dependency);
}
}
/**
* When query caching is available return caching,
* If not,Executes query and generates caching then returns all results .
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
* @return CachedActiveQuery|array|null a single row of query result. Depending on the setting of [[asArray]],
* the query result may be either an array or an CachedActiveQuery object. Null will be returned
* if the query results in nothing.
* @throws \Exception
*/
public function one($db = null)
{
$row = Yii::$app->getDb()->cache(function (Connection $db) {
return parent::one($db);
},$this->duration, $this->dependency);
if (!empty($row)) {
$models = $this->populate([$row]);
return reset($models) ?: null;
} else {
return null;
}
}
/**
* When query caching is available return caching,
* If not,Executes query and generates caching then returns all results as an array.
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
* @return array|CachedActiveQuery[] the query results. If the query results in nothing, an empty array will be returned.
* @throws \Exception
*/
public function all($db = null)
{
return Yii::$app->getDb()->cache(function (Connection $db) {
return parent::all($db);
},$this->duration, $this->dependency);
}
/**
* Returns the query result as a scalar value.
* The value returned will be the first column in the first row of the query results.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return string|boolean the value of the first column in the first row of the query result.
* False is returned if the query result is empty.
*/
public function scalar($db = null)
{
return Yii::$app->getDb()->cache(function (Connection $db) {
return parent::scalar($db);
},$this->duration, $this->dependency);
}
/**
* Returns the number of records.
* @param string $q the COUNT expression. Defaults to '*'.
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given (or null), the `db` application component will be used.
* @return integer|string number of records. The result may be a string depending on the
* underlying database engine and to support integer values higher than a 32bit PHP integer can handle.
*/
public function count($q = '*', $db = null)
{
return Yii::$app->getDb()->cache(function (Connection $db) use ($q) {
return parent::count($q, $db);
},$this->duration, $this->dependency);
}
/**
* Returns the sum of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return mixed the sum of the specified column values.
*/
public function sum($q, $db = null)
{
return Yii::$app->getDb()->cache(function (Connection $db) use ($q) {
return parent::sum($q, $db);
},$this->duration, $this->dependency);
}
/**
* Returns the average of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return mixed the average of the specified column values.
*/
public function average($q, $db = null)
{
return Yii::$app->getDb()->cache(function (Connection $db) use ($q) {
return parent::average($q, $db);
},$this->duration, $this->dependency);
}
/**
* Returns the minimum of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return mixed the minimum of the specified column values.
*/
public function min($q, $db = null)
{
return Yii::$app->getDb()->cache(function (Connection $db) use ($q) {
return parent::min($q, $db);
},$this->duration, $this->dependency);
}
/**
* Returns the maximum of the specified column values.
* @param string $q the column name or expression.
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return mixed the maximum of the specified column values.
*/
public function max($q, $db = null)
{
return Yii::$app->getDb()->cache(function (Connection $db) use ($q) {
return parent::max($q, $db);
},$this->duration, $this->dependency);
}
/**
* Returns a value indicating whether the query result contains any row of data.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return boolean whether the query result contains any row of data.
*/
public function exists($db = null)
{
return Yii::$app->getDb()->cache(function (Connection $db) {
return parent::exists($db);
},$this->duration, $this->dependency);
}
}