/[drupal]/contributions/modules/views/plugins/views_plugin_cache.inc
ViewVC logotype

Contents of /contributions/modules/views/plugins/views_plugin_cache.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Wed Jun 24 02:45:01 2009 UTC (5 months ago) by merlinofchaos
Branch: MAIN
CVS Tags: DRUPAL-6--2-7, HEAD
Branch point for: DRUPAL-6--2, DRUPAL-7--3
Changes since 1.4: +3 -1 lines
File MIME type: text/x-php
Make sure uid 1 cache does not combine with other user caches since permissions differ from uid 1 to other users.
1 <?php
2 // $Id: views_plugin_cache.inc,v 1.4 2009/06/17 19:57:03 merlinofchaos Exp $
3
4 /**
5 * The base plugin to handle caching.
6 *
7 * @ingroup views_cache_plugins
8 */
9 class views_plugin_cache extends views_plugin {
10 /**
11 * Contains all data that should be written/read from cache.
12 */
13 var $storage = array();
14
15 /**
16 * What table to store data in.
17 */
18 var $table = 'cache_views_data';
19
20 /**
21 * Initialize the plugin.
22 *
23 * @param $view
24 * The view object.
25 * @param $display
26 * The display handler.
27 */
28 function init(&$view, &$display) {
29 $this->view = &$view;
30 $this->display = &$display;
31 $this->options = array();
32
33 if (is_object($display->handler)) {
34 // Note: The below is read only.
35 $this->options = $display->handler->get_option('cache');
36 }
37 }
38
39 /**
40 * Retrieve the default options when this is a new access
41 * control plugin
42 */
43 function option_defaults(&$options) { }
44
45 /**
46 * Return a string to display as the clickable title for the
47 * access control.
48 */
49 function summary_title() {
50 return t('Unknown');
51 }
52
53 /**
54 * Determine the expiration time of the cache type, or NULL if no expire.
55 *
56 * Plugins must override this to implement expiration.
57 *
58 * @param $type
59 * The cache type, either 'query', 'result' or 'output'.
60 */
61 function cache_expire($type) { }
62
63 /**
64 * Save data to the cache.
65 *
66 * A plugin should override this to provide specialized caching behavior.
67 */
68 function cache_set($type) {
69 switch ($type) {
70 case 'query':
71 // Not supported currently, but this is certainly where we'd put it.
72 break;
73 case 'results':
74 $data = array(
75 'result' => $this->view->result,
76 'total_rows' => $this->view->total_rows,
77 'pager' => $this->view->pager,
78 );
79 cache_set($this->get_results_key(), $data, $this->table);
80 break;
81 case 'output':
82 $this->gather_headers();
83 $this->storage['output'] = $this->view->display_handler->output;
84 cache_set($this->get_output_key(), $this->storage, $this->table);
85 break;
86 }
87 }
88
89
90 /**
91 * Retrieve data from the cache.
92 *
93 * A plugin should override this to provide specialized caching behavior.
94 */
95 function cache_get($type) {
96 $cutoff = $this->cache_expire($type);
97 switch ($type) {
98 case 'query':
99 // Not supported currently, but this is certainly where we'd put it.
100 return FALSE;
101 case 'results':
102 // Values to set: $view->result, $view->total_rows, $view->execute_time,
103 // $view->pager['current_page'].
104 if ($cache = cache_get($this->get_results_key(), $this->table)) {
105 if (!$cutoff || $cache->created > $cutoff) {
106 $this->view->result = $cache->data['result'];
107 $this->view->total_rows = $cache->data['total_rows'];
108 $this->view->pager = $cache->data['pager'];
109 $this->view->execute_time = 0;
110 return TRUE;
111 }
112 }
113 return FALSE;
114 case 'output':
115 if ($cache = cache_get($this->get_output_key(), $this->table)) {
116 if (!$cutoff || $cache->created > $cutoff) {
117 $this->storage = $cache->data;
118 $this->view->display_handler->output = $cache->data['output'];
119 $this->restore_headers();
120 return TRUE;
121 }
122 }
123 return FALSE;
124 }
125 }
126
127 /**
128 * Clear out cached data for a view.
129 *
130 * We're just going to nuke anything related to the view, regardless of display,
131 * to be sure that we catch everything. Maybe that's a bad idea.
132 */
133 function cache_flush() {
134 cache_clear_all($this->view->name . ':', $this->table, TRUE);
135 }
136
137 /**
138 * Post process any rendered data.
139 *
140 * This can be valuable to be able to cache a view and still have some level of
141 * dynamic output. In an ideal world, the actual output will include HTML
142 * comment based tokens, and then the post process can replace those tokens.
143 *
144 * Example usage. If it is known that the view is a node view and that the
145 * primary field will be a nid, you can do something like this:
146 *
147 * <!--post-FIELD-NID-->
148 *
149 * And then in the post render, create an array with the text that should
150 * go there:
151 *
152 * strtr($output, array('<!--post-FIELD-1-->', 'output for FIELD of nid 1');
153 *
154 * All of the cached result data will be available in $view->result, as well,
155 * so all ids used in the query should be discoverable.
156 */
157 function post_render(&$output) { }
158
159 /**
160 * Start caching javascript, css and other out of band info.
161 *
162 * This takes a snapshot of the current system state so that we don't
163 * duplicate it. Later on, when gather_headers() is run, this information
164 * will be removed so that we don't hold onto it.
165 */
166 function cache_start() {
167 $this->storage['head'] = drupal_set_html_head();
168 $this->storage['css'] = drupal_add_css();
169
170 foreach (array('header', 'footer') as $scope) {
171 $this->storage['js'][$scope] = drupal_add_js(NULL, NULL, $scope);
172 }
173 }
174
175 /**
176 * Gather out of band data, compare it to what we started with and store the difference.
177 */
178 function gather_headers() {
179 // Simple replacement for head
180 $this->storage['head'] = str_replace($this->storage['head'], '', drupal_set_html_head());
181
182 // Slightly less simple for CSS:
183 $css = drupal_add_css();
184 $start = $this->storage['css'];
185 $this->storage['css'] = array();
186
187 foreach ($css as $media => $medias) {
188 foreach ($medias as $type => $types) {
189 foreach ($types as $path => $preprocess) {
190 if (!isset($start[$media][$type][$path])) {
191 $this->storage['css'][] = array($path, $type, $media, $preprocess);
192 }
193 }
194 }
195 }
196
197 $js = array();
198 // A little less simple for js
199 foreach (array('header', 'footer') as $scope) {
200 $js[$scope] = drupal_add_js(NULL, NULL, $scope);
201 }
202
203 $start = $this->storage['js'];
204 $this->storage['js'] = array();
205
206 foreach ($js as $scope => $scopes) {
207 foreach ($scopes as $type => $types) {
208 foreach ($types as $id => $info) {
209 if (!isset($start[$scope][$type][$id])) {
210 switch ($type) {
211 case 'setting':
212 $this->storage['js'][] = array($info, $type, $scope);
213 break;
214
215 case 'inline':
216 $this->storage['js'][] = array($info['code'], $type, $scope, $info['defer']);
217 break;
218
219 default:
220 $this->storage['js'][] = array($id, $type, $scope, $info['defer'], $info['cache']);
221 }
222 }
223 }
224 }
225 }
226 }
227
228 /**
229 * Restore out of band data saved to cache. Copied from Panels.
230 */
231 function restore_headers() {
232 if (!empty($this->storage['head'])) {
233 drupal_set_html_head($this->storage['head']);
234 }
235 if (!empty($this->storage['css'])) {
236 foreach ($this->storage['css'] as $args) {
237 call_user_func_array('drupal_add_css', $args);
238 }
239 }
240 if (!empty($this->storage['js'])) {
241 foreach ($this->storage['js'] as $args) {
242 call_user_func_array('drupal_add_js', $args);
243 }
244 }
245 }
246
247 function get_results_key() {
248 global $user;
249
250 if (!isset($this->_results_key)) {
251 $key_data = array(
252 'build_info' => $this->view->build_info,
253 'roles' => array_keys($user->roles),
254 'super-user' => $user->uid == 1, // special caching for super user.
255 );
256 foreach (array('exposed_info', 'page', 'sort', 'order') as $key) {
257 if (isset($_GET[$key])) {
258 $key_data[$key] = $_GET[$key];
259 }
260 }
261
262 $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . md5(serialize($key_data));
263 }
264
265 return $this->_results_key;
266 }
267
268 function get_output_key() {
269 global $user;
270 if (!isset($this->_output_key)) {
271 $key_data = array(
272 'result' => $this->view->result,
273 'roles' => array_keys($user->roles),
274 'super-user' => $user->uid == 1, // special caching for super user.
275 );
276
277 $this->_output_key = $this->view->name . ':' . $this->display->id . ':output:' . md5(serialize($key_data));
278 }
279
280 return $this->_output_key;
281 }
282
283 }

  ViewVC Help
Powered by ViewVC 1.1.2