Révision 5d12d676
Ajouté par Assos Assos il y a environ 6 ans
drupal7/sites/all/modules/views/plugins/views_plugin_cache.inc | ||
---|---|---|
17 | 17 |
* The base plugin to handle caching. |
18 | 18 |
*/ |
19 | 19 |
class views_plugin_cache extends views_plugin { |
20 |
|
|
20 | 21 |
/** |
21 | 22 |
* Contains all data that should be written/read from cache. |
22 | 23 |
*/ |
23 |
var $storage = array();
|
|
24 |
public $storage = array();
|
|
24 | 25 |
|
25 | 26 |
/** |
26 | 27 |
* What table to store data in. |
27 | 28 |
*/ |
28 |
var $table = 'cache_views_data';
|
|
29 |
public $table = 'cache_views_data';
|
|
29 | 30 |
|
30 | 31 |
/** |
31 | 32 |
* Initialize the plugin. |
32 | 33 |
* |
33 |
* @param $view |
|
34 |
* @param view $view
|
|
34 | 35 |
* The view object. |
35 |
* @param $display |
|
36 |
* @param object $display
|
|
36 | 37 |
* The display handler. |
37 | 38 |
*/ |
38 |
function init(&$view, &$display) { |
|
39 |
public function init(&$view, &$display) {
|
|
39 | 40 |
$this->view = &$view; |
40 | 41 |
$this->display = &$display; |
41 | 42 |
|
42 | 43 |
if (is_object($display->handler)) { |
43 | 44 |
$options = $display->handler->get_option('cache'); |
44 |
// Overlay incoming options on top of defaults |
|
45 |
// Overlay incoming options on top of defaults.
|
|
45 | 46 |
$this->unpack_options($this->options, $options); |
46 | 47 |
} |
47 | 48 |
} |
... | ... | |
50 | 51 |
* Return a string to display as the clickable title for the |
51 | 52 |
* access control. |
52 | 53 |
*/ |
53 |
function summary_title() { |
|
54 |
public function summary_title() {
|
|
54 | 55 |
return t('Unknown'); |
55 | 56 |
} |
56 | 57 |
|
... | ... | |
59 | 60 |
* |
60 | 61 |
* Plugins must override this to implement expiration. |
61 | 62 |
* |
62 |
* @param $type |
|
63 |
* @param string $type
|
|
63 | 64 |
* The cache type, either 'query', 'result' or 'output'. |
64 | 65 |
*/ |
65 |
function cache_expire($type) { } |
|
66 |
|
|
67 |
/** |
|
68 |
* Determine expiration time in the cache table of the cache type |
|
69 |
* or CACHE_PERMANENT if item shouldn't be removed automatically from cache. |
|
70 |
* |
|
71 |
* Plugins must override this to implement expiration in the cache table. |
|
72 |
* |
|
73 |
* @param $type |
|
74 |
* The cache type, either 'query', 'result' or 'output'. |
|
75 |
*/ |
|
76 |
function cache_set_expire($type) { |
|
77 |
return CACHE_PERMANENT; |
|
66 |
public function cache_expire($type) { |
|
78 | 67 |
} |
79 | 68 |
|
69 |
/** |
|
70 |
* Determine expiration time in the cache table of the cache type. |
|
71 |
* |
|
72 |
* Or CACHE_PERMANENT if item shouldn't be removed automatically from cache. |
|
73 |
* |
|
74 |
* Plugins must override this to implement expiration in the cache table. |
|
75 |
* |
|
76 |
* @param string $type |
|
77 |
* The cache type, either 'query', 'result' or 'output'. |
|
78 |
*/ |
|
79 |
public function cache_set_expire($type) { |
|
80 |
return CACHE_PERMANENT; |
|
81 |
} |
|
80 | 82 |
|
81 | 83 |
/** |
82 | 84 |
* Save data to the cache. |
83 | 85 |
* |
84 | 86 |
* A plugin should override this to provide specialized caching behavior. |
85 | 87 |
*/ |
86 |
function cache_set($type) { |
|
88 |
public function cache_set($type) {
|
|
87 | 89 |
switch ($type) { |
88 | 90 |
case 'query': |
89 | 91 |
// Not supported currently, but this is certainly where we'd put it. |
90 | 92 |
break; |
93 |
|
|
91 | 94 |
case 'results': |
92 | 95 |
$data = array( |
93 | 96 |
'result' => $this->view->result, |
... | ... | |
96 | 99 |
); |
97 | 100 |
cache_set($this->get_results_key(), $data, $this->table, $this->cache_set_expire($type)); |
98 | 101 |
break; |
102 |
|
|
99 | 103 |
case 'output': |
100 | 104 |
$this->gather_headers(); |
101 | 105 |
$this->storage['output'] = $this->view->display_handler->output; |
... | ... | |
104 | 108 |
} |
105 | 109 |
} |
106 | 110 |
|
107 |
|
|
108 | 111 |
/** |
109 | 112 |
* Retrieve data from the cache. |
110 | 113 |
* |
111 | 114 |
* A plugin should override this to provide specialized caching behavior. |
112 | 115 |
*/ |
113 |
function cache_get($type) { |
|
116 |
public function cache_get($type) {
|
|
114 | 117 |
$cutoff = $this->cache_expire($type); |
115 | 118 |
switch ($type) { |
116 | 119 |
case 'query': |
117 | 120 |
// Not supported currently, but this is certainly where we'd put it. |
118 | 121 |
return FALSE; |
122 |
|
|
119 | 123 |
case 'results': |
120 | 124 |
// Values to set: $view->result, $view->total_rows, $view->execute_time, |
121 | 125 |
// $view->current_page. |
... | ... | |
129 | 133 |
} |
130 | 134 |
} |
131 | 135 |
return FALSE; |
136 |
|
|
132 | 137 |
case 'output': |
133 | 138 |
if ($cache = cache_get($this->get_output_key(), $this->table)) { |
134 | 139 |
if (!$cutoff || $cache->created > $cutoff) { |
... | ... | |
145 | 150 |
/** |
146 | 151 |
* Clear out cached data for a view. |
147 | 152 |
* |
148 |
* We're just going to nuke anything related to the view, regardless of display,
|
|
149 |
* to be sure that we catch everything. Maybe that's a bad idea. |
|
153 |
* We're just going to nuke anything related to the view, regardless of |
|
154 |
* display, to be sure that we catch everything. Maybe that's a bad idea.
|
|
150 | 155 |
*/ |
151 |
function cache_flush() { |
|
156 |
public function cache_flush() {
|
|
152 | 157 |
cache_clear_all($this->view->name . ':', $this->table, TRUE); |
153 | 158 |
} |
154 | 159 |
|
155 | 160 |
/** |
156 | 161 |
* Post process any rendered data. |
157 | 162 |
* |
158 |
* This can be valuable to be able to cache a view and still have some level of
|
|
159 |
* dynamic output. In an ideal world, the actual output will include HTML |
|
163 |
* This can be valuable to be able to cache a view and still have some level |
|
164 |
* of dynamic output. In an ideal world, the actual output will include HTML
|
|
160 | 165 |
* comment based tokens, and then the post process can replace those tokens. |
161 | 166 |
* |
162 | 167 |
* Example usage. If it is known that the view is a node view and that the |
... | ... | |
172 | 177 |
* All of the cached result data will be available in $view->result, as well, |
173 | 178 |
* so all ids used in the query should be discoverable. |
174 | 179 |
*/ |
175 |
function post_render(&$output) { } |
|
180 |
public function post_render(&$output) { |
|
181 |
} |
|
176 | 182 |
|
177 | 183 |
/** |
178 | 184 |
* Start caching javascript, css and other out of band info. |
... | ... | |
181 | 187 |
* duplicate it. Later on, when gather_headers() is run, this information |
182 | 188 |
* will be removed so that we don't hold onto it. |
183 | 189 |
*/ |
184 |
function cache_start() { |
|
190 |
public function cache_start() {
|
|
185 | 191 |
$this->storage['head'] = drupal_add_html_head(); |
186 | 192 |
$this->storage['css'] = drupal_add_css(); |
187 | 193 |
$this->storage['js'] = drupal_add_js(); |
... | ... | |
189 | 195 |
} |
190 | 196 |
|
191 | 197 |
/** |
192 |
* Gather out of band data, compare it to what we started with and store the difference.
|
|
198 |
* Gather out of band data, compare it to the start data and store the diff.
|
|
193 | 199 |
*/ |
194 |
function gather_headers() { |
|
195 |
// Simple replacement for head |
|
200 |
public function gather_headers() {
|
|
201 |
// Simple replacement for head.
|
|
196 | 202 |
if (isset($this->storage['head'])) { |
197 | 203 |
$this->storage['head'] = str_replace($this->storage['head'], '', drupal_add_html_head()); |
198 | 204 |
} |
... | ... | |
203 | 209 |
// Check if the advanced mapping function of D 7.23 is available. |
204 | 210 |
$array_mapping_func = function_exists('drupal_array_diff_assoc_recursive') ? 'drupal_array_diff_assoc_recursive' : 'array_diff_assoc'; |
205 | 211 |
|
206 |
// Slightly less simple for CSS:
|
|
212 |
// Slightly less simple for CSS.
|
|
207 | 213 |
$css = drupal_add_css(); |
208 | 214 |
$css_start = isset($this->storage['css']) ? $this->storage['css'] : array(); |
209 | 215 |
$this->storage['css'] = $this->assetDiff($css, $css_start, $array_mapping_func); |
... | ... | |
259 | 265 |
/** |
260 | 266 |
* Restore out of band data saved to cache. Copied from Panels. |
261 | 267 |
*/ |
262 |
function restore_headers() { |
|
268 |
public function restore_headers() {
|
|
263 | 269 |
if (!empty($this->storage['head'])) { |
264 | 270 |
drupal_add_html_head($this->storage['head']); |
265 | 271 |
} |
... | ... | |
287 | 293 |
} |
288 | 294 |
} |
289 | 295 |
|
290 |
function get_results_key() { |
|
296 |
/** |
|
297 |
* |
|
298 |
*/ |
|
299 |
public function get_results_key() { |
|
291 | 300 |
if (!isset($this->_results_key)) { |
292 | 301 |
$key_data = array(); |
293 | 302 |
foreach (array('exposed_info', 'page', 'sort', 'order', 'items_per_page', 'offset') as $key) { |
... | ... | |
302 | 311 |
return $this->_results_key; |
303 | 312 |
} |
304 | 313 |
|
305 |
function get_output_key() { |
|
314 |
/** |
|
315 |
* |
|
316 |
*/ |
|
317 |
public function get_output_key() { |
|
306 | 318 |
if (!isset($this->_output_key)) { |
307 | 319 |
$key_data = array( |
308 | 320 |
'result' => $this->view->result, |
... | ... | |
323 | 335 |
* |
324 | 336 |
* @return string |
325 | 337 |
*/ |
326 |
function get_cache_key($key_data = array()) { |
|
338 |
public function get_cache_key($key_data = array()) {
|
|
327 | 339 |
global $user; |
328 | 340 |
|
329 | 341 |
$key_data += array( |
330 | 342 |
'roles' => array_keys($user->roles), |
331 |
'super-user' => $user->uid == 1, // special caching for super user. |
|
343 |
'super-user' => $user->uid == 1, |
|
344 |
// special caching for super user. |
|
332 | 345 |
'language' => $GLOBALS['language']->language, |
346 |
'language_content' => $GLOBALS['language_content']->language, |
|
333 | 347 |
'base_url' => $GLOBALS['base_url'], |
334 | 348 |
); |
335 | 349 |
|
336 | 350 |
if (empty($key_data['build_info'])) { |
337 | 351 |
$build_info = $this->view->build_info; |
338 |
foreach (array('query','count_query') as $index) { |
|
352 |
foreach (array('query', 'count_query') as $index) {
|
|
339 | 353 |
// If the default query back-end is used generate SQL query strings from |
340 | 354 |
// the query objects. |
341 | 355 |
if ($build_info[$index] instanceof SelectQueryInterface) { |
... | ... | |
348 | 362 |
} |
349 | 363 |
} |
350 | 364 |
} |
351 |
$key = md5(serialize($key_data)); |
|
352 |
return $key; |
|
365 |
return md5(serialize($key_data)); |
|
353 | 366 |
} |
367 |
|
|
354 | 368 |
} |
355 | 369 |
|
356 | 370 |
/** |
Formats disponibles : Unified diff
Weekly update of contrib modules