Skip to content

Commit

Permalink
Кэширование
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed May 1, 2021
1 parent 7ddf94d commit c6e47ad
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
8 changes: 5 additions & 3 deletions Services/Factories/ModelElementFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Arrilot\BitrixModels\Models\ElementModel;
use CIBlockElement;
use Exception;
use LogicException;
use Prokl\BitrixModelBundle\Services\Traits\CacheTrait;
use Prokl\BitrixModelBundle\Services\Traits\IblockTrait;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;
Expand All @@ -21,6 +21,7 @@
class ModelElementFactory
{
use IblockTrait;
use CacheTrait;

/**
* @var ServiceLocator $modelLocator Сервисы, помеченные в контейнере тэгом iblock.model.
Expand Down Expand Up @@ -79,7 +80,8 @@ public function getModel(int $idElement) : ElementModel
*/
public function getModelCached(int $idElement) : ElementModel
{
$keyCache = __CLASS__ . __METHOD__ . $idElement;
$keyCache = $this->getCacheKey(__CLASS__ . __METHOD__ . $idElement);

$iblockId = $this->cacher->get(
$keyCache,
/**
Expand Down Expand Up @@ -120,7 +122,7 @@ public function getModelByCodeIblock(string $iblockType, string $iblockCode) : E
*/
public function getModelByCodeIblockCached(string $iblockType, string $iblockCode) : ElementModel
{
$keyCache = __CLASS__ . __METHOD__ . $iblockType . $iblockCode;
$keyCache = $this->getCacheKey(__CLASS__ . __METHOD__ . $iblockType . $iblockCode);
$iblockId = $this->cacher->get(
$keyCache,
/**
Expand Down
8 changes: 5 additions & 3 deletions Services/Factories/ModelSectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Prokl\BitrixModelBundle\Services\Factories;

use Arrilot\BitrixModels\Models\ElementModel;
use Arrilot\BitrixModels\Models\SectionModel;
use CIBlockSection;
use LogicException;
use Prokl\BitrixModelBundle\Services\Traits\CacheTrait;
use Prokl\BitrixModelBundle\Services\Traits\IblockTrait;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;
Expand All @@ -21,6 +21,7 @@
class ModelSectionFactory
{
use IblockTrait;
use CacheTrait;

/**
* @var ServiceLocator $modelLocator Сервисы, помеченные в контейнере тэгом iblock.model.
Expand Down Expand Up @@ -79,7 +80,7 @@ public function getModel(int $idSection) : SectionModel
*/
public function getModelCached(int $idSection) : SectionModel
{
$keyCache = __CLASS__ . __METHOD__ . $idSection;
$keyCache = $this->getCacheKey(__CLASS__ . __METHOD__ . $idSection);
$iblockId = $this->cacher->get(
$keyCache,
/**
Expand Down Expand Up @@ -120,7 +121,8 @@ public function getModelByCodeIblock(string $iblockType, string $iblockCode) : S
*/
public function getModelByCodeIblockCached(string $iblockType, string $iblockCode) : SectionModel
{
$keyCache = __CLASS__ . __METHOD__ . $iblockType . $iblockCode;
$keyCache = $this->getCacheKey(__CLASS__ . __METHOD__ . $iblockType . $iblockCode);

$iblockId = $this->cacher->get(
$keyCache,
/**
Expand Down
28 changes: 28 additions & 0 deletions Services/Traits/CacheTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Prokl\BitrixModelBundle\Services\Traits;

/**
* Trait CacheTrait
* @package Prokl\BitrixModelBundle\Services\Traits
*
* @since 01.05.2021
*/
trait CacheTrait
{
/**
* Нормализация ключа кэша.
*
* @param string $src
*
* @return string
*/
private function getCacheKey(string $src) : string
{
return str_replace(
['{', '}', '\\', '@', '/', ':'],
'',
$src
);
}
}

0 comments on commit c6e47ad

Please sign in to comment.