diff --git includes/cache.inc includes/cache.inc index 355c98f..05ca7ec 100644 --- includes/cache.inc +++ includes/cache.inc @@ -100,53 +100,44 @@ function _views_discover_default_views($reset = FALSE) { static $cache = NULL; if (!isset($cache) || $reset) { - $index = views_cache_get('views_default_views_index', TRUE); - - // Retrieve each cached default view - if (!$reset && isset($index->data) && is_array($index->data)) { - $cache = array(); - foreach ($index->data as $view_name) { - $data = views_cache_get('views_default:' . $view_name, TRUE); - if (isset($data->data) && is_object($data->data)) { - $cache[$view_name] = $data->data; - } - } + $list = views_cache_get('views_default_views_list', TRUE); + + // Check if the cached list exists + if (!$reset && isset($list->data) && is_array($list->data)) { + $cache = $list->data; + return $cache; } - // If missing index, rebuild the cache - else { - views_include_default_views(); - $cache = array(); - - foreach (module_implements('views_default_views') as $module) { - $results = call_user_func($module . "_views_default_views"); - if (!empty($results) && is_array($results)) { - foreach($results as $name => $view) { - // Only views with a sufficiently high api version are eligible. - if (!empty($view->api_version) && $view->api_version >= 2) { - // Do not cache dead handlers. - $view->destroy(); - if (!isset($cache[$name])) { - $cache[$name] = $view; - } - else { - watchdog('view', "View name '@name' is already taken", array('@name' => $name), WATCHDOG_ERROR); - } + // If missing data, rebuild the cache + views_include_default_views(); + $cache = array(); + + foreach(module_implements('views_default_views') as $module) { + $results = call_user_func($module . "_views_default_views"); + if (!empty($results) && is_array($results)) { + foreach($results as $name => $view) { + // Only views with a sufficiently high api version are eligible. + if (!empty($view->api_version) && $view->api_version >= 2) { + // Do not cache dead handlers. + $view->destroy(); + if (!isset($cache[$name])) { + $cache[$name] = $view; + } + else { + watchdog('view', "View name '@name' is already taken", array('@name' => $name), WATCHDOG_ERROR); } } } } + } - // Allow modules to modify default views before they are cached. - drupal_alter('views_default_views', $cache); - - // Cache the index - $index = array_keys($cache); - views_cache_set('views_default_views_index', $index, TRUE); + // Allow modules to modify default views before they are cached. + drupal_alter('views_default_views', $cache); + // Cache the entire set of views + views_cache_set('views_default_views_list', $cache, TRUE); - // Cache each view - foreach ($cache as $name => $view) { - views_cache_set('views_default:' . $name, $view, TRUE); - } + // Cache each view individually. + foreach ($cache as $name => $view) { + views_cache_set('views_default:' . $name, $view, TRUE); } } diff --git views.module views.module index f3556e5..99f7685 100644 --- views.module +++ views.module @@ -843,23 +843,35 @@ function views_get_plugin($type, $plugin, $reset = FALSE) { * A view object or NULL if it is not available. */ function &views_get_default_view($view_name, $reset = FALSE) { - $null = NULL; + $return = NULL; + $cid = "views_default:$view_name"; // Attempt to load individually cached view from cache. views_include('cache'); + if (!$reset) { - $data = views_cache_get("views_default:{$view_name}", TRUE); - if (isset($data->data) && is_object($data->data)) { - return $data->data; + $data = views_cache_get($cid, TRUE); + if ($data) { + $return = $data->data; } } - - // Otherwise, allow entire cache to be rebuilt. - $cache = views_discover_default_views($reset); - if (isset($cache[$view_name])) { - return $cache[$view_name]; + else { + // Otherwise, allow entire cache to be rebuilt. + $cache = views_discover_default_views($reset); + if (isset($cache[$view_name])) { + views_cache_set($cid, $cache[$view_name], TRUE); + $return = $cache[$view_name]; + } + else { + // There is no default view for this view name. To avoid loading all + // default views into memory when this is the case, which can result in + // an additional 20mb of memory usage when loading a view that only + // exists in the databse, cache the fact that we didn't find a default + // view as well. + views_cache_set($cid, NULL, TRUE); + } } - return $null; + return $return; } /** @@ -884,13 +896,8 @@ function views_new_view() { * @return An associative array of all known default views. */ function views_discover_default_views($reset = FALSE) { - static $cache = array(); - - if (empty($cache) || $reset) { - views_include('cache'); - $cache = _views_discover_default_views($reset); - } - return $cache; + views_include('cache'); + return _views_discover_default_views($reset); } /**