Skip to content

Commit d21524a

Browse files
committed
Bugfixes
1 parent 41d00b6 commit d21524a

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

src/FilesystemCachePool.php

+27-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
class FilesystemCachePool extends AbstractCachePool
2323
{
24+
const CACHE_PATH = 'cache';
2425
/**
2526
* @var Filesystem
2627
*/
@@ -32,33 +33,53 @@ class FilesystemCachePool extends AbstractCachePool
3233
public function __construct(Filesystem $filesystem)
3334
{
3435
$this->filesystem = $filesystem;
35-
$this->filesystem->createDir('cache');
36+
$this->filesystem->createDir(self::CACHE_PATH);
3637
}
3738

3839
protected function fetchObjectFromCache($key)
3940
{
40-
return $this->filesystem->read($key);
41+
$file = $this->getFilePath($key);
42+
if (!$this->filesystem->has($file)) {
43+
return;
44+
}
45+
46+
return unserialize($this->filesystem->read($file));
4147
}
4248

4349
protected function clearAllObjectsFromCache()
4450
{
45-
$this->filesystem->deleteDir('cache');
46-
$this->filesystem->createDir('cache');
51+
$this->filesystem->deleteDir(self::CACHE_PATH);
52+
$this->filesystem->createDir(self::CACHE_PATH);
4753

4854
return true;
4955
}
5056

5157
protected function clearOneObjectFromCache($key)
5258
{
5359
try {
54-
return $this->filesystem->delete($key);
60+
return $this->filesystem->delete($this->getFilePath($key));
5561
} catch (FileNotFoundException $e) {
5662
return true;
5763
}
5864
}
5965

6066
protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
6167
{
62-
return $this->filesystem->write($key, $item);
68+
$file = $this->getFilePath($key);
69+
if ($this->filesystem->has($file)) {
70+
$this->filesystem->delete($file);
71+
}
72+
73+
return $this->filesystem->write($file, serialize($item));
74+
}
75+
76+
/**
77+
* @param $key
78+
*
79+
* @return mixed
80+
*/
81+
private function getFilePath($key)
82+
{
83+
return sprintf('%s/%s', self::CACHE_PATH, urlencode(base64_encode($key)));
6384
}
6485
}

tests/IntegrationPoolTest.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,28 @@
1212
namespace Cache\Adapter\Filesystem\tests;
1313

1414
use Cache\Adapter\Filesystem\FilesystemCachePool;
15-
use Cache\IntegrationTests\CachePoolTest as BaseTest;
15+
use Cache\IntegrationTests\CachePoolTest;
16+
use League\Flysystem\Adapter\Local;
17+
use League\Flysystem\Filesystem;
1618

17-
class IntegrationPoolTest extends BaseTest
19+
class IntegrationPoolTest extends CachePoolTest
1820
{
21+
/**
22+
* @var Filesystem
23+
*/
24+
private $filesystem;
25+
1926
public function createCachePool()
2027
{
21-
return new FilesystemCachePool();
28+
return new FilesystemCachePool($this->getFilesystem());
29+
}
30+
31+
private function getFilesystem()
32+
{
33+
if ($this->filesystem === null) {
34+
$this->filesystem = new Filesystem(new Local(__DIR__.'/'));
35+
}
36+
37+
return $this->filesystem;
2238
}
2339
}

tests/IntegrationTagTest.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,27 @@
1313

1414
use Cache\Adapter\Filesystem\FilesystemCachePool;
1515
use Cache\IntegrationTests\TaggableCachePoolTest;
16+
use League\Flysystem\Adapter\Local;
17+
use League\Flysystem\Filesystem;
1618

1719
class IntegrationTagTest extends TaggableCachePoolTest
1820
{
21+
/**
22+
* @var Filesystem
23+
*/
24+
private $filesystem;
25+
1926
public function createCachePool()
2027
{
21-
return new FilesystemCachePool();
28+
return new FilesystemCachePool($this->getFilesystem());
29+
}
30+
31+
private function getFilesystem()
32+
{
33+
if ($this->filesystem === null) {
34+
$this->filesystem = new Filesystem(new Local(__DIR__.'/'));
35+
}
36+
37+
return $this->filesystem;
2238
}
2339
}

0 commit comments

Comments
 (0)