Skip to content

Commit

Permalink
Merge pull request #8255 from il-coder/fix-duplicate-prefix-cache-fil…
Browse files Browse the repository at this point in the history
…ehandler

fix: [Cache] Double prefix for increment in FileHandler
  • Loading branch information
kenjis authored Nov 27, 2023
2 parents f779052 + 81352d5 commit b835455
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public function deleteMatching(string $pattern)
*/
public function increment(string $key, int $offset = 1)
{
$key = static::validateKey($key, $this->prefix);
$tmp = $this->getItem($key);
$prefixedKey = static::validateKey($key, $this->prefix);
$tmp = $this->getItem($prefixedKey);

if ($tmp === false) {
$tmp = ['data' => 0, 'ttl' => 60];
Expand Down
35 changes: 35 additions & 0 deletions tests/system/Cache/Handlers/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ protected function tearDown(): void
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key, 0777);
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key);
}
if (is_file($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key)) {
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key, 0777);
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key);
}
}

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

public function testIncrementWithDefaultPrefix(): void
{
$this->config->prefix = 'test_';
$this->handler = new FileHandler($this->config);
$this->handler->initialize();

$this->handler->save(self::$key1, 1);
$this->handler->save(self::$key2, 'value');

$this->assertSame(11, $this->handler->increment(self::$key1, 10));
$this->assertSame($this->handler->increment(self::$key1, 10), $this->handler->get(self::$key1));
$this->assertFalse($this->handler->increment(self::$key2, 10));
$this->assertSame(10, $this->handler->increment(self::$key3, 10));
$this->assertSame($this->handler->increment(self::$key3, 10), $this->handler->get(self::$key3));
}

public function testDecrement(): void
{
$this->handler->save(self::$key1, 10);
Expand All @@ -246,6 +266,21 @@ public function testDecrement(): void
$this->assertSame(-1, $this->handler->decrement(self::$key3, 1));
}

public function testDecrementWithDefaultPrefix(): void
{
$this->handler->save(self::$key1, 10);
$this->handler->save(self::$key2, 'value');

// Line following commented out to force the cache to add a zero entry for key3
// $this->fileHandler->save(self::$key3, 0);

$this->assertSame(9, $this->handler->decrement(self::$key1, 1));
$this->assertSame($this->handler->decrement(self::$key1, 1), $this->handler->get(self::$key1));
$this->assertFalse($this->handler->decrement(self::$key2, 1));
$this->assertSame(-1, $this->handler->decrement(self::$key3, 1));
$this->assertSame($this->handler->decrement(self::$key3, 1), $this->handler->get(self::$key3));
}

public function testClean(): void
{
$this->handler->save(self::$key1, 1);
Expand Down

0 comments on commit b835455

Please sign in to comment.