Speeding up Joomla!'s search results

Thursday, 18 January 2007

First of all why would you cache Joomla! search results?
Let me give you a few reasons:
-it speeds up 'hot' searches, especially if you have a high traffic website and you notice users are frequently searching for some keywords over and over
-you may use Phlooid's tag cloud, wich in turn triggers searches on tags. Wouldn't it be nice if those searches would spawn results immediately?
-searching, especially with more than one keyword in "any words" mode (which is the default mode), scans you database heavily, being a resource-demanding process
-you may have lots of articles, or any other items that a search bot is installed for. Caching will speed up result display on frequent searches.

This tip requires digging some Joomla! internals, specifically the search component.Iit's a core hack, so be aware a Joomla update will wipe it away.
You need to alter one single file, /components/com_search/search.php
This tip is tested and applies to Joomla 1.0.11!, please contact us if you have updated info on 1.0.12

Requirements:
-A Joomla website, preferably version 1.0.11
-A writeable /cache folder in your Joomla! root. this can be verified bu checking system information in your Joomla admin menu.
-you don't need global configuration cache option to be enabled. The hack only depends on cache folder writeability

How it's done:
First of all, make a backup copy of you /components/com_search/search.php. You never know!

Open for editing /components/com_search/search.php, and go to line 151
the line should look like:

$results   = $_MAMBOTS->trigger( 'onSearch', array( $searchword, $phrase, $ordering ) );
Empty this line, then select and copy the following text:
           // search result caching hack by teachmejoomla
/***********************************************************************/           
        global $mosConfig_cachepath;
        require_once( $mosConfig_absolute_path . '/includes/Cache/Lite/Function.php' );
        $lifetime=86400;
        $cache = new Cache_Lite( 
array("cacheDir" => $mosConfig_cachepath."/", 
  "lifeTime" =>  $lifetime) );
 
if ($cachedquery = $cache->get("searchcache_" . $searchword_clean . $phrase . $ordering) ) {
                    $results=unserialize($cachedquery);
                    
             //  echo "<h1>Cache hit on $searchword_clean . $phrase . $ordering</h1>" ;   
               
                }
else
 
{       //MOVED
        $results    = $_MAMBOTS->trigger( 'onSearch', array( $searchword, $phrase, $ordering ) );
        //MOVED
        
        $searchcache=serialize($results);
        //echo $searchcache;
        $cache->save($searchcache,"searchcache_" . $searchword_clean . $phrase . $ordering);
}
/***********************************************************************/


, and paste it in your (search.php) editor window at cursor position.
that's it, you're done.

A few notes

The hack stores results for $lifetime seconds, as a serialized PHP variable.
On the first search for a keyword/keyphrase, the results are pulled from the database and stored in Joomla's cache folder. On subsequent searches, the results are shown directly from cache, eliminating the SQL overhead.
If $lifetime seconds passed since the phrase was searched, the cache is cleaned and renewed automatically.

Enjoy!


Give us some social love (it really works now)!

Reddit! Del.icio.us! StumbleUpon! Yahoo! Swik!



Be first to comment this article
RSS comments

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Please don't use comments to plug your web site. Links are rel='nofollow'-ed
  • Please refresh the page if you're having trouble with the security image code
Name:
E-mail
Homepage
Title:
Comment:

:) :grin ;) 8) :p
:roll :eek :upset :zzz :sigh
:? :cry :( :x
Code:* Code

Last Updated ( Thursday, 18 January 2007 )
 
468x60b

Europe freelancer directory

Newsletter

Subscribe to TeachMeJoomla's newsletter
Name:
Email:


Auto tags

joomla search cache

joomla com_search howto

com_search joomla

joomla $results in array

joomla search nofollow

alter search in joomla

speed up joomla component

joomla caching search results

joomla searches result

Hack H1 de Joomla

joomla keyword

php +cache +lite +joomla

joomla + cache + folder

joomla search ordering

Cache Folder joomla!

speed up joomla db

joomla com search speed

joomla security cache folder

hack joomla com_search

joomla keyword function

php cache search results

joomla + keyword search