Skip to content

Commit 0b69ee8

Browse files
arhiopterecsshiftedreality
authored andcommitted
MAGECLOUD-3172: Update default Redis DB IDs (#440)
1 parent 1674453 commit 0b69ee8

File tree

6 files changed

+127
-40
lines changed

6 files changed

+127
-40
lines changed

src/Config/Factory/Cache.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
*/
1616
class Cache
1717
{
18+
/**
19+
* Redis database to store default cache data
20+
*/
21+
const REDIS_DATABASE_DEFAULT = 1;
22+
23+
/**
24+
* Redis database to store page cache data
25+
*/
26+
const REDIS_DATABASE_PAGE_CACHE = 2;
27+
1828
/**
1929
* @var Environment
2030
*/
@@ -93,7 +103,6 @@ public function get(): array
93103
'backend_options' => [
94104
'server' => $redisConfig[0]['host'],
95105
'port' => $redisConfig[0]['port'],
96-
'database' => 1,
97106
],
98107
];
99108

@@ -115,8 +124,14 @@ public function get(): array
115124

116125
return $this->configMerger->mergeConfigs([
117126
'frontend' => [
118-
'default' => $redisCache,
119-
'page_cache' => $redisCache,
127+
'default' => array_replace_recursive(
128+
$redisCache,
129+
['backend_options' => ['database' => self::REDIS_DATABASE_DEFAULT]]
130+
),
131+
'page_cache' => array_replace_recursive(
132+
$redisCache,
133+
['backend_options' => ['database' => self::REDIS_DATABASE_PAGE_CACHE]]
134+
),
120135
],
121136
], $envCacheConfiguration);
122137
}

src/Process/Deploy/InstallUpdate/ConfigUpdate/Session/Config.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
*/
1717
class Config
1818
{
19+
/**
20+
* Redis database to store session data
21+
*/
22+
const REDIS_DATABASE_SESSION = 0;
1923
/**
2024
* @var Environment
2125
*/
@@ -93,7 +97,7 @@ public function get(): array
9397
'redis' => [
9498
'host' => $redisConfig[0]['host'],
9599
'port' => $redisConfig[0]['port'],
96-
'database' => 0,
100+
'database' => self::REDIS_DATABASE_SESSION,
97101
],
98102
];
99103

src/Process/Deploy/PreDeploy/CleanRedisCache.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\MagentoCloud\Process\Deploy\PreDeploy;
77

88
use Magento\MagentoCloud\Config\Environment;
9+
use Magento\MagentoCloud\Config\Factory\Cache as CacheConfig;
910
use Magento\MagentoCloud\Process\ProcessInterface;
1011
use Magento\MagentoCloud\Shell\ShellInterface;
1112
use Psr\Log\LoggerInterface;
@@ -30,36 +31,53 @@ class CleanRedisCache implements ProcessInterface
3031
*/
3132
private $shell;
3233

34+
/**
35+
* @var CacheConfig
36+
*/
37+
private $cacheConfig;
38+
3339
/**
3440
* @param LoggerInterface $logger
3541
* @param ShellInterface $shell
3642
* @param Environment $env
43+
* @param CacheConfig $cacheConfig
3744
*/
3845
public function __construct(
3946
LoggerInterface $logger,
4047
ShellInterface $shell,
41-
Environment $env
48+
Environment $env,
49+
CacheConfig $cacheConfig
4250
) {
4351
$this->logger = $logger;
4452
$this->shell = $shell;
4553
$this->env = $env;
54+
$this->cacheConfig = $cacheConfig;
4655
}
4756

