views_plugin_cache.inc
9.52 KB
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
<?php
/**
* @file
* Definition of views_plugin_cache.
*/
/**
* @defgroup views_cache_plugins Views cache plugins
* @{
* @todo.
*
* @see hook_views_plugins()
*/
/**
* The base plugin to handle caching.
*/
class views_plugin_cache extends views_plugin {
/**
* Contains all data that should be written/read from cache.
*/
var $storage = array();
/**
* What table to store data in.
*/
var $table = 'cache_views_data';
/**
* Initialize the plugin.
*
* @param $view
* The view object.
* @param $display
* The display handler.
*/
function init(&$view, &$display) {
$this->view = &$view;
$this->display = &$display;
if (is_object($display->handler)) {
$options = $display->handler->get_option('cache');
// Overlay incoming options on top of defaults
$this->unpack_options($this->options, $options);
}
}
/**
* Return a string to display as the clickable title for the
* access control.
*/
function summary_title() {
return t('Unknown');
}
/**
* Determine the expiration time of the cache type, or NULL if no expire.
*
* Plugins must override this to implement expiration.
*
* @param $type
* The cache type, either 'query', 'result' or 'output'.
*/
function cache_expire($type) { }
/**
* Determine expiration time in the cache table of the cache type
* or CACHE_PERMANENT if item shouldn't be removed automatically from cache.
*
* Plugins must override this to implement expiration in the cache table.
*
* @param $type
* The cache type, either 'query', 'result' or 'output'.
*/
function cache_set_expire($type) {
return CACHE_PERMANENT;
}
/**
* Save data to the cache.
*
* A plugin should override this to provide specialized caching behavior.
*/
function cache_set($type) {
switch ($type) {
case 'query':
// Not supported currently, but this is certainly where we'd put it.
break;
case 'results':
$data = array(
'result' => $this->view->result,
'total_rows' => isset($this->view->total_rows) ? $this->view->total_rows : 0,
'current_page' => $this->view->get_current_page(),
);
cache_set($this->get_results_key(), $data, $this->table, $this->cache_set_expire($type));
break;
case 'output':
$this->gather_headers();
$this->storage['output'] = $this->view->display_handler->output;
cache_set($this->get_output_key(), $this->storage, $this->table, $this->cache_set_expire($type));
break;
}
}
/**
* Retrieve data from the cache.
*
* A plugin should override this to provide specialized caching behavior.
*/
function cache_get($type) {
$cutoff = $this->cache_expire($type);
switch ($type) {
case 'query':
// Not supported currently, but this is certainly where we'd put it.
return FALSE;
case 'results':
// Values to set: $view->result, $view->total_rows, $view->execute_time,
// $view->current_page.
if ($cache = cache_get($this->get_results_key(), $this->table)) {
if (!$cutoff || $cache->created > $cutoff) {
$this->view->result = $cache->data['result'];
$this->view->total_rows = $cache->data['total_rows'];
$this->view->set_current_page($cache->data['current_page']);
$this->view->execute_time = 0;
return TRUE;
}
}
return FALSE;
case 'output':
if ($cache = cache_get($this->get_output_key(), $this->table)) {
if (!$cutoff || $cache->created > $cutoff) {
$this->storage = $cache->data;
$this->view->display_handler->output = $cache->data['output'];
$this->restore_headers();
return TRUE;
}
}
return FALSE;
}
}
/**
* Clear out cached data for a view.
*
* We're just going to nuke anything related to the view, regardless of display,
* to be sure that we catch everything. Maybe that's a bad idea.
*/
function cache_flush() {
cache_clear_all($this->view->name . ':', $this->table, TRUE);
}
/**
* Post process any rendered data.
*
* This can be valuable to be able to cache a view and still have some level of
* dynamic output. In an ideal world, the actual output will include HTML
* comment based tokens, and then the post process can replace those tokens.
*
* Example usage. If it is known that the view is a node view and that the
* primary field will be a nid, you can do something like this:
*
* <!--post-FIELD-NID-->
*
* And then in the post render, create an array with the text that should
* go there:
*
* strtr($output, array('<!--post-FIELD-1-->', 'output for FIELD of nid 1');
*
* All of the cached result data will be available in $view->result, as well,
* so all ids used in the query should be discoverable.
*/
function post_render(&$output) { }
/**
* Start caching javascript, css and other out of band info.
*
* This takes a snapshot of the current system state so that we don't
* duplicate it. Later on, when gather_headers() is run, this information
* will be removed so that we don't hold onto it.
*/
function cache_start() {
$this->storage['head'] = drupal_add_html_head();
$this->storage['css'] = drupal_add_css();
$this->storage['js'] = drupal_add_js();
$this->storage['headers'] = drupal_get_http_header();
}
/**
* Gather out of band data, compare it to what we started with and store the difference.
*/
function gather_headers() {
// Simple replacement for head
if (isset($this->storage['head'])) {
$this->storage['head'] = str_replace($this->storage['head'], '', drupal_add_html_head());
}
else {
$this->storage['head'] = '';
}
// Check if the advanced mapping function of D 7.23 is available.
$array_mapping_func = function_exists('drupal_array_diff_assoc_recursive') ? 'drupal_array_diff_assoc_recursive' : 'array_diff_assoc';
// Slightly less simple for CSS:
$css = drupal_add_css();
$css_start = isset($this->storage['css']) ? $this->storage['css'] : array();
$this->storage['css'] = $array_mapping_func($css, $css_start);
// Get javascript after/before views renders.
$js = drupal_add_js();
$js_start = isset($this->storage['js']) ? $this->storage['js'] : array();
// If there are any differences between the old and the new javascript then
// store them to be added later.
$this->storage['js'] = $array_mapping_func($js, $js_start);
// Special case the settings key and get the difference of the data.
$settings = isset($js['settings']['data']) ? $js['settings']['data'] : array();
$settings_start = isset($js_start['settings']['data']) ? $js_start['settings']['data'] : array();
$this->storage['js']['settings'] = $array_mapping_func($settings, $settings_start);
// Get difference of HTTP headers.
$this->storage['headers'] = $array_mapping_func(drupal_get_http_header(), $this->storage['headers']);
}
/**
* Restore out of band data saved to cache. Copied from Panels.
*/
function restore_headers() {
if (!empty($this->storage['head'])) {
drupal_add_html_head($this->storage['head']);
}
if (!empty($this->storage['css'])) {
foreach ($this->storage['css'] as $args) {
drupal_add_css($args['data'], $args);
}
}
if (!empty($this->storage['js'])) {
foreach ($this->storage['js'] as $key => $args) {
if ($key !== 'settings') {
drupal_add_js($args['data'], $args);
}
else {
foreach ($args as $setting) {
drupal_add_js($setting, 'setting');
}
}
}
}
if (!empty($this->storage['headers'])) {
foreach ($this->storage['headers'] as $name => $value) {
drupal_add_http_header($name, $value);
}
}
}
function get_results_key() {
if (!isset($this->_results_key)) {
$this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . $this->get_cache_key();
}
return $this->_results_key;
}
function get_output_key() {
if (!isset($this->_output_key)) {
$key_data = array(
'theme' => $GLOBALS['theme'],
);
$this->_output_key = $this->view->name . ':' . $this->display->id . ':output:' . $this->get_cache_key($key_data);
}
return $this->_output_key;
}
/**
* Returns cache key.
*
* @param array $key_data
* Additional data for cache segmentation and/or overrides for default
* segmentation.
*
* @return string
*/
function get_cache_key($key_data = array()) {
global $user;
$key_data += array(
'roles' => array_keys($user->roles),
'super-user' => $user->uid == 1, // special caching for super user.
'language' => $GLOBALS['language']->language,
'base_url' => $GLOBALS['base_url'],
);
if (empty($key_data['build_info'])) {
$build_info = $this->view->build_info;
foreach (array('query','count_query') as $index) {
// If the default query back-end is used generate SQL query strings from
// the query objects.
if ($build_info[$index] instanceof SelectQueryInterface) {
$query = clone $build_info[$index];
$query->preExecute();
$key_data['build_info'][$index] = array(
'sql' => (string) $query,
'arguments' => $query->getArguments(),
);
}
}
}
$key = md5(serialize($key_data));
return $key;
}
}
/**
* @}
*/