includes/cache.inc

Go to the documentation of this file.
00001 <?php
00002 // $Id: cache.inc,v 1.25 2009/06/01 23:33:37 merlinofchaos Exp $
00013 function _views_include_handlers() {
00014   views_module_include('views.inc');
00015 }
00016 
00020 function _views_include_default_views() {
00021   views_module_include('views_default.inc');
00022 }
00023 
00027 function _views_fetch_data($table = NULL) {
00028   static $cache = NULL;
00029   if (!isset($cache)) {
00030     $start = views_microtime();
00031     // NOTE: This happens whether we retrieve them from cache or otherwise.
00032     views_include_handlers();
00033 
00034     $data = views_cache_get('views_data', TRUE);
00035     if (!empty($data->data)) {
00036       $cache = $data->data;
00037     }
00038 
00039     if (empty($cache)) {
00040       $cache = module_invoke_all('views_data');
00041       foreach (module_implements('views_data_alter') as $module) {
00042         $function = $module . '_views_data_alter';
00043         $function($cache);
00044       }
00045 
00046       views_cache_set('views_data', $cache, TRUE);
00047     }
00048 
00049     vpr('Views data build time: ' . (views_microtime() - $start) * 1000 . ' ms');
00050   }
00051 
00052   if (!$table) {
00053     return $cache;
00054   }
00055   if (isset($cache[$table])) {
00056     return $cache[$table];
00057   }
00058 
00059   // Return an empty array if there is no match.
00060   return array();
00061 }
00062 
00066 function _views_fetch_plugin_data($type = NULL, $plugin = NULL) {
00067   static $cache = NULL;
00068   if (!isset($cache)) {
00069     $start = views_microtime();
00070     views_include_handlers();
00071 
00072     $cache = views_discover_plugins();
00073 
00074     vpr('Views plugins build time: ' . (views_microtime() - $start) * 1000 . ' ms');
00075   }
00076 
00077   if (!$type && !$plugin) {
00078     return $cache;
00079   }
00080   else if (!$plugin) {
00081     // Not in the if above so the else below won't run
00082     if (isset($cache[$type])) {
00083       return $cache[$type];
00084     }
00085   }
00086   else if (isset($cache[$type][$plugin])) {
00087     return $cache[$type][$plugin];
00088   }
00089 
00090   // Return an empty array if there is no match.
00091   return array();
00092 }
00093 
00099 function _views_discover_default_views() {
00100   static $cache = NULL;
00101 
00102   if (!isset($cache)) {
00103     $data = views_cache_get('views_default_views', TRUE);
00104 
00105     if (isset($data->data) && is_array($data->data)) {
00106       $cache = $data->data;
00107     }
00108     else {
00109       views_include_default_views();
00110       $defaults = module_invoke_all('views_default_views');
00111       $cache = array();
00112 
00113       foreach ($defaults as $name => $view) {
00114         // Only views with a sufficiently high api version are eligible.
00115         if (isset($view->api_version) && $view->api_version >= 2) {
00116           // Do not cache dead handlers.
00117           $view->destroy();
00118           $cache[$name] = $view;
00119         }
00120       }
00121 
00122       // Allow modules to modify default views before they are cached.
00123       drupal_alter('views_default_views', $cache);
00124 
00125       views_cache_set('views_default_views', $cache, TRUE);
00126     }
00127   }
00128 
00129   return $cache;
00130 }
00131 
00145 function views_cache_set($cid, $data, $use_language = FALSE) {
00146   global $language;
00147 
00148   if (variable_get('views_skip_cache', FALSE)) {
00149     return;
00150   }
00151   if ($use_language) {
00152     $cid .= ':' . $language->language;
00153   }
00154 
00155   cache_set($cid, $data, 'cache_views');
00156 }
00157 
00168 function views_cache_get($cid, $use_language = FALSE) {
00169   global $language;
00170 
00171   if (variable_get('views_skip_cache', FALSE)) {
00172     return 0;
00173   }
00174   if ($use_language) {
00175     $cid .= ':' . $language->language;
00176   }
00177 
00178   return cache_get($cid, 'cache_views');
00179 }
00180 
00208 function views_object_cache_get($obj, $name, $skip_cache = FALSE) {
00209   static $cache = array();
00210   $key = "$obj:$name";
00211   if ($skip_cache) {
00212     unset($cache[$key]);
00213   }
00214 
00215   if (!array_key_exists($key, $cache)) {
00216     $data = db_fetch_object(db_query("SELECT * FROM {views_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name));
00217     if ($data) {
00218       $cache[$key] = unserialize($data->data);
00219     }
00220   }
00221   return isset($cache[$key]) ? $cache[$key] : NULL;
00222 }
00223 
00235 function views_object_cache_set($obj, $name, $cache) {
00236   views_object_cache_clear($obj, $name);
00237   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());
00238 }
00239 
00249 function views_object_cache_clear($obj, $name) {
00250   db_query("DELETE FROM {views_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name);
00251 }
00252 
00261 function views_object_cache_clean($age = NULL) {
00262   if (empty($age)) {
00263     $age = 86400 * 7; // 7 days
00264   }
00265   db_query("DELETE FROM {views_object_cache} WHERE updated < %d", time() - $age);
00266 }
00267 

Generated on Mon Nov 30 15:05:58 2009 for Views by  doxygen 1.4.7