Skip to content

Commit b835455

Browse files
authored
Merge pull request #8255 from il-coder/fix-duplicate-prefix-cache-filehandler
fix: [Cache] Double prefix for increment in FileHandler
2 parents f779052 + 81352d5 commit b835455

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

system/Cache/Handlers/FileHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public function deleteMatching(string $pattern)
150150
*/
151151
public function increment(string $key, int $offset = 1)
152152
{
153-
$key = static::validateKey($key, $this->prefix);
154-
$tmp = $this->getItem($key);
153+
$prefixedKey = static::validateKey($key, $this->prefix);
154+
$tmp = $this->getItem($prefixedKey);
155155

156156
if ($tmp === false) {
157157
$tmp = ['data' => 0, 'ttl' => 60];

tests/system/Cache/Handlers/FileHandlerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ protected function tearDown(): void
6767
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key, 0777);
6868
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key);
6969
}
70+
if (is_file($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key)) {
71+
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key, 0777);
72+
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key);
73+
}
7074
}
7175

7276
rmdir($this->config->file['storePath']);
@@ -233,6 +237,22 @@ public function testIncrement(): void
233237
$this->assertSame(10, $this->handler->increment(self::$key3, 10));
234238
}
235239

240+
public function testIncrementWithDefaultPrefix(): void
241+
{
242+
$this->config->prefix = 'test_';
243+
$this->handler = new FileHandler($this->config);
244+
$this->handler->initialize();
245+
246+
$this->handler->save(self::$key1, 1);
247+
$this->handler->save(self::$key2, 'value');
248+
249+
$this->assertSame(11, $this->handler->increment(self::$key1, 10));
250+
$this->assertSame($this->handler->increment(self::$key1, 10), $this->handler->get(self::$key1));
251+
$this->assertFalse($this->handler->increment(self::$key2, 10));
252+
$this->assertSame(10, $this->handler->increment(self::$key3, 10));
253+
$this->assertSame($this->handler->increment(self::$key3, 10), $this->handler->get(self::$key3));
254+
}
255+
236256
public function testDecrement(): void
237257
{
238258
$this->handler->save(self::$key1, 10);
@@ -246,6 +266,21 @@ public function testDecrement(): void
246266
$this->assertSame(-1, $this->handler->decrement(self::$key3, 1));
247267
}
248268

269+
public function testDecrementWithDefaultPrefix(): void
270+
{
271+
$this->handler->save(self::$key1, 10);
272+
$this->handler->save(self::$key2, 'value');
273+
274+
// Line following commented out to force the cache to add a zero entry for key3
275+
// $this->fileHandler->save(self::$key3, 0);
276+
277+
$this->assertSame(9, $this->handler->decrement(self::$key1, 1));
278+
$this->assertSame($this->handler->decrement(self::$key1, 1), $this->handler->get(self::$key1));
279+
$this->assertFalse($this->handler->decrement(self::$key2, 1));
280+
$this->assertSame(-1, $this->handler->decrement(self::$key3, 1));
281+
$this->assertSame($this->handler->decrement(self::$key3, 1), $this->handler->get(self::$key3));
282+
}
283+
249284
public function testClean(): void
250285
{
251286
$this->handler->save(self::$key1, 1);

0 commit comments

Comments
 (0)