--- path.inc 2009-04-07 22:35:47.000000000 +0000 +++ path.inc 2009-04-08 18:53:24.000000000 +0000 @@ -44,6 +44,93 @@ function drupal_lookup_path($action, $pa static $map = array(), $no_src = array(); static $count; + $path_language = ''; +// ************* Begin pathcache.module Path Caching Modifications ************** + + // Drupal core checks to see if the url_alias table is empty on each request. + // We assume that the table is nonempty, since it would be very silly to enable + // the Path Cache module if you had no paths to cache. Thus, we have eliminated + // the $count variable altogether. + /* + + // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases + if (!isset($count)) { + $count = (int)db_result(db_query('SELECT pid FROM {url_alias} LIMIT 1')); + } + */ + + // A wipe is most commonly performed by path_set_alias() in path.module via + // drupal_clear_path_cache() in common.inc. The intent it to clear the static + // variables $map and $no_src, but we can also use it as an indicator that + // the path cache is dirty. + if ($action == 'wipe') { + $map = array(); + $no_src = array(); + // This will flush the entire cache_path cache. + // Drastic, but if running with a memcached caching backend, wildcards + // are not supported so the whole bin must be flushed. + cache_clear_all('pathdst*', 'cache', TRUE); + cache_clear_all('pathsrc*', 'cache', TRUE); + } + elseif ($path != '') { + // When action is 'alias' we are given a Drupal path and are looking to + // see if there is an alias for it. + if ($action == 'alias') { + // First check the static variable in case we've looked up this system path earlier + // in this request. + if (isset($map[$path_language][$path])) { + return $map[$path_language][$path]; + } + // Not in the static variable. Try cache. + $cid = $path . $path_language; + $cache = cache_get('pathdst'.$cid, 'cache'); + if ($cache) { + return unserialize($cache->data); + } + // Get the most fitting result falling back with alias without language. + $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path)); + // Store in static variable in case we encounter the same system path again + // during this request. + $map[$path_language][$path] = $alias; + // Store in cache. + cache_set('pathdst'.$cid, 'cache', serialize($alias), variable_get('pathcache_expire', 43200)); // 12 hours + return $alias; + } + // When action is 'source' we are given a URL alias and need to return the + // Drupal path. + // Check $no_src for this $path in case we've already determined that there + // isn't a path that has this alias. + elseif ($action == 'source' && !isset($no_src[$path_language][$path])) { + $src = ''; + // Look for the value $path within the cached $map. + if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) { + $cid = $path . $path_language; + $cache = cache_get('pathsrc'.$cid, 'cache'); + if ($cache) { + return unserialize($cache->data); + } + + // Get the most fitting result falling back with alias without language + if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $path))) { + $map[$path_language][$src] = $path; + cache_set('pathsrc'.$cid, 'cache', serialize($src), variable_get('pathcache_expire', 43200)); // 12 hours + } + else { + // We can't record anything into $map because we do not have a valid + // index and there is no need because we have not learned anything + // about any Drupal path. Thus cache to $no_src. + // Setting this flag makes the array_search($path, $map[$path_language]) test + // above false, and skips to return $src, below. + $no_src[$path_language][$path] = TRUE; + } + } + return $src; + } + } + + return FALSE; + // ************** End pathcache.module Path Caching Modifications *************** + // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases if (!isset($count)) { $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}'));