Skip to content

Commit 1d469f8

Browse files
authored
Merge pull request #2848 from magento-performance/MAGETWO-93184
[Performance] MAGETWO-93184: [Forwardport] Static Assets deployment throws errors when redis is used for cache
2 parents 34da3bd + bd21cc0 commit 1d469f8

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

lib/internal/Magento/Framework/App/Cache/Frontend/Factory.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,17 @@ public function create(array $options)
147147
$result = $this->_objectManager->create(
148148
\Magento\Framework\Cache\Frontend\Adapter\Zend::class,
149149
[
150-
'frontend' => \Zend_Cache::factory(
151-
$frontend['type'],
152-
$backend['type'],
153-
$frontend,
154-
$backend['options'],
155-
true,
156-
true,
157-
true
158-
)
150+
'frontendFactory' => function () use ($frontend, $backend) {
151+
return \Zend_Cache::factory(
152+
$frontend['type'],
153+
$backend['type'],
154+
$frontend,
155+
$backend['options'],
156+
true,
157+
true,
158+
true
159+
);
160+
}
159161
]
160162
);
161163
$result = $this->_applyDecorators($result);

lib/internal/Magento/Framework/App/Test/Unit/Cache/Frontend/FactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected function _buildModelForCreate($enforcedOptions = [], $decorators = [])
128128
$processFrontendFunc = function ($class, $params) {
129129
switch ($class) {
130130
case \Magento\Framework\Cache\Frontend\Adapter\Zend::class:
131-
return new $class($params['frontend']);
131+
return new $class($params['frontendFactory']);
132132
case \Magento\Framework\App\Test\Unit\Cache\Frontend\FactoryTest\CacheDecoratorDummy::class:
133133
$frontend = $params['frontend'];
134134
unset($params['frontend']);

lib/internal/Magento/Framework/Cache/Frontend/Adapter/Zend.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,66 @@ class Zend implements \Magento\Framework\Cache\FrontendInterface
1616
protected $_frontend;
1717

1818
/**
19-
* @param \Zend_Cache_Core $frontend
19+
* Factory that creates the \Zend_Cache_Cores
20+
*
21+
* @var \Closure
22+
*/
23+
private $frontendFactory;
24+
25+
/**
26+
* The pid that owns the $_frontend object
27+
*
28+
* @var int
2029
*/
21-
public function __construct(\Zend_Cache_Core $frontend)
30+
private $pid;
31+
32+
/**
33+
* @param \Closure $frontendFactory
34+
*/
35+
public function __construct(\Closure $frontendFactory)
2236
{
23-
$this->_frontend = $frontend;
37+
$this->frontendFactory = $frontendFactory;
38+
$this->_frontend = $frontendFactory();
39+
$this->pid = getmypid();
2440
}
2541

2642
/**
2743
* {@inheritdoc}
2844
*/
2945
public function test($identifier)
3046
{
31-
return $this->_frontend->test($this->_unifyId($identifier));
47+
return $this->getFrontEnd()->test($this->_unifyId($identifier));
3248
}
3349

3450
/**
3551
* {@inheritdoc}
3652
*/
3753
public function load($identifier)
3854
{
39-
return $this->_frontend->load($this->_unifyId($identifier));
55+
return $this->getFrontEnd()->load($this->_unifyId($identifier));
4056
}
4157

4258
/**
4359
* {@inheritdoc}
4460
*/
4561
public function save($data, $identifier, array $tags = [], $lifeTime = null)
4662
{
47-
return $this->_frontend->save($data, $this->_unifyId($identifier), $this->_unifyIds($tags), $lifeTime);
63+
return $this->getFrontEnd()->save($data, $this->_unifyId($identifier), $this->_unifyIds($tags), $lifeTime);
4864
}
4965

5066
/**
5167
* {@inheritdoc}
5268
*/
5369
public function remove($identifier)
5470
{
55-
return $this->_frontend->remove($this->_unifyId($identifier));
71+
return $this->getFrontEnd()->remove($this->_unifyId($identifier));
5672
}
5773

5874
/**
5975
* {@inheritdoc}
6076
*
6177
* @throws \InvalidArgumentException Exception is thrown when non-supported cleaning mode is specified
78+
* @throws \Zend_Cache_Exception
6279
*/
6380
public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = [])
6481
{
@@ -76,23 +93,23 @@ public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = [])
7693
"Magento cache frontend does not support the cleaning mode '{$mode}'."
7794
);
7895
}
79-
return $this->_frontend->clean($mode, $this->_unifyIds($tags));
96+
return $this->getFrontEnd()->clean($mode, $this->_unifyIds($tags));
8097
}
8198

