| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: cache.inc,v 1.25.2.4 2010/03/12 01:51:47 merlinofchaos Exp $ 3 /** 4 * @file cache.inc 5 * 6 * Functions to load Views' data so that it knows what is available to 7 * build queries from. 8 */ 9 10 /** 11 * Load views files on behalf of modules. 12 */ 13 function _views_include_handlers() { 14 views_module_include('views.inc'); 15 } 16 17 /** 18 * Load default views files on behalf of modules. 19 */ 20 function _views_include_default_views() { 21 views_module_include('views_default.inc'); 22 } 23 24 /** 25 * Fetch Views' data from the cache 26 */ 27 function _views_fetch_data($table = NULL) { 28 static $cache = NULL; 29 if (!isset($cache)) { 30 $start = views_microtime(); 31 // NOTE: This happens whether we retrieve them from cache or otherwise. 32 views_include_handlers(); 33 34 $data = views_cache_get('views_data', TRUE); 35 if (!empty($data->data)) { 36 $cache = $data->data; 37 } 38 39 if (empty($cache)) { 40 $cache = module_invoke_all('views_data'); 41 foreach (module_implements('views_data_alter') as $module) { 42 $function = $module . '_views_data_alter'; 43 $function($cache); 44 } 45 46 views_cache_set('views_data', $cache, TRUE); 47 } 48 49 vpr('Views data build time: ' . (views_microtime() - $start) * 1000 . ' ms'); 50 } 51 52 if (!$table) { 53 return $cache; 54 } 55 if (isset($cache[$table])) { 56 return $cache[$table]; 57 } 58 59 // Return an empty array if there is no match. 60 return array(); 61 } 62 63 /** 64 * Fetch the plugin data from cache. 65 */ 66 function _views_fetch_plugin_data($type = NULL, $plugin = NULL) { 67 static $cache = NULL; 68 if (!isset($cache)) { 69 $start = views_microtime(); 70 views_include_handlers(); 71 72 $cache = views_discover_plugins(); 73 74 vpr('Views plugins build time: ' . (views_microtime() - $start) * 1000 . ' ms'); 75 } 76 77 if (!$type && !$plugin) { 78 return $cache; 79 } 80 else if (!$plugin) { 81 // Not in the if above so the else below won't run 82 if (isset($cache[$type])) { 83 return $cache[$type]; 84 } 85 } 86 else if (isset($cache[$type][$plugin])) { 87 return $cache[$type][$plugin]; 88 } 89 90 // Return an empty array if there is no match. 91 return array(); 92 } 93 94 /** 95 * Scan all modules for default views and rebuild the default views cache. 96 * 97 * @return An associative array of all known default views. 98 */ 99 function _views_discover_default_views() { 100 static $cache = NULL; 101 102 if (!isset($cache)) { 103 $index = views_cache_get('views_default_views_index', TRUE); 104 105 // Retrieve each cached default view 106 if (isset($index->data) && is_array($index->data)) { 107 $cache = array(); 108 foreach ($index->data as $view_name) { 109 $data = views_cache_get('views_default:' . $view_name, TRUE); 110 if (isset($data->data) && is_object($data->data)) { 111 $cache[$view_name] = $data->data; 112 } 113 } 114 } 115 // If missing index, rebuild the cache 116 else { 117 views_include_default_views(); 118 $cache = array(); 119 120 foreach(module_implements('views_default_views') as $module) { 121 $results = call_user_func($module . "_views_default_views"); 122 if (!empty($results) && is_array($results)) { 123 foreach($results as $name => $view) { 124 // Only views with a sufficiently high api version are eligible. 125 if (!empty($view->api_version) && $view->api_version >= 2) { 126 // Do not cache dead handlers. 127 $view->destroy(); 128 if (!isset($cache[$name])) { 129 $cache[$name] = $view; 130 } 131 else { 132 watchdog('view', "View name '@name' is already taken", array('@name' => $name), WATCHDOG_ERROR); 133 } 134 } 135 } 136 } 137 } 138 139 // Allow modules to modify default views before they are cached. 140 drupal_alter('views_default_views', $cache); 141 142 // Cache the index 143 $index = array_keys($cache); 144 views_cache_set('views_default_views_index', $index, TRUE); 145 146 // Cache each view 147 foreach ($cache as $name => $view) { 148 views_cache_set('views_default:' . $name, $view, TRUE); 149 } 150 } 151 } 152 153 return $cache; 154 } 155 156 /** 157 * Set a cached item in the views cache. 158 * 159 * This is just a convenience wrapper around cache_set(). 160 * 161 * @param $cid 162 * The cache ID of the data to store. 163 * @param $data 164 * The data to store in the cache. Complex data types will be automatically serialized before insertion. 165 * Strings will be stored as plain text and not serialized. 166 * @param $use_language 167 * If TRUE, the data will be cached specific to the currently active language. 168 */ 169 function views_cache_set($cid, $data, $use_language = FALSE) { 170 global $language; 171 172 if (variable_get('views_skip_cache', FALSE)) { 173 return; 174 } 175 if ($use_language) { 176 $cid .= ':' . $language->language; 177 } 178 179 cache_set($cid, $data, 'cache_views'); 180 } 181 182 /** 183 * Return data from the persistent views cache. 184 * 185 * This is just a convenience wrapper around cache_get(). 186 * 187 * @param $cid 188 * The cache ID of the data to retrieve. 189 * @param $use_language 190 * If TRUE, the data will be requested specific to the currently active language. 191 */ 192 function views_cache_get($cid, $use_language = FALSE) { 193 global $language; 194 195 if (variable_get('views_skip_cache', FALSE)) { 196 return 0; 197 } 198 if ($use_language) { 199 $cid .= ':' . $language->language; 200 } 201 202 return cache_get($cid, 'cache_views'); 203 } 204 205 /** 206 * @defgroup views_object_cache Non-volatile cache storage 207 * @{ 208 * The non-volatile object cache is used to store an object while it is 209 * being edited, so that we don't have to save until we're completely 210 * done. The cache should be 'cleaned' on a regular basis, meaning to 211 * remove old objects from the cache, but otherwise the data in this 212 * cache must remain stable, as it includes unsaved changes. 213 */ 214 215 /** 216 * Get an object from the non-volatile Views cache. 217 * 218 * This function caches in memory as well, so that multiple calls to this 219 * will not result in multiple database reads. 220 * 221 * @param $obj 222 * A 32 character or less string to define what kind of object is being 223 * stored; primarily this is used to prevent collisions. 224 * @param $name 225 * The name of the view (or other object) being stored. 226 * @param $skip_cache 227 * Skip the memory cache, meaning this must be read from the db again. 228 * 229 * @return 230 * The data that was cached. 231 */ 232 function views_object_cache_get($obj, $name, $skip_cache = FALSE) { 233 static $cache = array(); 234 $key = "$obj:$name"; 235 if ($skip_cache) { 236 unset($cache[$key]); 237 } 238 239 if (!array_key_exists($key, $cache)) { 240 $data = db_fetch_object(db_query("SELECT * FROM {views_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name)); 241 if ($data) { 242 $cache[$key] = unserialize($data->data); 243 } 244 } 245 return isset($cache[$key]) ? $cache[$key] : NULL; 246 } 247 248 /** 249 * Store an object in the non-volatile Views cache. 250 * 251 * @param $obj 252 * A 32 character or less string to define what kind of object is being 253 * stored; primarily this is used to prevent collisions. 254 * @param $name 255 * The name of the view (or other object) being stored. 256 * @param $cache 257 * The object to be cached. This will be serialized prior to writing. 258 */ 259 function views_object_cache_set($obj, $name, $cache) { 260 views_object_cache_clear($obj, $name); 261 db_query("INSERT INTO {views_object_cache} (sid, obj, name, data, updated) VALUES ('%s', '%s', '%s', '%s', %d)", session_id(), $obj, $name, serialize($cache), time()); 262 } 263 264 /** 265 * Remove an object from the non-volatile Views cache 266 * 267 * @param $obj 268 * A 32 character or less string to define what kind of object is being 269 * stored; primarily this is used to prevent collisions. 270 * @param $name 271 * The name of the view (or other object) being stored. 272 */ 273 function views_object_cache_clear($obj, $name) { 274 db_query("DELETE FROM {views_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name); 275 } 276 277 /** 278 * Remove all objects in the object cache that are older than the 279 * specified age. 280 * 281 * @param $age 282 * The minimum age of objects to remove, in seconds. For example, 86400 is 283 * one day. Defaults to 7 days. 284 */ 285 function views_object_cache_clean($age = NULL) { 286 if (empty($age)) { 287 $age = 86400 * 7; // 7 days 288 } 289 db_query("DELETE FROM {views_object_cache} WHERE updated < %d", time() - $age); 290 } 291 292 /** 293 * @} 294 */
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |