Skip to content

Commit 2d0aac3

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-4345' into PR_2025_12_03_chittima
2 parents cc95579 + adb19cf commit 2d0aac3

File tree

2 files changed

+179
-5
lines changed

2 files changed

+179
-5
lines changed

app/code/Magento/RemoteStorage/Driver/Adapter/Cache/Generic.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,32 @@ class Generic implements CacheInterface, ResetAfterRequestInterface
4949
*/
5050
private $pathUtil;
5151

52+
/**
53+
* @var int|null
54+
*/
55+
private ?int $cacheTTL = null;
56+
5257
/**
5358
* @param MagentoCacheInterface $cacheAdapter
5459
* @param SerializerInterface $serializer
5560
* @param PathUtil $pathUtil
5661
* @param string $prefix
62+
* @param int $cacheTTL
5763
*/
5864
public function __construct(
5965
MagentoCacheInterface $cacheAdapter,
6066
SerializerInterface $serializer,
6167
PathUtil $pathUtil,
62-
$prefix = 'flysystem:'
68+
$prefix = 'flysystem:',
69+
int $cacheTTL = 0
6370
) {
6471
$this->prefix = $prefix;
6572
$this->serializer = $serializer;
6673
$this->cacheAdapter = $cacheAdapter;
6774
$this->pathUtil = $pathUtil;
75+
if ($cacheTTL > 0) {
76+
$this->cacheTTL = $cacheTTL;
77+
}
6878
}
6979

7080
/**
@@ -97,7 +107,8 @@ public function updateMetadata(string $path, array $objectMetadata, bool $persis
97107
$this->cacheAdapter->save(
98108
$this->serializer->serialize([$path => $this->cacheData[$path]]),
99109
$this->prefix . $path,
100-
[self::CACHE_TAG]
110+
[self::CACHE_TAG],
111+
$this->cacheTTL
101112
);
102113
}
103114

@@ -113,7 +124,8 @@ public function storeFileNotExists(string $path): void
113124
$this->cacheAdapter->save(
114125
$this->serializer->serialize([$path => $this->cacheData[$path]]),
115126
$this->prefix . $path,
116-
[self::CACHE_TAG]
127+
[self::CACHE_TAG],
128+
$this->cacheTTL
117129
);
118130
}
119131

@@ -150,7 +162,8 @@ public function moveFile(string $path, string $newpath): void
150162
$this->cacheAdapter->save(
151163
$this->serializer->serialize([$newpath => $this->cacheData[$newpath]]),
152164
$this->prefix . $newpath,
153-
[self::CACHE_TAG]
165+
[self::CACHE_TAG],
166+
$this->cacheTTL
154167
);
155168
$this->purgeQueue();
156169
}

app/code/Magento/RemoteStorage/Test/Unit/Driver/Adpater/Cache/GenericTest.php

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\RemoteStorage\Test\Unit\Driver\Adpater\Cache;
88

9+
use Magento\RemoteStorage\Driver\Adapter\Cache\CacheInterface as AdapterCacheInterface;
910
use Magento\Framework\App\CacheInterface;
1011
use Magento\Framework\Serialize\SerializerInterface;
1112
use Magento\RemoteStorage\Driver\Adapter\Cache\Generic;
@@ -38,19 +39,176 @@ class GenericTest extends TestCase
3839
*/
3940
private PathUtil $pathUtilMock;
4041

42+
/**
43+
* @var int
44+
*/
45+
private int $ttl;
46+
4147
protected function setUp(): void
4248
{
4349
$this->cacheAdapterMock = $this->createMock(CacheInterface::class);
4450
$this->serializerMock = $this->createMock(SerializerInterface::class);
4551
$this->pathUtilMock = $this->createMock(PathUtil::class);
52+
$this->ttl = 300;
4653

4754
$this->generic = new Generic(
4855
$this->cacheAdapterMock,
4956
$this->serializerMock,
50-
$this->pathUtilMock
57+
$this->pathUtilMock,
58+
'flysystem:',
59+
$this->ttl
5160
);
5261
}
5362

