Skip to content

Commit c893715

Browse files
committed
ACP2E-1321: L2 cache save not working
1 parent 28c7f79 commit c893715

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

lib/internal/Magento/Framework/Cache/Backend/RemoteSynchronizedCache.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ public function load($id, $doNotTestCacheValidity = false)
195195
{
196196
$localData = $this->local->load($id);
197197

198-
if ($localData) {
198+
if ($localData !== false) {
199199
if ($this->getDataVersion($localData) === $this->loadRemoteDataVersion($id)) {
200200
return $localData;
201201
}
202202
}
203203

204204
$remoteData = $this->remote->load($id);
205-
if ($remoteData) {
205+
if ($remoteData !== false) {
206206
$this->local->save($remoteData, $id);
207207

208208
return $remoteData;
@@ -233,10 +233,15 @@ public function save($data, $id, $tags = [], $specificLifetime = false)
233233
{
234234
$dataToSave = $data;
235235
$remHash = $this->loadRemoteDataVersion($id);
236-
236+
$isRemoteUpToDate = false;
237237
if ($remHash !== false && $this->getDataVersion($data) === $remHash) {
238-
$dataToSave = $this->remote->load($id);
239-
} else {
238+
$remoteData = $this->remote->load($id);
239+
if ($remoteData !== false && $this->getDataVersion($data) === $this->getDataVersion($remoteData)) {
240+
$isRemoteUpToDate = true;
241+
$dataToSave = $remoteData;
242+
}
243+
}
244+
if (!$isRemoteUpToDate) {
240245
$this->remote->save($data, $id, $tags, $specificLifetime);
241246
$this->saveRemoteDataVersion($data, $id, $tags, $specificLifetime);
242247
}

lib/internal/Magento/Framework/Cache/Test/Unit/Backend/RemoteSynchronizedCacheTest.php

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,11 @@
1010
use Magento\Framework\Cache\Backend\Database;
1111
use Magento\Framework\Cache\Backend\RemoteSynchronizedCache;
1212
use Magento\Framework\DB\Adapter\Pdo\Mysql;
13-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1413
use PHPUnit\Framework\MockObject\MockObject;
1514
use PHPUnit\Framework\TestCase;
1615

1716
class RemoteSynchronizedCacheTest extends TestCase
1817
{
19-
/**
20-
* @var ObjectManager
21-
*/
22-
protected $objectManager;
23-
2418
/**
2519
* @var \Cm_Cache_Backend_File|MockObject
2620
*/
@@ -41,24 +35,12 @@ class RemoteSynchronizedCacheTest extends TestCase
4135
*/
4236
protected function setUp(): void
4337
{
44-
$this->objectManager = new ObjectManager($this);
45-
46-
$this->localCacheMockExample = $this->getMockBuilder(\Cm_Cache_Backend_File::class)
47-
->disableOriginalConstructor()
48-
->getMock();
49-
50-
$this->remoteCacheMockExample = $this->getMockBuilder(Database::class)
51-
->disableOriginalConstructor()
52-
->getMock();
53-
/** @var \Magento\Framework\Cache\Backend\Database $databaseCacheInstance */
54-
55-
$this->remoteSyncCacheInstance = $this->objectManager->getObject(
56-
RemoteSynchronizedCache::class,
38+
$this->localCacheMockExample = $this->createMock(\Cm_Cache_Backend_File::class);
39+
$this->remoteCacheMockExample = $this->createMock(Database::class);
40+
$this->remoteSyncCacheInstance = new RemoteSynchronizedCache(
5741
[
58-
'options' => [
59-
'remote_backend' => $this->remoteCacheMockExample,
60-
'local_backend' => $this->localCacheMockExample
61-
]
42+
'remote_backend' => $this->remoteCacheMockExample,
43+
'local_backend' => $this->localCacheMockExample
6244
]
6345
);
6446
}
@@ -67,19 +49,13 @@ protected function setUp(): void
6749
* Test that exception is thrown if cache is not configured.
6850
*
6951
* @param array $options
70-
*
7152
* @return void
7253
* @dataProvider initializeWithExceptionDataProvider
7354
*/
7455
public function testInitializeWithException($options): void
7556
{
7657
$this->expectException('Zend_Cache_Exception');
77-
$this->objectManager->getObject(
78-
RemoteSynchronizedCache::class,
79-
[
80-
'options' => $options
81-
]
82-
);
58+
new RemoteSynchronizedCache($options);
8359
}
8460

8561
/**
@@ -119,12 +95,7 @@ public function initializeWithExceptionDataProvider(): array
11995
*/
12096
public function testInitializeWithOutException($options): void
12197
{
122-
$result = $this->objectManager->getObject(
123-
RemoteSynchronizedCache::class,
124-
[
125-
'options' => $options
126-
]
127-
);
98+
$result = new RemoteSynchronizedCache($options);
12899
$this->assertInstanceOf(RemoteSynchronizedCache::class, $result);
129100
}
130101

@@ -377,6 +348,38 @@ public function testSaveWithEqualRemoteData(): void
377348
$this->remoteSyncCacheInstance->save($remoteData, 1, $tags);
378349
}
379350

351+
/**
352+
* Test data save when remote data are missed but hash exists.
353+
*
354+
* @return void
355+
*/
356+
public function testSaveWithEqualHashesAndMissedRemoteData(): void
357+
{
358+
$cacheKey = 'key';
359+
$dataToSave = '2';
360+
$remoteData = '1';
361+
$tags = ['MAGE'];
362+
363+
$this->remoteCacheMockExample
364+
->method('load')
365+
->willReturnOnConsecutiveCalls(\hash('sha256', $dataToSave), $remoteData);
366+
367+
$this->remoteCacheMockExample
368+
->expects($this->exactly(2))
369+
->method('save')
370+
->withConsecutive(
371+
[$dataToSave, $cacheKey, $tags],
372+
[\hash('sha256', $dataToSave), $cacheKey . ':hash', $tags]
373+
)->willReturn(true);
374+
$this->localCacheMockExample
375+
->expects($this->once())
376+
->method('save')
377+
->with($dataToSave, $cacheKey, [])
378+
->willReturn(true);
379+
380+
$this->remoteSyncCacheInstance->save($dataToSave, $cacheKey, $tags);
381+
}
382+
380383
/**
381384
* @return void
382385
*/

0 commit comments

Comments
 (0)