Speeding up Joomla!'s search results PDF Print E-mail

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!

Add as favourites (0)

  Comments (2)
RSS comments
 1 A1
Written by This e-mail address is being protected from spam bots, you need JavaScript enabled to view it , on 18-02-2007 20:19
Brilliant post, got me going with cache_lite
 2 guddd
Written by This e-mail address is being protected from spam bots, you need JavaScript enabled to view it website, on 04-11-2007 22:34
thanks broth for ur information..

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Personal verbal attacks will be deleted.
  • Please don't use comments to plug your web site. Such material will be removed.
  • Just ensure to *Refresh* your browser for a new security code to be displayed prior to clicking on the 'Send' button.
  • Keep in mind that the above process only applies if you simply entered the wrong security code.
Name:
E-mail
Homepage
Title:
Comment:

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

Powered by AkoComment Tweaked Special Edition v.1.4.2

 
Tag it:
Delicious
Furl it!
Spurl
Blinkbits
BlinkList
digg
Fark
Reddit
YahooMyWeb

Login Form






Lost Password?
No account yet? Register

Top SE queries for this URL

speeding up joomla

Joomla books

Are you secure?

Try BitDefender antivirus scan online for free!

Satisfied?Click below to buy world's best anti virus

Buy Antivirus software
© 2008 TeachMeJoomla
Joomla! is Free Software released under the GNU/GPL License.