Permalink
Browse files

Dev Activated model schema caching to reduce the number of queries

  • Loading branch information...
c-schmitz committed Dec 20, 2013
1 parent d8c6307 commit 055b7ab57d8a4f5e69ace4ec28c7f86d4faff14e
View
@@ -12,6 +12,7 @@
/upload/.htaccess
/nbproject
/tmp/assets/*
+/tmp/runtime/cache/*
.svn
/third_party/jqgrid/src
.settings/org.eclipse.php.core.prefs
@@ -49,7 +49,13 @@
),
'log' => array(
'class' => 'CLogRouter'
- )
+ ),
+ 'cache'=>array(
+ 'class'=>'system.caching.CFileCache',
+ ),
+ 'db' => array(
+ 'schemaCachingDuration' => 3600,
+ )
)
);
@@ -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);
@@ -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"));
@@ -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();
@@ -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)
{
@@ -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)
{
@@ -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;
@@ -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
{
@@ -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;
}
Oops, something went wrong.

0 comments on commit 055b7ab

Please sign in to comment.