63+
/**
64+
* @return void
65+
*/
66+
public function testUpdateMetadataPersists(): void
67+
{
68+
$path = 'dir/file.txt';
69+
$objectMetadata = ['type' => 'file', 'size' => 123];
70+
71+
$this->pathUtilMock
72+
->expects($this->exactly(2))
73+
->method('pathInfo')
74+
->willReturnOnConsecutiveCalls(
75+
['dirname' => 'dir', 'path' => $path],
76+
['dirname' => '', 'path' => 'dir']
77+
);
78+
79+
$this->serializerMock
80+
->expects($this->once())
81+
->method('serialize')
82+
->with($this->callback(function (array $data) use ($path) {
83+
return isset($data[$path])
84+
&& $data[$path]['type'] === 'file'
85+
&& $data[$path]['size'] === 123
86+
&& $data[$path]['dirname'] === 'dir'
87+
&& $data[$path]['path'] === $path;
88+
}))
89+
->willReturn('SERIALIZED_DATA');
90+
91+
$this->cacheAdapterMock
92+
->expects($this->once())
93+
->method('save')
94+
->with(
95+
'SERIALIZED_DATA',
96+
'flysystem:' . $path,
97+
[AdapterCacheInterface::CACHE_TAG],
98+
$this->ttl
99+
);
100+
101+
$this->cacheAdapterMock
102+
->expects($this->never())
103+
->method('load');
104+
105+
$this->generic->updateMetadata($path, $objectMetadata, true);
106+
}
107+
108+
/**
109+
* @return void
110+
*/
111+
public function testStoreFileNotExistsMarksPathAsNonExistingAndPersists(): void
112+
{
113+
$path = 'missing/file.txt';
114+
115+
$this->serializerMock
116+
->expects($this->once())
117+
->method('serialize')
118+
->with([$path => false])
119+
->willReturn('SERIALIZED_MISSING');
120+
121+
$this->cacheAdapterMock
122+
->expects($this->once())
123+
->method('save')
124+
->with(
125+
'SERIALIZED_MISSING',
126+
'flysystem:' . $path,
127+
[AdapterCacheInterface::CACHE_TAG],
128+
$this->ttl
129+
);
130+
131+
$this->generic->storeFileNotExists($path);
132+
133+
$this->cacheAdapterMock
134+
->expects($this->never())
135+
->method('load');
136+
137+
$this->assertFalse($this->generic->exists($path));
138+
}
139+
140+
/**
141+
* @return void
142+
*/
143+
public function testMoveFileMovesMetadataAndPurgesOldKey(): void
144+
{
145+
$oldPath = 'old/file.txt';
146+
$newPath = 'new/file.txt';
147+
148+
$this->cacheAdapterMock
149+
->expects($this->exactly(2))
150+
->method('load')
151+
->willReturnOnConsecutiveCalls(
152+
'SERIALIZED_OLD',
153+
false
154+
);
155+
156+
$oldMeta = [
157+
$oldPath => [
158+
'path' => $oldPath,
159+
'dirname' => 'old',
160+
'type' => 'file',
161+
],
162+
];
163+
164+
$this->serializerMock
165+
->expects($this->once())
166+
->method('unserialize')
167+
->with('SERIALIZED_OLD')
168+
->willReturn($oldMeta);
169+
170+
$this->pathUtilMock
171+
->expects($this->once())
172+
->method('pathInfo')
173+
->with($newPath)
174+
->willReturn(['dirname' => 'new', 'path' => $newPath]);
175+
176+
$this->serializerMock
177+
->expects($this->once())
178+
->method('serialize')
179+
->with($this->callback(function (array $data) use ($newPath) {
180+
if (!isset($data[$newPath])) {
181+
return false;
182+
}
183+
$meta = $data[$newPath];
184+
185+
return $meta['path'] === $newPath
186+
&& $meta['dirname'] === 'new'
187+
&& $meta['type'] === 'file';
188+
}))
189+
->willReturn('SERIALIZED_NEW');
190+
191+
$this->cacheAdapterMock
192+
->expects($this->once())
193+
->method('save')
194+
->with(
195+
'SERIALIZED_NEW',
196+
'flysystem:' . $newPath,
197+
[AdapterCacheInterface::CACHE_TAG],
198+
$this->ttl
199+
);
200+
201+
$this->cacheAdapterMock
202+
->expects($this->once())
203+
->method('remove')
204+
->with('flysystem:' . $oldPath);
205+
206+
$this->generic->moveFile($oldPath, $newPath);
207+
208+
$this->assertTrue($this->generic->exists($newPath));
209+
$this->assertNull($this->generic->exists($oldPath));
210+
}
211+
54212
/**
55213
* @param string $input
56214
* @param array|null $expectedOutput
@@ -117,6 +275,9 @@ public static function metaDataProvider(): array
117275
];
118276
}
119277

278+
/**
279+
* @return void
280+
*/
120281
protected function tearDown(): void
121282
{
122283
unset($this->generic);

0 commit comments

Comments
 (0)