Skip to content

Commit

Permalink
MC-32062: [2.4] Deliver first part of Redis improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vzabaznov committed Mar 2, 2020
1 parent 6bcc25b commit 4b66b00
Show file tree
Hide file tree
Showing 21 changed files with 532 additions and 208 deletions.
12 changes: 7 additions & 5 deletions app/code/Magento/Eav/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ class Config

/**
* @param \Magento\Framework\App\CacheInterface $cache
* @param \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory
* @param \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory
* @param Entity\TypeFactory $entityTypeFactory
* @param ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory
* @param \Magento\Framework\App\Cache\StateInterface $cacheState
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
* @param SerializerInterface $serializer
* @param ScopeConfigInterface $scopeConfig
* @param SerializerInterface|null $serializer
* @param ScopeConfigInterface|null $scopeConfig
* @param array $attributesForPreload
* @codeCoverageIgnore
*/
Expand Down Expand Up @@ -374,7 +374,9 @@ protected function _initEntityTypes()
}
\Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]);

if ($this->isCacheEnabled() && ($cache = $this->_cache->load(self::ENTITIES_CACHE_ID))) {
if ($this->isCacheEnabled() &&
($cache = $this->_cache->load(self::ENTITIES_CACHE_ID))
) {
$this->_entityTypeData = $this->serializer->unserialize($cache);
foreach ($this->_entityTypeData as $typeCode => $data) {
$typeId = $data['entity_type_id'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Magento\Eav\Model\ResourceModel\Entity\Attribute;

/**
* Basic implementation for attribute sets
*/
class Set extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
Expand All @@ -24,8 +27,6 @@ class Set extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
protected $eavConfig;

/**
* Constructor
*
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param GroupFactory $attrGroupFactory
* @param \Magento\Eav\Model\Config $eavConfig
Expand Down Expand Up @@ -54,7 +55,7 @@ protected function _construct()
}

/**
* Perform actions after object save
* Perform actions after object save.
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/Eav/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,14 @@
</argument>
</arguments>
</type>
<virtualType name="configured_eav_cache" type="Magento\Framework\App\Cache">
<arguments>
<argument name="cacheIdentifier" xsi:type="string">eav</argument>

This comment has been minimized.

Copy link
@ilnytskyi

ilnytskyi Dec 11, 2020

Contributor

it does not work with the remote sync cache.
If we have different name in configuration of frontend cache let's say I have
l2 name for for that cache and I want to use it with EAV cache

I set in env.php

        'type' => [
            'config' => [
                'frontend' => 'l2'
            ],
            'eav' => [
                'frontend' => 'l2'
            ],

but because of this virtual type has eav value I should either define a sepparate backend with eav name or rename my l2 to eav to be able to work with this cache type
Selection_235

</arguments>
</virtualType>
<type name="Magento\Eav\Model\Config">
<arguments>
<argument name="cache" xsi:type="object">configured_eav_cache</argument>
</arguments>
</type>
</config>
20 changes: 20 additions & 0 deletions app/code/Magento/Theme/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,24 @@
<argument name="identifierName" xsi:type="string">theme_id</argument>
</arguments>
</type>
<virtualType name="configured_design_cache" type="Magento\Framework\App\Cache">
<arguments>
<argument name="cacheIdentifier" xsi:type="string">layout</argument>
</arguments>
</virtualType>
<virtualType name="design_context" type="Magento\Framework\Model\Context">
<arguments>
<argument name="cacheManager" xsi:type="object">configured_design_cache</argument>
</arguments>
</virtualType>
<type name="Magento\Theme\Model\Design">
<arguments>
<argument name="context" xsi:type="object">design_context</argument>
</arguments>
</type>
<type name="Magento\Theme\Model\Theme\ThemeProvider">
<arguments>
<argument name="cache" xsi:type="object">configured_design_cache</argument>
</arguments>
</type>
</config>
10 changes: 10 additions & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1804,4 +1804,14 @@
</type>
<preference for="Magento\Framework\GraphQl\Query\ErrorHandlerInterface" type="Magento\Framework\GraphQl\Query\ErrorHandler"/>
<preference for="Magento\Framework\Filter\VariableResolverInterface" type="Magento\Framework\Filter\VariableResolver\StrategyResolver"/>
<virtualType name="configured_block_cache" type="Magento\Framework\App\Cache">
<arguments>
<argument name="cacheIdentifier" xsi:type="string">block_html</argument>
</arguments>
</virtualType>
<type name="Magento\Framework\View\Element\Context">
<arguments>
<argument name="cache" xsi:type="object">configured_block_cache</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function testRemoteSynchronizedCache()
//Removing data
sleep(2);
$this->assertTrue($this->model->remove($secondIdentifier));
$this->assertTrue($this->model->remove($identifier));
$this->assertEquals($this->model->load($identifier), false);
$this->assertEquals($this->model->load($secondIdentifier), false);

Expand All @@ -73,11 +74,5 @@ public function testRemoteSynchronizedCache()
//Checking data
$this->assertEquals($this->model->load($identifier), $data);
$this->assertEquals($this->model->load($secondIdentifier), $secondData);

//Removing data
sleep(2);
$this->assertTrue($this->model->remove($identifier));
$this->assertEquals($this->model->load($identifier), false);
$this->assertEquals($this->model->load($secondIdentifier), false);
}
}
14 changes: 7 additions & 7 deletions lib/internal/Magento/Framework/App/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* See COPYING.txt for license details.
*/

/**
* System cache model
* support id and tags prefix support,
*/
namespace Magento\Framework\App;

/**
* System cache model support id and tags prefix support.
*/
class Cache implements CacheInterface
{
/**
Expand All @@ -30,12 +29,13 @@ class Cache implements CacheInterface
protected $_frontend;

/**
* @param \Magento\Framework\App\Cache\Frontend\Pool $frontendPool
* @param Cache\Frontend\Pool $frontendPool
* @param string|null $cacheIdentifier
*/
public function __construct(\Magento\Framework\App\Cache\Frontend\Pool $frontendPool)
public function __construct(\Magento\Framework\App\Cache\Frontend\Pool $frontendPool, $cacheIdentifier = null)
{
$this->_frontendPool = $frontendPool;
$this->_frontend = $frontendPool->get($this->_frontendIdentifier);
$this->_frontend = $frontendPool->get($cacheIdentifier ?? $this->_frontendIdentifier);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/internal/Magento/Framework/App/Cache/Frontend/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ public function get($identifier)
if (isset($this->_instances[$identifier])) {
return $this->_instances[$identifier];
}
throw new \InvalidArgumentException("Cache frontend '{$identifier}' is not recognized.");

if (!isset($this->_instances[self::DEFAULT_FRONTEND_ID])) {
throw new \InvalidArgumentException(
"Cache frontend '{$identifier}' is not recognized. As well as " .
self::DEFAULT_FRONTEND_ID .
"cache is not configured"
);
}

return $this->_instances[self::DEFAULT_FRONTEND_ID];
}
}
26 changes: 21 additions & 5 deletions lib/internal/Magento/Framework/App/Router/ActionList.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Framework\App\Router;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\State;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Serialize\Serializer\Serialize;
use Magento\Framework\Module\Dir\Reader as ModuleReader;
Expand Down Expand Up @@ -70,12 +72,26 @@ public function __construct(
$this->reservedWords = array_merge($reservedWords, $this->reservedWords);
$this->actionInterface = $actionInterface;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Serialize::class);
$data = $cache->load($cacheKey);
if (!$data) {
$this->actions = $moduleReader->getActionFiles();
$cache->save($this->serializer->serialize($this->actions), $cacheKey);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get(State::class);

if ($state->getMode() === State::MODE_PRODUCTION) {
$directoryList = $objectManager->get(DirectoryList::class);
$file = $directoryList->getPath(DirectoryList::GENERATED_METADATA) . '/' . $cacheKey . '.' . 'php';

if (file_exists($file)) {
$this->actions = (include $file) ?? $moduleReader->getActionFiles();
} else {
$this->actions = $moduleReader->getActionFiles();
}
} else {
$this->actions = $this->serializer->unserialize($data);
$data = $cache->load($cacheKey);
if (!$data) {
$this->actions = $moduleReader->getActionFiles();
$cache->save($this->serializer->serialize($this->actions), $cacheKey);
} else {
$this->actions = $this->serializer->unserialize($data);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,8 @@ public function testGet()
}
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cache frontend 'unknown' is not recognized
*/
public function testGetUnknownFrontendId()
public function testFallbackOnDefault()
{
$this->_model->get('unknown');
$this->assertSame($this->_frontendInstances[Pool::DEFAULT_FRONTEND_ID], $this->_model->get('unknown'));
}
}
83 changes: 83 additions & 0 deletions lib/internal/Magento/Framework/Cache/Backend/Redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Cache\Backend;

/**
* Redis wrapper to extend current implementation behaviour.
*/
class Redis extends \Cm_Cache_Backend_Redis
{
/**
* Local state of preloaded keys.
*
* @var array
*/
private $preloadedData = [];

/**
* Array of keys to be preloaded.
*
* @var array
*/
private $preloadKeys = [];

/**
* @param array $options
*/
public function __construct($options = [])
{
$this->preloadKeys = $options['preload_keys'] ?? [];
parent::__construct($options);
}

/**
* Load value with given id from cache
*
* @param string $id Cache id
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
* @return bool|string
*/
public function load($id, $doNotTestCacheValidity = false)
{
if (!empty($this->preloadKeys) && empty($this->preloadedData)) {
$redis = $this->_slave ?? $this->_redis;
$redis = $redis->pipeline();

foreach ($this->preloadKeys as $key) {
$redis->hGet(self::PREFIX_KEY . $key, self::FIELD_DATA);
}

$this->preloadedData = array_filter(array_combine($this->preloadKeys, $redis->exec()));
}

if (isset($this->preloadedData[$id])) {
return $this->_decodeData($this->preloadedData[$id]);
}

return parent::load($id, $doNotTestCacheValidity);
}

/**
* Cover errors on save operations, which may occurs when Redis cannot evict keys, which is expected in some cases.
*
* @param string $data
* @param string $id
* @param array $tags
* @param bool $specificLifetime
* @return bool
*/
public function save($data, $id, $tags = [], $specificLifetime = false)
{
try {
parent::save($data, $id, $tags, $specificLifetime);
} catch (\Throwable $exception) {
return false;
}

return true;
}
}
Loading

0 comments on commit 4b66b00

Please sign in to comment.