Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require": {
"php": ">=7.4",
"doctrine/cache": "^1.3",
"league/flysystem": "^1.0",
"league/flysystem": "^2.0 || ^3.0",
"psr/cache": "^1.0 || ^2.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"psr/simple-cache": "^1.0"
Expand Down
71 changes: 47 additions & 24 deletions src/Adapter/Filesystem/FilesystemCachePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
use Cache\Adapter\Common\AbstractCachePool;
use Cache\Adapter\Common\Exception\InvalidArgumentException;
use Cache\Adapter\Common\PhpCacheItem;
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\UnableToCreateDirectory;
use League\Flysystem\UnableToDeleteDirectory;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToWriteFile;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class FilesystemCachePool extends AbstractCachePool
{
/**
* @type FilesystemInterface
* @type FilesystemOperator
*/
private $filesystem;

Expand All @@ -36,15 +39,15 @@ class FilesystemCachePool extends AbstractCachePool
private $folder;

/**
* @param FilesystemInterface $filesystem
* @param string $folder
* @param FilesystemOperator $filesystem
* @param string $folder
*/
public function __construct(FilesystemInterface $filesystem, $folder = 'cache')
public function __construct(FilesystemOperator $filesystem, $folder = 'cache')
{
$this->folder = $folder;

$this->filesystem = $filesystem;
$this->filesystem->createDir($this->folder);
$this->filesystem->createDirectory($this->folder);
}

/**
Expand All @@ -68,7 +71,7 @@ protected function fetchObjectFromCache($key)
if ($data === false) {
return $empty;
}
} catch (FileNotFoundException $e) {
} catch (UnableToReadFile $e) {
return $empty;
}

Expand All @@ -91,8 +94,17 @@ protected function fetchObjectFromCache($key)
*/
protected function clearAllObjectsFromCache()
{
$this->filesystem->deleteDir($this->folder);
$this->filesystem->createDir($this->folder);
try {
$this->filesystem->deleteDirectory($this->folder);
} catch (UnableToDeleteDirectory $e) {
return false;
}

try {
$this->filesystem->createDirectory($this->folder);
} catch (UnableToCreateDirectory $e) {
return false;
}

return true;
}
Expand All @@ -119,16 +131,13 @@ protected function storeItemInCache(PhpCacheItem $item, $ttl)
);

$file = $this->getFilePath($item->getKey());
if ($this->filesystem->has($file)) {
// Update file if it exists
return $this->filesystem->update($file, $data);
}

try {
return $this->filesystem->write($file, $data);
} catch (FileExistsException $e) {
// To handle issues when/if race conditions occurs, we try to update here.
return $this->filesystem->update($file, $data);
$this->filesystem->write($file, $data);

return true;
} catch (UnableToWriteFile $e) {
return false;
}
}

Expand All @@ -155,7 +164,7 @@ protected function getList($name)
{
$file = $this->getFilePath($name);

if (!$this->filesystem->has($file)) {
if (!$this->filesystem->fileExists($file)) {
$this->filesystem->write($file, serialize([]));
}

Expand All @@ -179,7 +188,13 @@ protected function appendListItem($name, $key)
$list = $this->getList($name);
$list[] = $key;

return $this->filesystem->update($this->getFilePath($name), serialize($list));
try {
$this->filesystem->write($this->getFilePath($name), serialize($list));

return true;
} catch (UnableToWriteFile $e) {
return false;
}
}

/**
Expand All @@ -194,7 +209,13 @@ protected function removeListItem($name, $key)
}
}

return $this->filesystem->update($this->getFilePath($name), serialize($list));
try {
$this->filesystem->write($this->getFilePath($name), serialize($list));

return true;
} catch (UnableToWriteFile $e) {
return false;
}
}

/**
Expand All @@ -205,9 +226,11 @@ protected function removeListItem($name, $key)
private function forceClear($key)
{
try {
return $this->filesystem->delete($this->getFilePath($key));
} catch (FileNotFoundException $e) {
$this->filesystem->delete($this->getFilePath($key));

return true;
} catch (UnableToDeleteFile $e) {
return false;
}
}
}
7 changes: 4 additions & 3 deletions src/Adapter/Filesystem/Tests/CreatePoolTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
namespace Cache\Adapter\Filesystem\Tests;

use Cache\Adapter\Filesystem\FilesystemCachePool;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\Local\LocalFilesystemAdapter;

trait CreatePoolTrait
{
/**
* @type Filesystem
* @type FilesystemOperator
*/
private $filesystem;

Expand All @@ -35,7 +36,7 @@ public function createSimpleCache()
private function getFilesystem()
{
if ($this->filesystem === null) {
$this->filesystem = new Filesystem(new Local(__DIR__.'/cache'.rand(1, 100000)));
$this->filesystem = new Filesystem(new LocalFilesystemAdapter(__DIR__.'/cache'.rand(1, 100000)));
}

return $this->filesystem;
Expand Down
6 changes: 3 additions & 3 deletions src/Adapter/Filesystem/Tests/FilesystemCachePoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public function testCleanupOnExpire()
$item->set('data');
$item->expiresAt(new \DateTime('now'));
$pool->save($item);
$this->assertTrue($this->getFilesystem()->has('cache/test_ttl_null'));
$this->assertTrue($this->getFilesystem()->fileExists('cache/test_ttl_null'));

sleep(1);

$item = $pool->getItem('test_ttl_null');
$this->assertFalse($item->isHit());
$this->assertFalse($this->getFilesystem()->has('cache/test_ttl_null'));
$this->assertFalse($this->getFilesystem()->fileExists('cache/test_ttl_null'));
}

public function testChangeFolder()
Expand All @@ -53,7 +53,7 @@ public function testChangeFolder()
$pool->setFolder('foobar');

$pool->save($pool->getItem('test_path'));
$this->assertTrue($this->getFilesystem()->has('foobar/test_path'));
$this->assertTrue($this->getFilesystem()->fileExists('foobar/test_path'));
}

public function testCorruptedCacheFileHandledNicely()
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Filesystem/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require": {
"php": ">=7.4",
"cache/adapter-common": "^1.0",
"league/flysystem": "^1.0",
"league/flysystem": "^2.0 || ^3.0",
"psr/cache": "^1.0 || ^2.0",
"psr/simple-cache": "^1.0"
},
Expand Down