Skip to content

Commit

Permalink
Merge pull request #2848 from magento-performance/MAGETWO-93184
Browse files Browse the repository at this point in the history
[Performance] MAGETWO-93184: [Forwardport] Static Assets deployment throws errors when redis is used for cache
  • Loading branch information
duhon authored Jul 17, 2018
2 parents 34da3bd + bd21cc0 commit 1d469f8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 24 deletions.
20 changes: 11 additions & 9 deletions lib/internal/Magento/Framework/App/Cache/Frontend/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,17 @@ public function create(array $options)
$result = $this->_objectManager->create(
\Magento\Framework\Cache\Frontend\Adapter\Zend::class,
[
'frontend' => \Zend_Cache::factory(
$frontend['type'],
$backend['type'],
$frontend,
$backend['options'],
true,
true,
true
)
'frontendFactory' => function () use ($frontend, $backend) {
return \Zend_Cache::factory(
$frontend['type'],
$backend['type'],
$frontend,
$backend['options'],
true,
true,
true
);
}
]
);
$result = $this->_applyDecorators($result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected function _buildModelForCreate($enforcedOptions = [], $decorators = [])
$processFrontendFunc = function ($class, $params) {
switch ($class) {
case \Magento\Framework\Cache\Frontend\Adapter\Zend::class:
return new $class($params['frontend']);
return new $class($params['frontendFactory']);
case \Magento\Framework\App\Test\Unit\Cache\Frontend\FactoryTest\CacheDecoratorDummy::class:
$frontend = $params['frontend'];
unset($params['frontend']);
Expand Down
53 changes: 43 additions & 10 deletions lib/internal/Magento/Framework/Cache/Frontend/Adapter/Zend.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,66 @@ class Zend implements \Magento\Framework\Cache\FrontendInterface
protected $_frontend;

/**
* @param \Zend_Cache_Core $frontend
* Factory that creates the \Zend_Cache_Cores
*
* @var \Closure
*/
private $frontendFactory;

/**
* The pid that owns the $_frontend object
*
* @var int
*/
public function __construct(\Zend_Cache_Core $frontend)
private $pid;

/**
* @param \Closure $frontendFactory
*/
public function __construct(\Closure $frontendFactory)
{
$this->_frontend = $frontend;
$this->frontendFactory = $frontendFactory;
$this->_frontend = $frontendFactory();
$this->pid = getmypid();
}

/**
* {@inheritdoc}
*/
public function test($identifier)
{
return $this->_frontend->test($this->_unifyId($identifier));
return $this->getFrontEnd()->test($this->_unifyId($identifier));
}

/**
* {@inheritdoc}
*/
public function load($identifier)
{
return $this->_frontend->load($this->_unifyId($identifier));
return $this->getFrontEnd()->load($this->_unifyId($identifier));
}

/**
* {@inheritdoc}
*/
public function save($data, $identifier, array $tags = [], $lifeTime = null)
{
return $this->_frontend->save($data, $this->_unifyId($identifier), $this->_unifyIds($tags), $lifeTime);
return $this->getFrontEnd()->save($data, $this->_unifyId($identifier), $this->_unifyIds($tags), $lifeTime);
}

/**
* {@inheritdoc}
*/
public function remove($identifier)
{
return $this->_frontend->remove($this->_unifyId($identifier));
return $this->getFrontEnd()->remove($this->_unifyId($identifier));
}

/**
* {@inheritdoc}
*
* @throws \InvalidArgumentException Exception is thrown when non-supported cleaning mode is specified
* @throws \Zend_Cache_Exception
*/
public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = [])
{
Expand All @@ -76,23 +93,23 @@ public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = [])
"Magento cache frontend does not support the cleaning mode '{$mode}'."
);
}
return $this->_frontend->clean($mode, $this->_unifyIds($tags));
return $this->getFrontEnd()->clean($mode, $this->_unifyIds($tags));
}

/**
* {@inheritdoc}
*/
public function getBackend()
{
return $this->_frontend->getBackend();
return $this->getFrontEnd()->getBackend();
}

/**
* {@inheritdoc}
*/
public function getLowLevelFrontend()
{
return $this->_frontend;
return $this->getFrontEnd();
}

/**
Expand All @@ -119,4 +136,20 @@ protected function _unifyIds(array $ids)
}
return $ids;
}

/**
* Get frontEnd cache adapter for current pid
*
* @return \Zend_Cache_Core
*/
private function getFrontEnd()
{
if (getmypid() === $this->pid) {
return $this->_frontend;
}
$frontendFactory = $this->frontendFactory;
$this->_frontend = $frontendFactory();
$this->pid = getmypid();
return $this->_frontend;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class ZendTest extends \PHPUnit\Framework\TestCase
public function testProxyMethod($method, $params, $expectedParams, $expectedResult)
{
$frontendMock = $this->createMock(\Zend_Cache_Core::class);
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendMock);
$frontendFactory = function () use ($frontendMock) {
return $frontendMock;
};
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendFactory);
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ProxyTesting();
$result = $helper->invokeWithExpectations(
$object,
Expand Down Expand Up @@ -82,7 +85,11 @@ public function testCleanException($cleaningMode, $expectedErrorMessage)
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage($expectedErrorMessage);
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($this->createMock(\Zend_Cache_Core::class));
$frontendMock = $this->createMock(\Zend_Cache_Core::class);
$frontendFactory = function () use ($frontendMock) {
return $frontendMock;
};
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendFactory);
$object->clean($cleaningMode);
}

Expand Down Expand Up @@ -110,7 +117,10 @@ public function cleanExceptionDataProvider()
public function testGetLowLevelFrontend()
{
$frontendMock = $this->createMock(\Zend_Cache_Core::class);
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendMock);
$frontendFactory = function () use ($frontendMock) {
return $frontendMock;
};
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendFactory);
$this->assertSame($frontendMock, $object->getLowLevelFrontend());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public function proxyMethodDataProvider()
{
$backend = new \Zend_Cache_Backend_BlackHole();
$adaptee = $this->createMock(\Zend_Cache_Core::class);
$lowLevelFrontend = new \Magento\Framework\Cache\Frontend\Adapter\Zend($adaptee);
$frontendFactory = function () use ($adaptee) {
return $adaptee;
};
$lowLevelFrontend = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendFactory);

return [
[
Expand Down

0 comments on commit 1d469f8

Please sign in to comment.