Skip to content

Commit

Permalink
Dev Activated model schema caching to reduce the number of queries
Browse files Browse the repository at this point in the history
  • Loading branch information
c-schmitz committed Dec 20, 2013
1 parent d8c6307 commit 055b7ab
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 127 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/upload/.htaccess
/nbproject
/tmp/assets/*
/tmp/runtime/cache/*
.svn
/third_party/jqgrid/src
.settings/org.eclipse.php.core.prefs
Expand Down
8 changes: 7 additions & 1 deletion application/config/internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@
),
'log' => array(
'class' => 'CLogRouter'
)
),
'cache'=>array(
'class'=>'system.caching.CFileCache',
),
'db' => array(
'schemaCachingDuration' => 3600,
)
)
);

Expand Down
7 changes: 6 additions & 1 deletion application/controllers/admin/tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ function updatetokenattributes($iSurveyId)
$fields['attribute_' . $i] = array('type' => 'VARCHAR', 'constraint' => '255');
}

Yii::app()->db->schema->getTable('{{tokens_' . $iSurveyId . '}}', true); // Refresh schema cache just in case the table existed in the past
LimeExpressionManager::SetDirtyFlag(); // so that knows that token tables have changed

Yii::app()->session['flashmessage'] = sprintf($clang->gT("%s field(s) were successfully added."), $number2add);
Expand Down Expand Up @@ -1147,7 +1148,9 @@ function deletetokenattributes($iSurveyId)
}
elseif($sAttributeToDelete)
{
Yii::app()->db->createCommand(Yii::app()->db->getSchema()->dropColumn("{{tokens_".intval($iSurveyId)."}}", $sAttributeToDelete))->execute();
$sTableName="{{tokens_".intval($iSurveyId)."}}";
Yii::app()->db->createCommand(Yii::app()->db->getSchema()->dropColumn($sTableName, $sAttributeToDelete))->execute();
Yii::app()->db->schema->getTable($sTableName, true); // Refresh schema cache just in case the table existed in the past
LimeExpressionManager::SetDirtyFlag();
Yii::app()->session['flashmessage'] = sprintf($clang->gT("Attribute %s was deleted."), $sAttributeToDelete);
Yii::app()->getController()->redirect(Yii::app()->getController()->createUrl("/admin/tokens/sa/managetokenattributes/surveyid/$iSurveyId"));
Expand Down Expand Up @@ -2386,6 +2389,8 @@ function _newtokentable($iSurveyId)


Yii::app()->db->createCommand()->renameTable(Yii::app()->request->getPost('oldtable'), Yii::app()->db->tablePrefix."tokens_".intval($iSurveyId));
Yii::app()->db->schema->getTable(Yii::app()->db->tablePrefix."tokens_".intval($iSurveyId), true); // Refresh schema cache just in case the table existed in the past

//Check that the tokens table has the required fields
TokenDynamic::model($iSurveyId)->checkColumns();

Expand Down
2 changes: 2 additions & 0 deletions application/helpers/admin/activate_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ function activateSurvey($iSurveyID, $simulate = false)
try
{
$execresult = createTable($tabname, $createsurvey);
Yii::app()->db->schema->getTable($tabname, true); // Refresh schema cache just in case the table existed in the past
}
catch (CDbException $e)
{
Expand Down Expand Up @@ -438,6 +439,7 @@ function activateSurvey($iSurveyID, $simulate = false)
try
{
$execresult = createTable($tabname,$column);
Yii::app()->db->schema->getTable($tabname, true); // Refresh schema cache just in case the table existed in the past
}
catch (CDbException $e)
{
Expand Down
4 changes: 3 additions & 1 deletion application/helpers/admin/token_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ function createTokenTable($iSurveyID, $aAttributeFields=array())
$fields[$sAttributeField]='string';
}
try{
createTable("{{tokens_".intval($iSurveyID)."}}", $fields);
$sTableName="{{tokens_".intval($iSurveyID)."}}";
createTable($sTableName, $fields);
try{
Yii::app()->db->createCommand()->createIndex("idx_token_token_{$iSurveyID}_".rand(1,50000),"{{tokens_".intval($iSurveyID)."}}",'token');
} catch(Exception $e) {}
Yii::app()->db->schema->getTable($sTableName, true); // Refresh schema cache just in case the table existed in the past
return true;
} catch(Exception $e) {
return false;
Expand Down
13 changes: 13 additions & 0 deletions application/helpers/update/updatedb_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function db_upgrade_all($iOldDBVersion) {
$sAutoIncrement = Yii::app()->getConfig('autoincrement');

$oDB = Yii::app()->getDb();
$oDB->schemaCachingDuration=0; // Deactivate schema caching
$oTransaction = $oDB->beginTransaction();
try
{
Expand Down Expand Up @@ -1169,10 +1170,22 @@ function db_upgrade_all($iOldDBVersion) {
$oDB->createCommand()->update('{{settings_global}}',array('stg_value'=>173),"stg_name='DBVersion'");
}
$oTransaction->commit();
// Activate schema caching
$oDB->schemaCachingDuration=3600;
// Load all tables of the application in the schema
Yii::app()->db->schema->getTables();
// clear the cache of all loaded tables
Yii::app()->db->schema->refresh();
}
catch(Exception $e)
{
$oTransaction->rollback();
// Activate schema caching
$oDB->schemaCachingDuration=3600;
// Load all tables of the application in the schema
Yii::app()->db->schema->getTables();
// clear the cache of all loaded tables
Yii::app()->db->schema->refresh();
echo '<br /><br />'.$oLang->gT('An non-recoverable error happened during the update. Error details:')."<p>".htmlspecialchars($e->getMessage()).'</p><br />';
return false;
}
Expand Down
Loading

0 comments on commit 055b7ab

Please sign in to comment.