4857
/**
49-
* Clears redis cache if redis enabled and configuration exists in MAGENTO_CLOUD_RELATIONSHIPS env variable.
58+
* Clears redis cache
5059
*
5160
* @return void
5261
*/
5362
public function execute()
5463
{
55-
$redis = $this->env->getRelationship('redis');
64+
$cacheConfigs = $this->cacheConfig->get();
5665

57-
if (count($redis) > 0) {
58-
$redisHost = $redis[0]['host'];
59-
$redisPort = $redis[0]['port'];
60-
$redisCacheDb = '1';
61-
$this->logger->info('Clearing redis cache');
62-
$this->shell->execute("redis-cli -h $redisHost -p $redisPort -n $redisCacheDb flushdb");
66+
if (!isset($cacheConfigs['frontend'])) {
67+
return;
68+
}
69+
foreach ($cacheConfigs['frontend'] as $cacheType => $cacheConfig) {
70+
if ($cacheConfig['backend'] != 'Cm_Cache_Backend_Redis') {
71+
continue;
72+
}
73+
$redisConfig = $cacheConfig['backend_options'];
74+
$this->logger->info("Clearing redis cache: $cacheType");
75+
$cmd = 'redis-cli';
76+
$cmd .= isset($redisConfig['server']) ? ' -h ' . $redisConfig['server'] : '';
77+
$cmd .= isset($redisConfig['port']) ? ' -p ' . $redisConfig['port'] : '';
78+
$cmd .= isset($redisConfig['database']) ? ' -n ' . $redisConfig['database'] : '';
79+
$cmd .= ' flushdb';
80+
$this->shell->execute($cmd);
6381
}
6482
}
6583
}

src/Test/Unit/Config/Factory/CacheTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,15 @@ public function getFromRelationshipsDataProvider()
202202
'backend_options' => [
203203
'server' => 'master.host',
204204
'port' => 'master.port',
205-
'database' => 1
205+
'database' => Cache::REDIS_DATABASE_DEFAULT
206206
],
207207
],
208208
'page_cache' => [
209209
'backend' => 'Cm_Cache_Backend_Redis',
210210
'backend_options' => [
211211
'server' => 'master.host',
212212
'port' => 'master.port',
213-
'database' => 1
213+
'database' => Cache::REDIS_DATABASE_PAGE_CACHE
214214
],
215215
],
216216
]
@@ -354,15 +354,15 @@ public function envConfigurationMergingDataProvider(): array
354354
'backend_options' => [
355355
'server' => 'master.host',
356356
'port' => 'master.port',
357-
'database' => 1
357+
'database' => Cache::REDIS_DATABASE_DEFAULT
358358
],
359359
],
360360
'page_cache' => [
361361
'backend' => 'Cm_Cache_Backend_Redis',
362362
'backend_options' => [
363363
'server' => 'master.host',
364364
'port' => 'master.port',
365-
'database' => 1
365+
'database' => Cache::REDIS_DATABASE_PAGE_CACHE
366366
],
367367
],
368368
]

src/Test/Unit/Process/Deploy/InstallUpdate/ConfigUpdate/Session/ConfigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function envConfigurationMergingDataProvider(): array
136136
'redis' => [
137137
'host' => 'host',
138138
'port' => 'port',
139-
'database' => 0,
139+
'database' => Config::REDIS_DATABASE_SESSION,
140140
'disable_locking' => 1
141141
],
142142
];
@@ -233,7 +233,7 @@ public function envConfigurationMergingWithPrevVersionDataProvider(): array
233233
'redis' => [
234234
'host' => 'host',
235235
'port' => 'port',
236-
'database' => 0,
236+
'database' => Config::REDIS_DATABASE_SESSION,
237237
'disable_locking' => 0
238238
],
239239
];

src/Test/Unit/Process/Deploy/PreDeploy/CleanRedisCacheTest.php

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\MagentoCloud\Test\Unit\Process\Deploy\PreDeploy;
78

89
use Magento\MagentoCloud\Config\Environment;
910
use Magento\MagentoCloud\Process\Deploy\PreDeploy\CleanRedisCache;
1011
use Magento\MagentoCloud\Shell\ShellInterface;
12+
use Magento\MagentoCloud\Config\Factory\Cache as СacheConfig;
1113
use PHPUnit\Framework\TestCase;
1214
use PHPUnit_Framework_MockObject_MockObject as Mock;
1315
use Psr\Log\LoggerInterface;
@@ -32,6 +34,11 @@ class CleanRedisCacheTest extends TestCase
3234
*/
3335
private $loggerMock;
3436

