cache optimization

This commit is contained in:
Taras Machyshyn
2015-01-08 13:10:41 +02:00
parent e2fa620a81
commit ed1a58cd1f
3 changed files with 56 additions and 29 deletions

View File

@@ -33,6 +33,18 @@ class Language
private $preferences;
private $unifier;
/**
* Data of all languages
*
* @var array
*/
private $allData = array();
/**
* Data of current language
*
* @var array
*/
private $data = null;
private $name = 'i18n';
@@ -82,7 +94,6 @@ class Language
return $this->unifier;
}
public function getLanguage()
{
if (!isset($this->currentLanguage) && isset($this->preferences)) {
@@ -182,7 +193,6 @@ class Language
return $this->get();
}
/**
* Get data of Unifier language files
*
@@ -230,20 +240,24 @@ class Language
return $result;
}
protected function init($reload = false)
{
if ($reload || !file_exists($this->getLangCacheFile()) || !$this->getConfig()->get('useCache')) {
$this->fullData = $this->getUnifier()->unify($this->name, $this->paths, true);
$fullData = $this->getUnifier()->unify($this->name, $this->paths, true);
$result = true;
foreach ($this->fullData as $i18nName => $i18nData) {
$i18nCacheFile = str_replace('{*}', $i18nName, $this->cacheFile);
foreach ($fullData as $i18nName => $i18nData) {
if ($i18nName != $this->defaultLanguage) {
$i18nData = Util::merge($this->fullData[$this->defaultLanguage], $i18nData);
$i18nData = Util::merge($fullData[$this->defaultLanguage], $i18nData);
}
$this->allData[$i18nName] = $i18nData;
if ($this->getConfig()->get('useCache')) {
$i18nCacheFile = str_replace('{*}', $i18nName, $this->cacheFile);
$result &= $this->getFileManager()->putContentsPHP($i18nCacheFile, $i18nData);
}
$result &= $this->getFileManager()->putContentsPHP($i18nCacheFile, $i18nData);
}
if ($result == false) {
@@ -251,7 +265,12 @@ class Language
}
}
$this->data = $this->getFileManager()->getContents($this->getLangCacheFile());
$currentLanguage = $this->getLanguage();
if (empty($this->allData[$currentLanguage])) {
$this->allData[$currentLanguage] = $this->getFileManager()->getContents($this->getLangCacheFile());
}
$this->data = $this->allData[$currentLanguage];
}
protected function normalizeDefs($label, $value, $category)

View File

@@ -114,9 +114,11 @@ class Metadata
if ($reload) {
//save medatada to a cache file
$isSaved = $this->getFileManager()->putContentsPHP($this->cacheFile, $data);
if ($isSaved === false) {
$GLOBALS['log']->emergency('Metadata:init() - metadata has not been saved to a cache file');
if ($this->getConfig()->get('useCache')) {
$isSaved = $this->getFileManager()->putContentsPHP($this->cacheFile, $data);
if ($isSaved === false) {
$GLOBALS['log']->emergency('Metadata:init() - metadata has not been saved to a cache file');
}
}
}
}
@@ -288,16 +290,20 @@ class Metadata
$this->getConverter()->process();
}
$this->ormMeta = $this->getFileManager()->getContents($this->ormCacheFile);
if (empty($this->ormMeta)) {
$this->ormMeta = $this->getFileManager()->getContents($this->ormCacheFile);
}
return $this->ormMeta;
}
public function setOrmMetadata(array $ormMeta)
{
$result = $this->getFileManager()->putContentsPHP($this->ormCacheFile, $ormMeta);
if ($result == false) {
throw new \Espo\Core\Exceptions\Error('Metadata::setOrmMetadata() - Cannot save ormMetadata to a file');
if ($this->getConfig()->get('useCache')) {
$result = $this->getFileManager()->putContentsPHP($this->ormCacheFile, $ormMeta);
if ($result == false) {
throw new \Espo\Core\Exceptions\Error('Metadata::setOrmMetadata() - Cannot save ormMetadata to a file');
}
}
$this->ormMeta = $ormMeta;

View File

@@ -18,7 +18,7 @@
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
************************************************************************/
namespace Espo\Core\Utils;
@@ -35,7 +35,7 @@ class Route
protected $paths = array(
'corePath' => 'application/Espo/Resources/routes.json',
'modulePath' => 'application/Espo/Modules/{*}/Resources/routes.json',
'customPath' => 'custom/Espo/Custom/Resources/routes.json',
'customPath' => 'custom/Espo/Custom/Resources/routes.json',
);
public function __construct(Config $config, Metadata $metadata, File\Manager $fileManager)
@@ -99,10 +99,12 @@ class Route
} else {
$this->data = $this->unify();
$result = $this->getFileManager()->putContentsPHP($this->cacheFile, $this->data);
if ($result == false) {
throw new \Espo\Core\Exceptions\Error('Route - Cannot save unified routes');
}
if ($this->getConfig()->get('useCache')) {
$result = $this->getFileManager()->putContentsPHP($this->cacheFile, $this->data);
if ($result == false) {
throw new \Espo\Core\Exceptions\Error('Route - Cannot save unified routes');
}
}
}
}
@@ -110,10 +112,10 @@ class Route
{
$data = array();
$data = $this->getAddData($data, $this->paths['customPath']);
$data = $this->getAddData($data, $this->paths['customPath']);
foreach ($this->getMetadata()->getModuleList() as $moduleName) {
$modulePath = str_replace('{*}', $moduleName, $this->paths['modulePath']);
$modulePath = str_replace('{*}', $moduleName, $this->paths['modulePath']);
$data = $this->getAddData($data, $modulePath);
}
@@ -145,11 +147,11 @@ class Route
return $data;
}
foreach($newData as $route) {
foreach($newData as $route) {
$route['route'] = $this->adjustPath($route['route']);
$data[] = $route;
$data[] = $route;
}
return $data;
@@ -167,7 +169,7 @@ class Route
$routePath = trim($routePath);
if ( substr($routePath,0,1) != '/') {
return '/'.$routePath;
return '/'.$routePath;
}
return $routePath;