Skip to content

Commit 2680e6a

Browse files
guyradfordNyholm
authored andcommitted
Remove file exists check as not required (#112)
* Updated way filename is generated to allow all cache keys to work. * fixed code style * Removed un-required file exists check. * fixed code for StyleCi * removed filename fix * removed tests relating to filename fix * styleCi... is there an auto fix script? * add extra check for failed unserialize. * StyleCI tidy up * StyleCI tidy up
1 parent 65a5f87 commit 2680e6a

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

FilesystemCachePool.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
1312
namespace Cache\Adapter\Filesystem;
1413

1514
use Cache\Adapter\Common\AbstractCachePool;
@@ -68,12 +67,12 @@ protected function fetchObjectFromCache($key)
6867
{
6968
$empty = [false, null, []];
7069
$file = $this->getFilePath($key);
71-
if (!$this->filesystem->has($file)) {
72-
return $empty;
73-
}
7470

7571
try {
76-
$data = unserialize($this->filesystem->read($file));
72+
$data = @unserialize($this->filesystem->read($file));
73+
if ($data === false) {
74+
return $empty;
75+
}
7776
} catch (FileNotFoundException $e) {
7877
return $empty;
7978
}
@@ -153,7 +152,7 @@ protected function storeItemInCache(CacheItemInterface $item, $ttl)
153152
private function getFilePath($key)
154153
{
155154
if (!preg_match('|^[a-zA-Z0-9_\.! ]+$|', $key)) {
156-
throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid keys must match [a-zA-Z0-9_\.! ].', $key));
155+
throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid filenames must match [a-zA-Z0-9_\.! ].', $key));
157156
}
158157

159158
return sprintf('%s/%s', $this->folder, $key);

Tests/FilesystemCachePoolTest.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
1312
namespace Cache\Adapter\Filesystem\Tests;
1413

1514
/**
@@ -54,4 +53,16 @@ public function testChangeFolder()
5453
$pool->save($pool->getItem('test_path'));
5554
$this->assertTrue($this->getFilesystem()->has('foobar/test_path'));
5655
}
56+
57+
public function testCorruptedCacheFileHandledNicely()
58+
{
59+
$pool = $this->createCachePool();
60+
61+
$this->getFilesystem()->write('cache/corrupt', 'corrupt data');
62+
63+
$item = $pool->getItem('corrupt');
64+
$this->assertFalse($item->isHit());
65+
66+
$this->getFilesystem()->delete('cache/corrupt');
67+
}
5768
}

0 commit comments

Comments
 (0)