/
QueryCacheBehavior.php
268 lines (235 loc) · 7.42 KB
/
QueryCacheBehavior.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Generator\Behavior\QueryCache;
use Propel\Generator\Model\Behavior;
/**
* Speeds up queries on a model by caching the query
*
* @author François Zaninotto
*/
class QueryCacheBehavior extends Behavior
{
// default parameters value
protected $parameters = array(
'backend' => 'apc',
'lifetime' => 3600,
);
public function queryAttributes($builder)
{
$script = "protected \$queryKey = '';
";
switch ($this->getParameter('backend')) {
case 'backend':
$script .= "protected static \$cacheBackend = array();
";
break;
case 'apc':
break;
case 'custom':
default:
$script .= "protected static \$cacheBackend;
";
break;
}
return $script;
}
public function queryMethods($builder)
{
$builder->declareClasses('\Propel\Runtime\Propel', '\Propel\Runtime\Util\BasePeer');
$this->peerClassname = $builder->getStubPeerBuilder()->getClassname();
$script = '';
$this->addSetQueryKey($script);
$this->addGetQueryKey($script);
$this->addCacheContains($script);
$this->addCacheFetch($script);
$this->addCacheStore($script);
$this->addDoSelect($script);
$this->addDoCount($script);
return $script;
}
protected function addSetQueryKey(&$script)
{
$script .= "
public function setQueryKey(\$key)
{
\$this->queryKey = \$key;
return \$this;
}
";
}
protected function addGetQueryKey(&$script)
{
$script .= "
public function getQueryKey()
{
return \$this->queryKey;
}
";
}
protected function addCacheContains(&$script)
{
$script .= "
public function cacheContains(\$key)
{";
switch ($this->getParameter('backend')) {
case 'apc':
$script .= "
return apc_fetch(\$key);";
break;
case 'array':
$script .= "
return isset(self::\$cacheBackend[\$key]);";
break;
case 'custom':
default:
$script .= "
throw new PropelException('You must override the cacheContains(), cacheStore(), and cacheFetch() methods to enable query cache');";
break;
}
$script .= "
}
";
}
protected function addCacheStore(&$script)
{
$script .= "
public function cacheStore(\$key, \$value, \$lifetime = " .$this->getParameter('lifetime') . ")
{";
switch ($this->getParameter('backend')) {
case 'apc':
$script .= "
apc_store(\$key, \$value, \$lifetime);";
break;
case 'array':
$script .= "
self::\$cacheBackend[\$key] = \$value;";
break;
case 'custom':
default:
$script .= "
throw new PropelException('You must override the cacheContains(), cacheStore(), and cacheFetch() methods to enable query cache');";
break;
}
$script .= "
}
";
}
protected function addCacheFetch(&$script)
{
$script .= "
public function cacheFetch(\$key)
{";
switch ($this->getParameter('backend')) {
case 'apc':
$script .= "
return apc_fetch(\$key);";
break;
case 'array':
$script .= "
return isset(self::\$cacheBackend[\$key]) ? self::\$cacheBackend[\$key] : null;";
break;
case 'custom':
default:
$script .= "
throw new PropelException('You must override the cacheContains(), cacheStore(), and cacheFetch() methods to enable query cache');";
break;
}
$script .= "
}
";
}
protected function addDoSelect(&$script)
{
$script .= "
protected function doSelect(\$con)
{
// check that the columns of the main class are already added (if this is the primary ModelCriteria)
if (!\$this->hasSelectClause() && !\$this->getPrimaryCriteria()) {
\$this->addSelfSelectColumns();
}
\$this->configureSelectColumns();
\$dbMap = Propel::getServiceContainer()->getDatabaseMap(" . $this->peerClassname ."::DATABASE_NAME);
\$db = Propel::getServiceContainer()->getAdapter(" . $this->peerClassname ."::DATABASE_NAME);
\$key = \$this->getQueryKey();
if (\$key && \$this->cacheContains(\$key)) {
\$params = \$this->getParams();
\$sql = \$this->cacheFetch(\$key);
} else {
\$params = array();
\$sql = BasePeer::createSelectSql(\$this, \$params);
if (\$key) {
\$this->cacheStore(\$key, \$sql);
}
}
try {
\$stmt = \$con->prepare(\$sql);
\$db->bindValues(\$stmt, \$params, \$dbMap);
\$stmt->execute();
} catch (Exception \$e) {
Propel::log(\$e->getMessage(), Propel::LOG_ERR);
throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', \$sql), 0, \$e);
}
return \$stmt;
}
";
}
protected function addDoCount(&$script)
{
$script .= "
protected function doCount(\$con)
{
\$dbMap = Propel::getServiceContainer()->getDatabaseMap(\$this->getDbName());
\$db = Propel::getServiceContainer()->getAdapter(\$this->getDbName());
\$key = \$this->getQueryKey();
if (\$key && \$this->cacheContains(\$key)) {
\$params = \$this->getParams();
\$sql = \$this->cacheFetch(\$key);
} else {
// check that the columns of the main class are already added (if this is the primary ModelCriteria)
if (!\$this->hasSelectClause() && !\$this->getPrimaryCriteria()) {
\$this->addSelfSelectColumns();
}
\$this->configureSelectColumns();
\$needsComplexCount = \$this->getGroupByColumns()
|| \$this->getOffset()
|| \$this->getLimit()
|| \$this->getHaving()
|| in_array(Criteria::DISTINCT, \$this->getSelectModifiers());
\$params = array();
if (\$needsComplexCount) {
if (BasePeer::needsSelectAliases(\$this)) {
if (\$this->getHaving()) {
throw new PropelException('Propel cannot create a COUNT query when using HAVING and duplicate column names in the SELECT part');
}
\$db->turnSelectColumnsToAliases(\$this);
}
\$selectSql = BasePeer::createSelectSql(\$this, \$params);
\$sql = 'SELECT COUNT(*) FROM (' . \$selectSql . ') propelmatch4cnt';
} else {
// Replace SELECT columns with COUNT(*)
\$this->clearSelectColumns()->addSelectColumn('COUNT(*)');
\$sql = BasePeer::createSelectSql(\$this, \$params);
}
if (\$key) {
\$this->cacheStore(\$key, \$sql);
}
}
try {
\$stmt = \$con->prepare(\$sql);
\$db->bindValues(\$stmt, \$params, \$dbMap);
\$stmt->execute();
} catch (Exception \$e) {
Propel::log(\$e->getMessage(), Propel::LOG_ERR);
throw new PropelException(sprintf('Unable to execute COUNT statement [%s]', \$sql), 0, \$e);
}
return \$stmt;
}
";
}
}