Skip to content

Commit 5c39911

Browse files
authored
ENGCOM-6834: #25669: fixed issue "health_check.php fails if any database cache engine configured" #25722
2 parents 16247f6 + 2d3ad56 commit 5c39911

File tree

4 files changed

+101
-13
lines changed

4 files changed

+101
-13
lines changed

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Database extends \Zend_Cache_Backend implements \Zend_Cache_Backend_Extend
5959
* Constructor
6060
*
6161
* @param array $options associative array of options
62+
* @throws \Zend_Cache_Exception
6263
*/
6364
public function __construct($options = [])
6465
{
@@ -82,6 +83,7 @@ public function __construct($options = [])
8283
* Get DB adapter
8384
*
8485
* @return \Magento\Framework\DB\Adapter\AdapterInterface
86+
* @throws \Zend_Cache_Exception
8587
*/
8688
protected function _getConnection()
8789
{
@@ -106,6 +108,7 @@ protected function _getConnection()
106108
* Get table name where data is stored
107109
*
108110
* @return string
111+
* @throws \Zend_Cache_Exception
109112
*/
110113
protected function _getDataTable()
111114
{
@@ -122,6 +125,7 @@ protected function _getDataTable()
122125
* Get table name where tags are stored
123126
*
124127
* @return string
128+
* @throws \Zend_Cache_Exception
125129
*/
126130
protected function _getTagsTable()
127131
{
@@ -139,9 +143,10 @@ protected function _getTagsTable()
139143
*
140144
* Note : return value is always "string" (unserialization is done by the core not by the backend)
141145
*
142-
* @param string $id Cache id
143-
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
146+
* @param string $id Cache id
147+
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
144148
* @return string|false cached datas
149+
* @throws \Zend_Cache_Exception
145150
*/
146151
public function load($id, $doNotTestCacheValidity = false)
147152
{
@@ -166,8 +171,9 @@ public function load($id, $doNotTestCacheValidity = false)
166171
/**
167172
* Test if a cache is available or not (for the given id)
168173
*
169-
* @param string $id cache id
174+
* @param string $id cache id
170175
* @return mixed|false (a cache is not available) or "last modified" timestamp (int) of the available cache record
176+
* @throws \Zend_Cache_Exception
171177
*/
172178
public function test($id)
173179
{
@@ -196,11 +202,13 @@ public function test($id)
196202
* Note : $data is always "string" (serialization is done by the
197203
* core not by the backend)
198204
*
199-
* @param string $data Datas to cache
200-
* @param string $id Cache id
201-
* @param string[] $tags Array of strings, the cache record will be tagged by each string entry
202-
* @param int|bool $specificLifetime Integer to set a specific lifetime or null for infinite lifetime
205+
* @param string $data Datas to cache
206+
* @param string $id Cache id
207+
* @param string[] $tags Array of strings, the cache record will be tagged by each string entry
208+
* @param int|bool $specificLifetime Integer to set a specific lifetime or null for infinite lifetime
203209
* @return bool true if no problem
210+
* @throws \Zend_Db_Statement_Exception
211+
* @throws \Zend_Cache_Exception
204212
*/
205213
public function save($data, $id, $tags = [], $specificLifetime = false)
206214
{
@@ -239,8 +247,9 @@ public function save($data, $id, $tags = [], $specificLifetime = false)
239247
/**
240248
* Remove a cache record
241249
*
242-
* @param string $id Cache id
243-
* @return boolean True if no problem
250+
* @param string $id Cache id
251+
* @return int|boolean Number of affected rows or false on failure
252+
* @throws \Zend_Cache_Exception
244253
*/
245254
public function remove($id)
246255
{
@@ -266,9 +275,10 @@ public function remove($id)
266275
* \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
267276
* ($tags can be an array of strings or a single string)
268277
*
269-
* @param string $mode Clean mode
270-
* @param string[] $tags Array of tags
278+
* @param string $mode Clean mode
279+
* @param string[] $tags Array of tags
271280
* @return boolean true if no problem
281+
* @throws \Zend_Cache_Exception
272282
*/
273283
public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = [])
274284
{
@@ -301,6 +311,7 @@ public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = [])
301311
* Return an array of stored cache ids
302312
*
303313
* @return string[] array of stored cache ids (string)
314+
* @throws \Zend_Cache_Exception
304315
*/
305316
public function getIds()
306317
{
@@ -316,6 +327,7 @@ public function getIds()
316327
* Return an array of stored tags
317328
*
318329
* @return string[] array of stored tags (string)
330+
* @throws \Zend_Cache_Exception
319331
*/
320332
public function getTags()
321333
{
@@ -330,6 +342,7 @@ public function getTags()
330342
*
331343
* @param string[] $tags array of tags
332344
* @return string[] array of matching cache ids (string)
345+
* @throws \Zend_Cache_Exception
333346
*/
334347
public function getIdsMatchingTags($tags = [])
335348
{
@@ -356,6 +369,7 @@ public function getIdsMatchingTags($tags = [])
356369
*
357370
* @param string[] $tags array of tags
358371
* @return string[] array of not matching cache ids (string)
372+
* @throws \Zend_Cache_Exception
359373
*/
360374
public function getIdsNotMatchingTags($tags = [])
361375
{
@@ -369,6 +383,7 @@ public function getIdsNotMatchingTags($tags = [])
369383
*
370384
* @param string[] $tags array of tags
371385
* @return string[] array of any matching cache ids (string)
386+
* @throws \Zend_Cache_Exception
372387
*/
373388
public function getIdsMatchingAnyTags($tags = [])
374389
{
@@ -404,6 +419,7 @@ public function getFillingPercentage()
404419
*
405420
* @param string $id cache id
406421
* @return array|false array of metadatas (false if the cache id is not found)
422+
* @throws \Zend_Cache_Exception
407423
*/
408424
public function getMetadatas($id)
409425
{
@@ -425,6 +441,7 @@ public function getMetadatas($id)
425441
* @param string $id cache id
426442
* @param int $extraLifetime
427443
* @return boolean true if ok
444+
* @throws \Zend_Cache_Exception
428445
*/
429446
public function touch($id, $extraLifetime)
430447
{
@@ -471,6 +488,7 @@ public function getCapabilities()
471488
* @param string $id
472489
* @param string[] $tags
473490
* @return bool
491+
* @throws \Zend_Cache_Exception
474492
*/
475493
protected function _saveTags($id, $tags)
476494
{
@@ -509,6 +527,8 @@ protected function _saveTags($id, $tags)
509527
* @param string $mode
510528
* @param string[] $tags
511529
* @return bool
530+
* @throws \Zend_Cache_Exception
531+
* @throws \Zend_Db_Statement_Exception
512532
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
513533
*/
514534
protected function _cleanByTags($mode, $tags)
@@ -558,6 +578,7 @@ protected function _cleanByTags($mode, $tags)
558578
*
559579
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
560580
* @return bool
581+
* @throws \Zend_Cache_Exception
561582
*/
562583
private function cleanAll(\Magento\Framework\DB\Adapter\AdapterInterface $connection)
563584
{
@@ -575,6 +596,7 @@ private function cleanAll(\Magento\Framework\DB\Adapter\AdapterInterface $connec
575596
*
576597
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
577598
* @return bool
599+
* @throws \Zend_Cache_Exception
578600
*/
579601
private function cleanOld(\Magento\Framework\DB\Adapter\AdapterInterface $connection)
580602
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function lock(string $name, int $timeout = -1): bool
4949
*/
5050
public function unlock(string $name): bool
5151
{
52-
return $this->cache->remove($this->getIdentifier($name));
52+
return (bool)$this->cache->remove($this->getIdentifier($name));
5353
}
5454

5555
/**
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Lock\Test\Unit\Backend;
9+
10+
use Magento\Framework\Cache\FrontendInterface;
11+
use Magento\Framework\Lock\Backend\Cache;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class CacheTest extends TestCase
17+
{
18+
const LOCK_PREFIX = 'LOCKED_RECORD_INFO_';
19+
20+
/**
21+
* @var FrontendInterface|MockObject
22+
*/
23+
private $frontendCacheMock;
24+
25+
/**
26+
* @var Cache
27+
*/
28+
private $cache;
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function setUp()
34+
{
35+
$this->frontendCacheMock = $this->createMock(FrontendInterface::class);
36+
37+
$objectManager = new ObjectManagerHelper($this);
38+
39+
$this->cache = $objectManager->getObject(
40+
Cache::class,
41+
[
42+
'cache' => $this->frontendCacheMock
43+
]
44+
);
45+
}
46+
47+
/**
48+
* Verify released a lock.
49+
*
50+
* @return void
51+
*/
52+
public function testUnlock(): void
53+
{
54+
$identifier = 'lock_name';
55+
56+
$this->frontendCacheMock
57+
->expects($this->once())
58+
->method('remove')
59+
->with(self::LOCK_PREFIX . $identifier)
60+
->willReturn(true);
61+
62+
$this->assertEquals(true, $this->cache->unlock($identifier));
63+
}
64+
}

pub/health_check.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@
6363
}
6464
$cacheBackendClass = $cacheConfig[ConfigOptionsListConstants::CONFIG_PATH_BACKEND];
6565
try {
66+
/** @var \Magento\Framework\App\Cache\Frontend\Factory $cacheFrontendFactory */
67+
$cacheFrontendFactory = $objectManager->get(Magento\Framework\App\Cache\Frontend\Factory::class);
6668
/** @var \Zend_Cache_Backend_Interface $backend */
67-
$backend = new $cacheBackendClass($cacheConfig[ConfigOptionsListConstants::CONFIG_PATH_BACKEND_OPTIONS]);
69+
$backend = $cacheFrontendFactory->create($cacheConfig);
6870
$backend->test('test_cache_id');
6971
} catch (\Exception $e) {
7072
http_response_code(500);

0 commit comments

Comments
 (0)