Skip to content

Commit

Permalink
クエリの結果をキャッシュをとるように修正。そのため、staticに持っていた変数が不要になるため削除
Browse files Browse the repository at this point in the history
  • Loading branch information
s-nakajima committed Jan 5, 2019
1 parent c8a4b93 commit d4f099f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 68 deletions.
69 changes: 42 additions & 27 deletions Model/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,14 @@ class Language extends M17nAppModel {
*/
public $validate = array();

/**
* 言語データ
*
* @var array
*/
public static $languages = array();

/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'M17n.SaveM17n',
'NetCommons.NetCommonsCache',
);

/**
Expand Down Expand Up @@ -115,7 +109,7 @@ public function getLanguage($type = null, $options = array()) {
];
}

$languages = $this->find($type, Hash::merge(array(
$languages = $this->cacheFindQuery($type, Hash::merge(array(
'recursive' => -1,
'conditions' => array(
'is_active' => true
Expand All @@ -130,11 +124,7 @@ public function getLanguage($type = null, $options = array()) {
* @return array
*/
public function getLanguages() {
if (self::$languages) {
return self::$languages;
}

self::$languages = $this->find('all', array(
$result = $this->cacheFindQuery('all', array(
'recursive' => -1,
'fields' => [
'id', 'code', 'weight', 'is_active'
Expand All @@ -144,8 +134,22 @@ public function getLanguages() {
),
'order' => array('weight' => 'asc')
));
return $result;
}

return self::$languages;
/**
* 複数言語か否か
*
* @return bool
*/
public function isMultipleLang() {
$result = $this->cacheFindQuery('count', array(
'recursive' => -1,
'conditions' => array(
'is_active' => true
),
));
return $result;
}

/**
Expand All @@ -154,22 +158,30 @@ public function getLanguages() {
* @return array
*/
public function getLanguagesWithName() {
$languages = $this->find('all', array(
$languages = $this->cacheFindQuery('all', array(
'fields' => array('code', 'is_active'),
'recursive' => -1,
));

$activeLangs = [];
$enableLangs = [];
foreach ($languages as $lang) {
$code = $lang[$this->alias]['code'];
if (! isset($this->__labelLanguages[$code])) {
continue;
}
$enableLangs[$code] = __d('m17n', $this->__labelLanguages[$code]);
if ($lang[$this->alias]['is_active']) {
$activeLangs[] = $code;
$activeLangs = $this->cacheRead('activeLangs');
$enableLangs = $this->cacheRead('enableLangs');
if (! $activeLangs || ! $enableLangs) {
$activeLangs = [];
$enableLangs = [];
foreach ($languages as $lang) {
$code = $lang[$this->alias]['code'];
if (! isset($this->__labelLanguages[$code])) {
continue;
}
$enableLangs[$code] = __d('m17n', $this->__labelLanguages[$code]);
if ($lang[$this->alias]['is_active']) {
$activeLangs[] = $code;
}
}

//キャッシュの書き込み
$this->cacheWrite($activeLangs, 'activeLangs');
$this->cacheWrite($enableLangs, 'enableLangs');
}

return array($activeLangs, $enableLangs);
Expand Down Expand Up @@ -209,7 +221,10 @@ public function saveActive($data) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

$languages = $this->find('list', array(
//キャッシュのクリア
$this->cacheClear();

$languages = $this->cacheFindQuery('list', array(
'recursive' => -1,
'conditions' => array(
'is_active' => true,
Expand All @@ -231,7 +246,7 @@ public function saveActive($data) {
public function validateActive($data) {
$fieldName = 'Language.code';

$languages = $this->find('list', array(
$languages = $this->cacheFindQuery('list', array(
'fields' => array('code', 'is_active'),
'recursive' => -1,
));
Expand Down
19 changes: 0 additions & 19 deletions Test/Case/Model/Language/GetLanguagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,4 @@ public function testGetLanguages() {
$this->assertEquals($expected, $result);
}

/**
* getLanguages()の既に取得済みの場合のテスト
*
* @return void
*/
public function testGetLanguagesWithData() {
$model = $this->_modelName;
$methodName = $this->_methodName;

Language::$languages = array('test dummy');

//テスト実施
$result = $this->$model->$methodName();

//チェック
$expected = array('test dummy');
$this->assertEquals($expected, $result);
}

}
22 changes: 0 additions & 22 deletions TestSuite/M17nGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,4 @@ public function __construct($name = null, array $data = array(), $dataName = '')
parent::__construct($name, $data, $dataName);
}

/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();

Language::$languages = array();
}

/**
* tearDown method
*
* @return void
*/
public function tearDown() {
Language::$languages = array();

parent::tearDown();
}

}

0 comments on commit d4f099f

Please sign in to comment.