Skip to content

Commit

Permalink
Изменен механизм кэширования.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed May 2, 2021
1 parent c6e47ad commit 86f0408
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 149 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/ModelExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function load(array $configs, ContainerBuilder $container) : void
);

$loader->load('services.yaml');
$loader->load('caching.yaml');

// Фасады подтягиваются только, если установлен соответствующий бандл.
if (class_exists('Prokl\FacadeBundle\Services\AbstractFacade')) {
Expand Down
38 changes: 38 additions & 0 deletions Resources/config/caching.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: true

# Кэш
model_bundle.cacher:
public: false
class: WebArch\BitrixCache\AntiStampedeCacheAdapter
arguments: ['/', '%bitrix-model.ttl_cache%', '%bitrix-model.cache_dir%']

model_bundle.reflection_processor:
public: false
class: Prokl\CacheProxificator\ReflectionProcessor

model_bundle.factory_elements_cached:
class: Prokl\BitrixModelBundle\Services\Factories\ModelElementFactoryCached
arguments:
- '@model_bundle.factory_elements'
- '@model_bundle.cacher'
- '@model_bundle.reflection_processor'
- ['getModel', 'getModelByCodeIblock', 'getModelByIdIblock']
- '@model_bundle.env_resolver_factory'

Prokl\BitrixModelBundle\Services\Factories\ModelElementFactoryCached: 'model_bundle.factory_elements_cached'

model_bundle.factory_sections_cached:
class: Prokl\BitrixModelBundle\Services\Factories\ModelSectionFactoryCached
arguments:
- '@model_bundle.factory_elements'
- '@model_bundle.cacher'
- '@model_bundle.reflection_processor'
- ['getModel', 'getModelByCodeIblock', 'getModelByIdIblock']
- '@model_bundle.env_resolver_factory'

Prokl\BitrixModelBundle\Services\Factories\ModelSectionFactoryCached: 'model_bundle.factory_elements_cached'

2 changes: 2 additions & 0 deletions Resources/config/facades.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ services:
public: true

Prokl\BitrixModelBundle\Services\Facades\ModelElementFacade: ~
Prokl\BitrixModelBundle\Services\Facades\ModelElementFacadeCached: ~
Prokl\BitrixModelBundle\Services\Facades\ModelSectionFacade: ~
Prokl\BitrixModelBundle\Services\Facades\ModelSectionFacadeCached: ~
Prokl\BitrixModelBundle\Services\Facades\D7ModelFacade: ~
16 changes: 10 additions & 6 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ services:
model_bundle.factory_elements:
public: true
class: Prokl\BitrixModelBundle\Services\Factories\ModelElementFactory
arguments: [!tagged_locator { tag: 'iblock.element.model', index_by: 'key' }, '@model_bundle.ciblockelement', '@model_bundle.cacher']
arguments: [!tagged_locator { tag: 'iblock.element.model', index_by: 'key' }, '@model_bundle.ciblockelement']

Prokl\BitrixModelBundle\Services\Factories\ModelElementFactory: '@model_bundle.factory_elements'

model_bundle.factory_sections:
public: true
class: Prokl\BitrixModelBundle\Services\Factories\ModelSectionFactory
arguments: [!tagged_locator { tag: 'iblock.section.model', index_by: 'key' }, '@model_bundle.ciblocksection', '@model_bundle.cacher']
arguments: [!tagged_locator { tag: 'iblock.section.model', index_by: 'key' }, '@model_bundle.ciblocksection']

Prokl\BitrixModelBundle\Services\Factories\ModelSectionFactory: '@model_bundle.factory_sections'

Expand All @@ -33,8 +33,12 @@ services:

Prokl\BitrixModelBundle\Services\Factories\D7ModelFactory: '@model_bundle.d7_model'

# Кэш
model_bundle.cacher:
model_bundle.env_resolver:
public: false
class: WebArch\BitrixCache\AntiStampedeCacheAdapter
arguments: ['/', '%bitrix-model.ttl_cache%', '%bitrix-model.cache_dir%']
class: Prokl\BitrixModelBundle\Services\Utils\EnvironmentResolver
arguments: ['%env(resolve:DEBUG)%']

model_bundle.env_resolver_factory:
public: false
class: Prokl\BitrixModelBundle\Services\Utils\EnvironmentResolver
factory: ['@model_bundle.env_resolver', 'getEnv']
2 changes: 0 additions & 2 deletions Services/Facades/ModelElementFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
* @since 30.04.2021
*
* @method static getModel(int $idElement)
* @method static getModelCached(int $idElement)
* @method static getModelByIdIblock(int $iblockId)
* @method static getModelByCodeIblock(string $iblockType, string $iblockCode)
* @method static getModelByCodeIblockCached(string $iblockType, string $iblockCode)
*/
class ModelElementFacade extends AbstractFacade
{
Expand Down
26 changes: 26 additions & 0 deletions Services/Facades/ModelElementFacadeCached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Prokl\BitrixModelBundle\Services\Facades;

use Prokl\FacadeBundle\Services\AbstractFacade;

/**
* Class ModelElementFacade
* @package Prokl\BitrixModelBundle\Services\Facades
*
* @since 02.05.2021
*
* @method static getModel(int $idElement)
* @method static getModelByIdIblock(int $iblockId)
* @method static getModelByCodeIblock(string $iblockType, string $iblockCode)
*/
class ModelElementFacadeCached extends AbstractFacade
{
/**
* @inheritDoc
*/
protected static function getFacadeAccessor() : string
{
return 'model_bundle.factory_elements_cached';
}
}
2 changes: 0 additions & 2 deletions Services/Facades/ModelSectionFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
* @since 30.04.2021
*
* @method static getModel(int $idElement)
* @method static getModelCached(int $idElement)
* @method static getModelByIdIblock(int $iblockId)
* @method static getModelByCodeIblock(string $iblockType, string $iblockCode)
* @method static getModelByCodeIblockCached(string $iblockType, string $iblockCode)
*/
class ModelSectionFacade extends AbstractFacade
{
Expand Down
26 changes: 26 additions & 0 deletions Services/Facades/ModelSectionFacadeCached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Prokl\BitrixModelBundle\Services\Facades;

use Prokl\FacadeBundle\Services\AbstractFacade;

/**
* Class ModelSectionFacadeCached
* @package Prokl\BitrixModelBundle\Services\Facades
*
* @since 02.05.2021
*
* @method static getModel(int $idElement)
* @method static getModelByIdIblock(int $iblockId)
* @method static getModelByCodeIblock(string $iblockType, string $iblockCode)
*/
class ModelSectionFacadeCached extends AbstractFacade
{
/**
* @inheritDoc
*/
protected static function getFacadeAccessor() : string
{
return 'model_bundle.factory_sections';
}
}
67 changes: 1 addition & 66 deletions Services/Factories/ModelElementFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
use Arrilot\BitrixModels\Models\ElementModel;
use CIBlockElement;
use LogicException;
use Prokl\BitrixModelBundle\Services\Traits\CacheTrait;
use Prokl\BitrixModelBundle\Services\Traits\IblockTrait;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Contracts\Cache\CacheInterface;

/**
* Class ModelElementFactory
Expand All @@ -21,7 +17,6 @@
class ModelElementFactory
{
use IblockTrait;
use CacheTrait;

/**
* @var ServiceLocator $modelLocator Сервисы, помеченные в контейнере тэгом iblock.model.
Expand All @@ -33,26 +28,18 @@ class ModelElementFactory
*/
private $blockElement;

/**
* @var CacheInterface $cacher Кэшер.
*/
private $cacher;

/**
* ModelElementFactory constructor.
*
* @param ServiceLocator $locator Сервисы, помеченные в контейнере тэгом iblock.model.
* @param CIBlockElement $blockElement Битриксовый CIBlockElement.
* @param CacheInterface $cacher Кэшер.
*/
public function __construct(
ServiceLocator $locator,
CIBlockElement $blockElement,
CacheInterface $cacher
CIBlockElement $blockElement
) {
$this->modelLocator = $locator;
$this->blockElement = $blockElement;
$this->cacher = $cacher;
}

/**
Expand All @@ -70,32 +57,6 @@ public function getModel(int $idElement) : ElementModel
return $this->getModelByIdIblock($iblockId);
}

/**
* Модель по ID элемента. С использованием кэша.
*
* @param integer $idElement ID элемента.
*
* @return ElementModel
* @throws LogicException|InvalidArgumentException
*/
public function getModelCached(int $idElement) : ElementModel
{
$keyCache = $this->getCacheKey(__CLASS__ . __METHOD__ . $idElement);

$iblockId = $this->cacher->get(
$keyCache,
/**
* @param CacheItemInterface $item
* @return mixed
*/
function (CacheItemInterface $item) use ($idElement) {
return $this->getIblockId($idElement);
}
);

return $this->getModelByIdIblock($iblockId);
}

/**
* Модель по коду и типу инфоблока.
*
Expand All @@ -111,32 +72,6 @@ public function getModelByCodeIblock(string $iblockType, string $iblockCode) : E
return $this->getModelByIdIblock($iblockId);
}

/**
* Модель по коду и типу инфоблока. Кэширование.
*
* @param string $iblockType Тип инфоблока.
* @param string $iblockCode Код инфоблока.
*
* @return ElementModel
* @throws InvalidArgumentException
*/
public function getModelByCodeIblockCached(string $iblockType, string $iblockCode) : ElementModel
{
$keyCache = $this->getCacheKey(__CLASS__ . __METHOD__ . $iblockType . $iblockCode);
$iblockId = $this->cacher->get(
$keyCache,
/**
* @param CacheItemInterface $item
* @return mixed
*/
function (CacheItemInterface $item) use ($iblockType, $iblockCode) {
return $this->getIBlockIdByCode($iblockType, $iblockCode);
}
);

return $this->getModelByIdIblock($iblockId);
}

/**
* Модель по ID инфоблока.
*
Expand Down
16 changes: 16 additions & 0 deletions Services/Factories/ModelElementFactoryCached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Prokl\BitrixModelBundle\Services\Factories;

use Prokl\CacheProxificator\CacheProxificator;

/**
* Class ModelElementFactoryCached
* @package Prokl\BitrixModelBundle\Services\Factories
*
* @since 02.05.2021
*/
class ModelElementFactoryCached extends CacheProxificator
{

}
Loading

0 comments on commit 86f0408

Please sign in to comment.