Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.512 diff -u -p -r1.512 common.inc --- includes/common.inc 29 Jan 2006 07:36:29 -0000 1.512 +++ includes/common.inc 3 Feb 2006 23:42:33 -0000 @@ -1035,6 +1035,8 @@ function drupal_page_footer() { } module_invoke_all('exit'); + // write path alias cache + drupal_put_path_map(); } /** Index: includes/path.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/path.inc,v retrieving revision 1.3 diff -u -p -r1.3 path.inc --- includes/path.inc 24 Jan 2006 08:18:26 -0000 1.3 +++ includes/path.inc 3 Feb 2006 23:42:33 -0000 @@ -22,6 +22,43 @@ function drupal_init_path() { } } +function &drupal_get_path_map() { + static $map = NULL; + static $loaded = false; + if (!$loaded) { + $data = cache_get('url_alias'); + $map = unserialize($data->data); + $loaded = true; + + if (!is_array($map->dest)) { + $map->dest = array(); + } + if (!is_array($map->lasthit)) { + $map->lasthit = array(); + } + } + return $map; +} + +function drupal_put_path_map() { + $map = drupal_get_path_map(); + if (!$map) { + return; + } + + $max = variable_get('url_alias_max_cache', 500); + $total = count($map->dest); + if ($count > $max) { + // prune + asort($map->lasthit); + for ($i = $total; $i > $max; $i--) { + $dest = array_shift($map->lasthit); + unset($map->dest[$dest]); + } + } + cache_set('url_alias', serialize($map)); +} + /** * Given an alias, return its Drupal system URL if one exists. Given a Drupal * system URL return its alias if one exists. @@ -39,36 +76,39 @@ function drupal_init_path() { * found. */ function drupal_lookup_path($action, $path = '') { - static $map = array(); - static $count = NULL; + $cache = &drupal_get_path_map(); - if ($count === NULL) { - $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}')); + if ($cache->count === NULL) { + $cache->count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}')); } if ($action == 'wipe') { - $map = array(); + $cache->dest = array(); + $cache->lasthit = array(); } - elseif ($count > 0 && $path != '') { + elseif ($cache->count > 0 && $path != '') { if ($action == 'alias') { - if (isset($map[$path])) { - return $map[$path]; + if (isset($cache->dest[$path])) { + return $cache->dest[$path]; } if ($alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path))) { - $map[$path] = $alias; + $cache->dest[$path] = $alias; + $cache->lasthit[$path] = time(); return $alias; } else { - $map[$path] = $path; + $cache->dest[$path] = $path; + $cache->lasthit[$path] = time(); } } elseif ($action == 'source') { - if ($alias = array_search($path, $map)) { + if ($alias = array_search($path, $cache->dest)) { return $alias; } - if (!isset($map[$path])) { + if (!isset($cache->dest[$path])) { if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $path))) { - $map[$src] = $path; + $cache->dest[$src] = $path; + $cache->lasthit[$src] = time(); return $src; } }