Skip to content

Commit

Permalink
Кеширование компонентов
Browse files Browse the repository at this point in the history
  • Loading branch information
mzhelskiy committed Feb 3, 2017
1 parent c145810 commit 3a35733
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 29 deletions.
102 changes: 73 additions & 29 deletions classes/modules/component/Component.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,52 +75,96 @@ public function InitComponentsList()
public function LoadAll()
{
/**
* Строим дерево компонентов с учетом зависимостей
* Подгрузка из кеша данных компонентов
*/
$aTree = array();
$this->RetrieveComponentsDataCache();
/**
* Для каждого компонента считываем данные из json
*/
$aComponentsName = array_keys($this->aComponentsList);
foreach ($aComponentsName as $sName) {
list($sComponentPlugin, $sComponentName) = $this->ParseName($sName);
$aTree[$sName] = array();
/**
* Считываем данные компонента
*/
$aData = $this->GetComponentData($sName);
$aData = $aData['json'];
/**
* Используем кеширование построения дерева компонентов
*/
$bCacheUse = Config::Get('module.component.cache_tree');
$sCacheKey = 'components-tree-' . json_encode($aComponentsName);

if (!$bCacheUse or false === ($aTree = $this->Cache_Get($sCacheKey))) {
/**
* Проверяем зависимости
* Строим дерево компонентов с учетом зависимостей
*/
if (isset($aData['dependencies']) and is_array($aData['dependencies'])) {
foreach ($aData['dependencies'] as $mKey => $mValue) {
if (!is_int($mKey) and $mValue === false) {
/**
* Пропускаем отмененную зависимость
*/
continue;
}
$sNameDepend = is_int($mKey) ? $mValue : $mKey;
list($sComponentDependPlugin, $sComponentDependName) = $this->ParseName($sNameDepend);
if (!$sComponentDependPlugin and $sComponentPlugin) {
$sNameDepend = $sComponentPlugin . ':' . $sComponentDependName;
$aTree = array();
foreach ($aComponentsName as $sName) {
list($sComponentPlugin, $sComponentName) = $this->ParseName($sName);
$aTree[$sName] = array();
/**
* Считываем данные компонента
*/
$aData = $this->GetComponentData($sName);
$aData = $aData['json'];
/**
* Проверяем зависимости
*/
if (isset($aData['dependencies']) and is_array($aData['dependencies'])) {
foreach ($aData['dependencies'] as $mKey => $mValue) {
if (!is_int($mKey) and $mValue === false) {
/**
* Пропускаем отмененную зависимость
*/
continue;
}
$sNameDepend = is_int($mKey) ? $mValue : $mKey;
list($sComponentDependPlugin, $sComponentDependName) = $this->ParseName($sNameDepend);
if (!$sComponentDependPlugin and $sComponentPlugin) {
$sNameDepend = $sComponentPlugin . ':' . $sComponentDependName;
}
$aTree[$sName][] = strtolower($sNameDepend);
}
$aTree[$sName][] = strtolower($sNameDepend);
}
}
/**
* Сортируем компоненты с учетом зависимостей
*/
$this->iCountDependsRecursive = 0;
$aTree = $this->GetSortedByDepends($aTree);

if ($bCacheUse) {
$this->Cache_Set($aTree, $sCacheKey, array(), 60 * 60 * 24);
}
}
/**
* Сортируем компоненты с учетом зависимостей
*/
$this->iCountDependsRecursive = 0;
$aTree = $this->GetSortedByDepends($aTree);

/**
* Подключаем каждый компонент
*/
foreach ($aTree as $sName => $aDepends) {
$this->Load($sName);
}
/**
* Информация по компонентам сохраняем в кеше
*/
$this->StoreComponentsDataCache();
}

public function StoreComponentsDataCache()
{
if (!Config::Get('module.component.cache_data')) {
return;
}

$sCacheKey = 'components-data-' . json_encode(array_keys($this->aComponentsList));
$this->Cache_Set($this->aComponentsData, $sCacheKey, array(), 60 * 60 * 24);
}

public function RetrieveComponentsDataCache()
{
if (!Config::Get('module.component.cache_data')) {
return;
}
$sCacheKey = 'components-data-' . json_encode(array_keys($this->aComponentsList));
if (false !== ($aComponentsData = $this->Cache_Get($sCacheKey))) {
foreach ($aComponentsData as $sName => $aData) {
$this->aComponentsData[$sName] = $aData;
}
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@
$config['module']['asset']['css']['compress'] = false; // указывает на необходимость компрессии файлов. Компрессия используется только в активированном режиме слияния файлов.
$config['module']['asset']['js']['merge'] = true; // указывает на необходимость слияния js файлов
$config['module']['asset']['js']['compress'] = false; // указывает на необходимость компрессии файлов. Компрессия используется только в активированном режиме слияния файлов.
// Модель Component
$config['module']['component']['cache_tree'] = false; // кешировать или нет построение дерева компонентов
$config['module']['component']['cache_data'] = false; // кешировать или нет данные компонентов
// Модуль Security
$config['module']['security']['hash'] = "livestreet_security_key"; // "примесь" к строке, хешируемой в качестве security-кода
// Модуль Cron
Expand Down

0 comments on commit 3a35733

Please sign in to comment.