From d48ef4198ebd5e5107335091e94f4910384c3af0 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 7 Jun 2013 11:47:50 -0400 Subject: [PATCH] Minor refactoring of DbMessageSource. --- framework/yii/i18n/DbMessageSource.php | 35 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/framework/yii/i18n/DbMessageSource.php b/framework/yii/i18n/DbMessageSource.php index 831cabb..14cf0b9 100644 --- a/framework/yii/i18n/DbMessageSource.php +++ b/framework/yii/i18n/DbMessageSource.php @@ -41,8 +41,6 @@ use yii\db\Query; * the translated messages. The name of these two tables can be customized by setting [[sourceMessageTable]] * and [[messageTable]], respectively. * - * When [[cachingDuration]] is set as a positive number, message translations will be cached. - * * @author resurtm * @since 2.0 */ @@ -68,18 +66,23 @@ class DbMessageSource extends MessageSource */ public $cache = 'cache'; /** - * @var string the name of the source message table. Defaults to `{{%source_message}}`. + * @var string the name of the source message table. */ - public $sourceMessageTable = '{{%source_message}}'; + public $sourceMessageTable = 'tbl_source_message'; /** - * @var string the name of the translated message table. Defaults to `{{%message}}`. + * @var string the name of the translated message table. */ - public $messageTable = '{{%message}}'; + public $messageTable = 'tbl_message'; /** * @var integer the time in seconds that the messages can remain valid in cache. - * Defaults to 0, meaning the caching is disabled. + * Use 0 to indicate that the cached data will never expire. + * @see enableCaching */ public $cachingDuration = 0; + /** + * @var boolean whether to enable caching translated messages + */ + public $enableCaching = false; /** * Initializes the DbMessageSource component. @@ -96,7 +99,7 @@ class DbMessageSource extends MessageSource if (!$this->db instanceof Connection) { throw new InvalidConfigException("DbMessageSource::db must be either a DB connection instance or the application component ID of a DB connection."); } - if ($this->cachingDuration > 0) { + if ($this->enableCaching) { if (is_string($this->cache)) { $this->cache = Yii::$app->getComponent($this->cache); } @@ -117,16 +120,20 @@ class DbMessageSource extends MessageSource */ protected function loadMessages($category, $language) { - if ($this->cachingDuration === 0) { - return $this->retrieveMessages($category, $language); - } else { - $key = static::CACHE_KEY_PREFIX . ".messages.{$category}.{$language}"; + if ($this->enableCaching) { + $key = array( + __CLASS__, + $category, + $language, + ); $messages = $this->cache->get($key); if ($messages === false) { - $messages = $this->retrieveMessages($category, $language); + $messages = $this->loadMessagesFromDb($category, $language); $this->cache->set($key, $messages, $this->cachingDuration); } return $messages; + } else { + return $this->loadMessagesFromDb($category, $language); } } @@ -137,7 +144,7 @@ class DbMessageSource extends MessageSource * @param string $language the target language. * @return array the messages loaded from database. */ - protected function retrieveMessages($category, $language) + protected function loadMessagesFromDb($category, $language) { $query = new Query(); $messages = $query->select(array('t1.message message', 't2.translation translation'))