spacer.png, 0 kB
ExtCal slow? Optimize performance

For the Guanxi.nu website that I made, I used the very nice Open Source ExtCalendar Events Calendar 0.9.1. This calendar has a very nice Module that shows a goodlooking graphical calender called mod_extcalendar_minical, but suffers from terrible performance. That's why I decided to dive into the sourcecode to see what a difference a little bit of caching would make to this calander. It turns out that this calender does TWO databasequeries per day in the calender (for example 31 days would give you 62 queries). This could probably be greatly reduced by smarter queries, but this is not the easiest way to improve the performance.

Joomla has built-in caching features (Site - Global Configuration - Cache Tab) and we can simply edit the Minical module with these simple steps:

  • Open the file called mod_extcalendar_minical.php (in joomla/modules)

  • Browse down to the function declaration (near the bottom)

function minical_get_events($date_stamp, $include_recurrent = false, $show_overlapping_recurrences = false)

{


  • Now add these two new functions above it:

function minical_get_events_on_date($event_condition)
{
    global $CONFIG_EXT, $database, $cat_id;
   
    $query = "SELECT e.extid, start_date, end_date from " . $CONFIG_EXT['TABLE_EVENTS'] . " AS e LEFT JOIN " . $CONFIG_EXT['TABLE_CATEGORIES'] . " AS c ON e.cat=c.cat_id ";
  $query .= "WHERE ".$event_condition." AND c.published = '1' AND approved = '1' AND recur_type = ''";
    if(isset($cat_id) && is_numeric($cat_id)) $query .= "AND e.cat = '".$cat_id."' ";
  $query .= "ORDER BY start_date,title ASC";
 
  $database->setQuery( $query );   
    $rows = $database->loadObjectList();
  return $rows;
}

function minical_get_events_on_date_recurring($day_pattern, $cat_filter)
{
    global $CONFIG_EXT, $database;
    $query = "SELECT e.extid, recur_type, recur_val, recur_until, start_date, end_date, recur_end_type, recur_count from ";
    $query .= $CONFIG_EXT['TABLE_EVENTS'] . " AS e LEFT JOIN " . $CONFIG_EXT['TABLE_CATEGORIES'] . " AS c ON e.cat=c.cat_id ";
    $query .= "WHERE (DATE_FORMAT(e.start_date,'%Y%m%d') <= ". $day_pattern . ") AND c.published = '1' AND approved = '1' AND recur_type <> '' ". $cat_filter ." ";
    $query .= "ORDER BY start_date, title ASC";

    $database->setQuery( $query );   
    $rows = $database->loadObjectList();
  return $rows;
}



          • Next we make sure these functions are actually used. Inside function minical_get_events, we update the code in the following manner:
After switch($CONFIG_EXT['multi_day_events']) {...} and before if($include_recurrent) {

 

//here insert call to enable cache
  $cacheevents =& mosCache::getCache( 'mos_extcalendar_minical' );
  $rowsevents = $cacheevents->call('minical_get_events_on_date', $event_condition);
 
  $events = array();
 
  //while ($row = extcal_db_fetch_row($result))
  foreach ($rowsevents as $row)
  {
      $events[] = array($row->extid,strtotime($row->start_date),strtotime($row->end_date));
  }


  • and a little bit further:
After if(isset($cat_id) && is_numeric($cat_id)) $cat_filter .= "AND e.cat = '".$cat_id."'"; and before $event = new ExtCal_Event()

        //these calls will enable cacheing for the inner SQL-queries
      $cacheeventsrecur =& mosCache::getCache( 'mos_extcalendar_minical' );
      $rowseventsrecur = $cacheeventsrecur->call('minical_get_events_on_date_recurring', $day_pattern, $cat_filter);
     
      $recur_events = array();
     
      foreach ($rowseventsrecur as $row)
      {



  • That's all! This will make your calender use cacheing and you will see the performance of your website drastically improve, especially on slower hosts or on popular sites.

 

 Confused? you can view the total source of  mod_extcalendar_minical.php here .

 

Last Updated ( Friday, 23 June 2006 )
 
spacer.png, 0 kB
rightborder
© 2006-2008 StartInChina.com - News and tips from Shenzhen, China