forked from symphonists/cacheabledatasource
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.driver.php
327 lines (262 loc) · 12.4 KB
/
extension.driver.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
Class Extension_CacheableDatasource extends Extension {
protected $ttl = 5;
protected $cache;
public function getSubscribedDelegates() {
return array(
array(
'page' => '/frontend/',
'delegate' => 'DataSourcePreExecute',
'callback' => 'dataSourcePreExecute'
),
array(
'page' => '/frontend/',
'delegate' => 'AssociationOutputPostExecute',
'callback' => 'associationOutputPostExecute'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCachingOpportunity',
'callback' => 'addCachingOpportunity'
),
array(
'page' => '/publish/',
'delegate' => 'EntryPreDelete',
'callback' => 'entryPreDelete'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => 'entryPostEdit'
),
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'entryPostCreate'
),
array(
'page' => '/publish/',
'delegate' => 'EntriesPostOrder',
'callback' => 'entriesPostOrder'
),
);
}
// by default it will use ds-datasourcehandle but it can be overwritten for entry-based datasources to be ds-datasourcehandle-entryid
public function getCacheNamespace($datasource){
if (method_exists($datasource,'getCacheNamespace')){
return $datasource->getCacheNamespace();
} else {
return 'ds-'.$this->getDSHandle($datasource);
}
}
public function entryPreDelete($context){
// purge before delete as we will lose section context - might purge unnecessarily if something else blocks the entry from being deleted
$this->purgeCache(current(EntryManager::fetch(current($context['entry_id']))));
}
public function entryPostEdit($context){
$this->purgeCache($context['entry']);
}
public function entryPostCreate($context){
$this->purgeCache($context['entry']);
}
public function entriesPostOrder($context){
//since all entries for ordering are within the same section taking the first one is sufficient
$entry = EntryManager::fetch(current($context['entry_id']));
$this->purgeCache(current($entry));
}
public function purgeCache($entry){
if (!$this->cache) {
$this->cache = Symphony::ExtensionManager()->getCacheProvider('cacheabledatasource');
}
$sectionID = $entry->get('section_id');
//get all datasources check where the seciton is used and purge
$datasources = DatasourceManager::listAll();
foreach ($datasources as $datasource) {
if ($datasource['source'] = $sectionID){
$datasourceHandle = Lang::createHandle($datasource['name']);
// purge cache for this datasource
$this->cache->delete(null,'ds-'.$datasourceHandle);
$this->cache->delete(null,'ds-'.$datasourceHandle.'-'.$entry->get('id'));
//continue no need to check for included associations
continue;
}
foreach ($datasource->dsParamINCLUDEDASSOCIATIONS as $fieldname => $association) {
if ($association['section_id'] == $sectionID){
$datasourceHandle = Lang::createHandle($datasource['name']);
// purge cache for this datasource
$this->cache->delete(null,'ds-'.$datasourceHandle);
// continue iteration of outer loop do not check any more included associations
continue 2;
}
}
}
}
public function addCachingOpportunity($context) {
$current_cache = Symphony::Configuration()->get('cacheabledatasource', 'caching');
$label = Widget::Label(__('Cacheable Datasource'));
$options = array();
foreach($context['available_caches'] as $handle => $cache_name) {
$options[] = array($handle, ($current_cache == $handle || (!isset($current_cache) && $handle === 'database')), $cache_name);
}
$select = Widget::Select('settings[caching][cacheabledatasource]', $options, array('class' => 'picker'));
$label->appendChild($select);
$context['wrapper']->appendChild($label);
}
public function dataSourcePreExecute(&$context) {
// return;
// if (!(Symphony::Author() && Symphony::Author()->isDeveloper())){
// return;
// }
if (!$this->cache) {
$this->cache = Symphony::ExtensionManager()->getCacheProvider('cacheabledatasource');
}
$param_pool = $context['param_pool'];
$datasource = $context['datasource'];
if (!$datasource->dsParamCache){
//no cache time specified so ignore
return;
}
if ($datasource->isForcedEmpty()){
//datasource should not run so don't bother
return;
}
$output = $this->getCachedDSOutput($datasource, $param_pool);
if (!$output) {
// send a blank pool to the ds [should only add it's own into pool]
$output['param_pool'] = array();
$result = $datasource->grab($output['param_pool']);
if (is_object($result)){
$result->setAttribute('status','stale');
}
$cacheResult = false;
if ( is_object($result) ){
$cacheResult = sizeof($result->getChildrenByName('error')) == 0;
if (!($cacheResult)){
//having no results is pretty standard and we should cache a 'no result message' as this is not an unusual error
$cacheResult = $result->getChildByName('error',0)->getValue() == "No records found.";
}
}
$output['xml'] = is_object($result) ? $result->generate(false) : $result;
// $output['xml'] = $result;
if ($cacheResult){
$this->cacheDSOutput(
serialize($output),
$datasource,
$output['param_pool'],
$datasource->dsParamCache
);
}
}
if (!isset($output['param_pool'])){
$output['param_pool'] = array();
}
$datasource->cachedPool = $output['param_pool'];
if ($output['association_output']){
//use flag so association output does not run
$datasource->addedAssociationOutput = true;
}
$output['param_pool'] = array_merge($param_pool,$output['param_pool']);
if (!empty($output['xml'])){
$xmlOutput = is_object($result) ? $result : XMLElement::convertFromXMLString($datasource->dsParamROOTELEMENT,$output['xml']);
// $xmlOutput = is_object($result) ? $result : $output['xml'];
}
$context['xml'] = $xmlOutput;
$context['param_pool'] = $output['param_pool'];
}
/*
* By caching the outputs after the association output has completed,
* the param pool would be cleaned when cached so association output will not run any queries when loaded from cache
*/
public function associationOutputPostExecute(&$context) {
// if (!(Symphony::Author() && Symphony::Author()->isDeveloper())){
// return;
// }
if (!$this->cache) {
$this->cache = Symphony::ExtensionManager()->getCacheProvider('cacheabledatasource');
}
$xml = $context['xml'];
if (is_object($xml)){
$xml->setAttribute('generated-at',date('c'));
}
$param_pool = $context['param_pool'];
$datasource = $context['datasource'];
if (!$datasource->dsParamCache){
//no cache time specified so ignore
return;
}
$output = array('xml'=>is_object($xml) ? $xml->generate(false) : $xml);
//intersect the data of the cached and non cached pools. This will return only the keys which were not removed by the Association Output.
$output['param_pool'] = array_intersect_key($datasource->cachedPool, $param_pool);
$output['association_output'] = true;
$this->cacheDSOutput(
serialize($output),
$datasource,
$datasource->cachedPool, //use the original pool for caching as otherwise cache might not match ?
$datasource->dsParamCache
);
}
protected function getVersion(Datasource $datasource) {
$about = $datasource->about();
$name = Lang::createHandle($about['name']);
$version = $this->cache->read(sprintf("ds/%s/version", $name));
if (!$version) {
$version = 1;
}
return $version;
}
protected function increaseVersion(Datasource $datasource) {
$name = $this->getDSName($datasource);
$version = $this->getVersion($datasource);
$version++;
$this->cache->write(
sprintf("ds/%s/version", $name),
$version,
0
);
return $version;
}
protected function getCachedDSOutput(Datasource $datasource, $param_pool) {
$hash = $this->getHash($datasource, $param_pool);
// $cache = $this->cache->read($hash);
$cache = $this->cache->read($hash,$this->getCacheNamespace($datasource));
if (!is_array($cache)){
return unserialize($cache);
} else if ($cache['expiry'] > time())
return unserialize($cache['data']);
else return false;
}
protected function cacheDSOutput($output, Datasource $datasource, $param_pool, $ttl = null) {
try{
$hash = $this->getHash($datasource, $param_pool);
return $this->cache->write($hash, $output, $ttl,$this->getCacheNamespace($datasource));
}
catch (Exception $e) {
//log error whilst caching however continue with page load as ds should be rendered anyway
Symphony::Log()->writeToLog(__('Error whilst saving datasource cache for %s with hash %s.', array($datasource,$hash)), E_WARNING, true);
return false();
}
}
protected function getHash(Datasource $datasource, $param_pool) {
if (isset($datasource->hash)) {
//if already generated no need to regenerate (eg changing params)
return $datasource->hash;
}
$name = $this->getDSHandle($datasource);
// $version = $this->getVersion($datasource);
$params = array();
foreach (get_class_vars(get_class($datasource)) as $key => $value) {
if (substr($key, 0, 2) == 'ds') {
$params[$key] = $datasource->{$key};
}
}
// $hash = sprintf("ds/%s/%s/%d", $name, md5(serialize($params)), $version);
//temporary due to db limit
$hash = md5('ds' . $name . serialize($params) . $version);
$datasource->hash = $hash;
return $hash;
}
protected function getDSHandle(Datasource $datasource) {
$about = $datasource->about();
return Lang::createHandle($about['name']);
}
}