37+
/**
38+
* @var СacheConfig|Mock
39+
*/
40+
private $cacheConfigMock;
41+
3542
/**
3643
* @var CleanRedisCache
3744
*/
@@ -44,48 +51,91 @@ protected function setUp()
4451
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
4552
->getMockForAbstractClass();
4653
$this->environmentMock = $this->createMock(Environment::class);
54+
$this->cacheConfigMock = $this->createMock(СacheConfig::class);
4755

4856
$this->process = new CleanRedisCache(
4957
$this->loggerMock,
5058
$this->shellMock,
51-
$this->environmentMock
59+
$this->environmentMock,
60+
$this->cacheConfigMock
5261
);
5362
}
5463

5564
public function testExecute()
5665
{
57-
$redisConfig = [
58-
[
59-
'host' => 'localhost',
60-
'port' => 1234
61-
]
62-
];
63-
64-
$this->environmentMock->expects($this->once())
65-
->method('getRelationship')
66-
->with('redis')
67-
->willReturn($redisConfig);
68-
$this->loggerMock->expects($this->once())
66+
$this->cacheConfigMock->expects($this->once())
67+
->method('get')
68+
->willReturn([
69+
'frontend' => [
70+
'default' => [
71+
'backend' => 'Cm_Cache_Backend_Redis',
72+
'backend_options' => [
73+
'server' => 'localhost',
74+
'port' => 1234,
75+
'database' => 0
76+
]
77+
],
78+
'page_cache' => [
79+
'backend' => 'Cm_Cache_Backend_Redis',
80+
'backend_options' => [
81+
'port' => 1234,
82+
'database' => 1
83+
]
84+
],
85+
'some_type0' => [
86+
'backend' => 'Cm_Cache_Backend_Redis',
87+
'backend_options' => [
88+
'server' => 'localhost',
89+
'database' => 2
90+
]
91+
],
92+
'some_type1' => [
93+
'backend' => 'Cm_Cache_Backend_Redis',
94+
'backend_options' => [
95+
'server' => 'localhost',
96+
'port' => 1234,
97+
]
98+
],
99+
'some_type2' => [
100+
'backend' => 'Cm_Cache_Backend_Redis',
101+
'backend_options' => []
102+
],
103+
'some_type3' => [
104+
'backend' => 'SomeClase',
105+
]
106+
]
107+
]);
108+
$this->loggerMock->expects($this->exactly(5))
69109
->method('info')
70-
->with('Clearing redis cache');
71-
$this->shellMock->expects($this->once())
110+
->withConsecutive(
111+
['Clearing redis cache: default'],
112+
['Clearing redis cache: page_cache'],
113+
['Clearing redis cache: some_type0'],
114+
['Clearing redis cache: some_type1'],
115+
['Clearing redis cache: some_type2']
116+
);
117+
$this->shellMock->expects($this->exactly(5))
72118
->method('execute')
73-
->with('redis-cli -h localhost -p 1234 -n 1 flushdb');
119+
->withConsecutive(
120+
['redis-cli -h localhost -p 1234 -n 0 flushdb'],
121+
['redis-cli -p 1234 -n 1 flushdb'],
122+
['redis-cli -h localhost -n 2 flushdb'],
123+
['redis-cli -h localhost -p 1234 flushdb'],
124+
['redis-cli flushdb']
125+
);
74126

75127
$this->process->execute();
76128
}
77129

78-
public function testExecuteWithoutRedisRelationship()
130+
public function testExecuteWithoutRedis()
79131
{
80-
$this->environmentMock->expects($this->once())
81-
->method('getRelationship')
82-
->with('redis')
132+
$this->cacheConfigMock->expects($this->once())
133+
->method('get')
83134
->willReturn([]);
84135
$this->loggerMock->expects($this->never())
85136
->method('info');
86137
$this->shellMock->expects($this->never())
87138
->method('execute');
88-
89139
$this->process->execute();
90140
}
91141
}

0 commit comments

Comments
 (0)