- maurits's blog
- Login to post comments
When troubleshooting performance on Drupal websites you may find out that Drupal executes tenths or hundreds of queries per request to get an alias by the internal path. This is done in the function drupal_lookup_path (which resides in includes/path.inc). The query is:
SELECT dst FROM url_alias WHERE src = 'node/3496' AND language IN('nl', '') ORDER BY language DESC
Although this is a fast query, it’s desired to cache those cause they are with many. But there is no solution in core nor a module on drupal.org which can do that. Pressflow - however - can do this. And it is possible to implement this in Drupal.
Download Pressflow and grab the module named path_alias_cache. This module doesn’t work out of the box in Drupal. It depends on the lookup_path hook, which - In Drupal - doesn’t exists.
The lookup_path hook is implemented in the path.inc file. Pressflow had changed the drupal_get_path_alias function and added a function _drupal_lookup_path_direct function, which holds the original Drupal code from drupal_get_path_alias. Attached is a patch which we have to apply to make this hook available in Drupal. We can now copy the path_alias_cache module from Pressflow to have path aliases cached in our Drupal installation!
There is one more thing I worried about… What if path aliases gets deleted or changed? Will the path_alias_cache module always return the new aliases? It will lead to broken links otherwise…
Some browsing in the code and looking at the cache entries in the database learned me more about the working of this module. For each request, it saves a record with only original paths (destinations) and alias id’s. The real alias is not cached anywhere. On the first call to the lookup_path implementation, the record with expected aliases is retrieved from cache, and aliases for these paths are queried from the database with only 1 query. That query is very similar to the query above, but with “src IN (…)” instead of “src = ‘…’”. Thus aliases are still fetched from the database, but with lesser queries. So path aliases remains up-to-date all the time.
That also means it still possible to improve performance on this part, but then you may get side-effects. Nonetheless, it’s a worth improvement in performance.