At the moment, the getGender call doesn't cache either the field for the gender or the gender of the person. This means that each time getGender is called, JomSocial goes back to the database to find what field the gender is in (potentially hundreds of times) and then looks for the gender in the database, potentially multiple times dependent upon how many times that "user" appears on the page (e.g. in the activity stream + latest members + active members could mean a few instances).
So instead of naively querying the database hundreds of time per request for data we've already queried, I suggest altering the function to look more like the following:
public function getGender( $userId )
{
static $genderCache = array();
$fieldId = $this->getGenderFieldId();
// Check we haven't asked this question before once, twice or a dozen times for this user ID.
if (!isset($genderCache[$userId]))
{
$db = JFactory::getDBO();
$query = 'SELECT '.$db->quoteName('value') . ' FROM '
.$db->quoteName('#__community_fields_values'). ' '
.'WHERE '. $db->quoteName( 'field_id' ) . ' = '. $db->Quote( $fieldId ) . ' '
. 'AND '. $db->quoteName( 'user_id' ) . ' = '. $db->Quote( $userId);
$db->setQuery( $query );
$genderCache[$userId] = $db->loadResult();
}
return $genderCache[$userId];
}
public function getGenderFieldId()
{
static $fieldId = null;
if (is_null($fieldId))
{
$db = JFactory::getDBO();
$query = 'SELECT ' . $db->quoteName( 'id' ) . ' FROM '
. $db->quoteName( '#__community_fields' ) . ' '
. 'WHERE ' . $db->quoteName( 'fieldcode' ) . '=' . $db->Quote( 'FIELD_GENDER' );
$db->setQuery( $query );
$fieldId = $db->loadResult();
}
return $fieldId;
}
This way the gender field ID is only queried once and returned and the gender is only queried once and returned per user. This reduces the number of queries posed to the database which can help it scale to more users and perform better.
Code change based on 3.0.5.3 but seems to be still applicable to 3.1
At the moment, the getGender call doesn't cache either the field for the gender or the gender of the person. This means that each time getGender is called, JomSocial goes back to the database to find what field the gender is in (potentially hundreds of times) and then looks for the gender in the database, potentially multiple times dependent upon how many times that "user" appears on the page (e.g. in the activity stream + latest members + active members could mean a few instances).
So instead of naively querying the database hundreds of time per request for data we've already queried, I suggest altering the function to look more like the following:
This way the gender field ID is only queried once and returned and the gender is only queried once and returned per user. This reduces the number of queries posed to the database which can help it scale to more users and perform better.
Code change based on 3.0.5.3 but seems to be still applicable to 3.1