8299
/**
83100
* {@inheritdoc}
84101
*/
85102
public function getBackend()
86103
{
87-
return $this->_frontend->getBackend();
104+
return $this->getFrontEnd()->getBackend();
88105
}
89106

90107
/**
91108
* {@inheritdoc}
92109
*/
93110
public function getLowLevelFrontend()
94111
{
95-
return $this->_frontend;
112+
return $this->getFrontEnd();
96113
}
97114

98115
/**
@@ -119,4 +136,20 @@ protected function _unifyIds(array $ids)
119136
}
120137
return $ids;
121138
}
139+
140+
/**
141+
* Get frontEnd cache adapter for current pid
142+
*
143+
* @return \Zend_Cache_Core
144+
*/
145+
private function getFrontEnd()
146+
{
147+
if (getmypid() === $this->pid) {
148+
return $this->_frontend;
149+
}
150+
$frontendFactory = $this->frontendFactory;
151+
$this->_frontend = $frontendFactory();
152+
$this->pid = getmypid();
153+
return $this->_frontend;
154+
}
122155
}

lib/internal/Magento/Framework/Cache/Test/Unit/Frontend/Adapter/ZendTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ class ZendTest extends \PHPUnit\Framework\TestCase
1717
public function testProxyMethod($method, $params, $expectedParams, $expectedResult)
1818
{
1919
$frontendMock = $this->createMock(\Zend_Cache_Core::class);
20-
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendMock);
20+
$frontendFactory = function () use ($frontendMock) {
21+
return $frontendMock;
22+
};
23+
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendFactory);
2124
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ProxyTesting();
2225
$result = $helper->invokeWithExpectations(
2326
$object,
@@ -82,7 +85,11 @@ public function testCleanException($cleaningMode, $expectedErrorMessage)
8285
{
8386
$this->expectException('InvalidArgumentException');
8487
$this->expectExceptionMessage($expectedErrorMessage);
85-
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($this->createMock(\Zend_Cache_Core::class));
88+
$frontendMock = $this->createMock(\Zend_Cache_Core::class);
89+
$frontendFactory = function () use ($frontendMock) {
90+
return $frontendMock;
91+
};
92+
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendFactory);
8693
$object->clean($cleaningMode);
8794
}
8895

@@ -110,7 +117,10 @@ public function cleanExceptionDataProvider()
110117
public function testGetLowLevelFrontend()
111118
{
112119
$frontendMock = $this->createMock(\Zend_Cache_Core::class);
113-
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendMock);
120+
$frontendFactory = function () use ($frontendMock) {
121+
return $frontendMock;
122+
};
123+
$object = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendFactory);
114124
$this->assertSame($frontendMock, $object->getLowLevelFrontend());
115125
}
116126
}

lib/internal/Magento/Framework/Cache/Test/Unit/Frontend/Decorator/ProfilerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public function proxyMethodDataProvider()
6363
{
6464
$backend = new \Zend_Cache_Backend_BlackHole();
6565
$adaptee = $this->createMock(\Zend_Cache_Core::class);
66-
$lowLevelFrontend = new \Magento\Framework\Cache\Frontend\Adapter\Zend($adaptee);
66+
$frontendFactory = function () use ($adaptee) {
67+
return $adaptee;
68+
};
69+
$lowLevelFrontend = new \Magento\Framework\Cache\Frontend\Adapter\Zend($frontendFactory);
6770

6871
return [
6972
[

0 commit comments

Comments
 (0)