From c45efc1d6e96be70b52e1d9fcb37600f56453f8e Mon Sep 17 00:00:00 2001 From: Ms_Natali Date: Tue, 29 Jan 2019 09:49:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=82=D0=B5=D0=B3=D0=B3=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=B5=D1=88=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=20=D0=98=D0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BitrixWrapper.php | 45 ++++++++++++++++++++++++++++++++++++ src/Queries/BaseQuery.php | 15 +++++++++++- src/Queries/ElementQuery.php | 11 +++++++++ tests/ElementModelTest.php | 1 + tests/SectionModelTest.php | 1 + tests/Stubs/COption.php | 23 ++++++++++++++++++ tests/Stubs/CacheManager.php | 20 ++++++++++++++++ tests/TestCase.php | 8 +++++++ tests/UserModelTest.php | 1 + 9 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/BitrixWrapper.php create mode 100644 tests/Stubs/COption.php create mode 100644 tests/Stubs/CacheManager.php diff --git a/src/BitrixWrapper.php b/src/BitrixWrapper.php new file mode 100644 index 0000000..63d6b66 --- /dev/null +++ b/src/BitrixWrapper.php @@ -0,0 +1,45 @@ +initCache($minutes * 60, $key, '/bitrix-models')) { + if ($cache->initCache($minutes * 60, $key, static::$cacheDir)) { $vars = $cache->getVars(); return !empty($vars['isCollection']) ? new Collection($vars['cache']) : $vars['cache']; } @@ -577,4 +580,14 @@ protected function prepareMultiFilter(&$key, &$value) { } + + /** + * Проверка включен ли тегированный кеш + * @return bool + */ + protected function isManagedCacheOn() + { + $config = BitrixWrapper::configProvider(); + return $config::GetOptionString('main', 'component_managed_cache_on', 'N') == 'Y'; + } } diff --git a/src/Queries/ElementQuery.php b/src/Queries/ElementQuery.php index 9f09ac2..bf6d9cb 100644 --- a/src/Queries/ElementQuery.php +++ b/src/Queries/ElementQuery.php @@ -3,6 +3,7 @@ namespace Arrilot\BitrixModels\Queries; use Arrilot\BitrixCacher\Cache; +use Arrilot\BitrixModels\BitrixWrapper; use CIBlock; use Illuminate\Support\Collection; use Arrilot\BitrixModels\Models\ElementModel; @@ -159,6 +160,11 @@ protected function loadModels() list($select, $chunkQuery) = $this->multiplySelectForMaxJoinsRestrictionIfNeeded($select); $callback = function() use ($sort, $filter, $groupBy, $navigation, $select, $chunkQuery) { + if (static::isManagedCacheOn()) { + BitrixWrapper::cacheManagerProvider()->StartTagCache(static::$cacheDir); + BitrixWrapper::cacheManagerProvider()->RegisterTag("iblock_id_new"); + } + if ($chunkQuery) { $itemsChunks = []; foreach ($select as $chunkIndex => $selectForChunk) { @@ -176,6 +182,11 @@ protected function loadModels() $this->addItemToResultsUsingKeyBy($items, new $this->modelName($arItem['ID'], $arItem)); } } + + if (static::isManagedCacheOn()) { + BitrixWrapper::cacheManagerProvider()->EndTagCache(); + } + return new Collection($items); }; diff --git a/tests/ElementModelTest.php b/tests/ElementModelTest.php index 9340af1..e423cc0 100644 --- a/tests/ElementModelTest.php +++ b/tests/ElementModelTest.php @@ -14,6 +14,7 @@ public function setUp() { TestElement::$bxObject = m::mock('obj'); ElementQuery::$cIblockObject = m::mock('cIblockObject'); + parent::setUp(); } public function tearDown() diff --git a/tests/SectionModelTest.php b/tests/SectionModelTest.php index 647e8cd..6234d38 100644 --- a/tests/SectionModelTest.php +++ b/tests/SectionModelTest.php @@ -10,6 +10,7 @@ class SectionModelTest extends TestCase public function setUp() { TestSection::$bxObject = m::mock('obj'); + parent::setUp(); } public function tearDown() diff --git a/tests/Stubs/COption.php b/tests/Stubs/COption.php new file mode 100644 index 0000000..94966db --- /dev/null +++ b/tests/Stubs/COption.php @@ -0,0 +1,23 @@ + [ + 'component_managed_cache_on' => 'Y', + ] + ]; + + public static function GetOptionInt($module_id, $name, $def = "", $site = false) + { + return intval(static::GetOptionString($module_id, $name, $def, $site)); + } + + public static function GetOptionString($module_id, $name, $def = "", $site = false) + { + return static::$config[$module_id][$name] ?: $def; + } +} \ No newline at end of file diff --git a/tests/Stubs/CacheManager.php b/tests/Stubs/CacheManager.php new file mode 100644 index 0000000..e410529 --- /dev/null +++ b/tests/Stubs/CacheManager.php @@ -0,0 +1,20 @@ +