Skip to content

Commit

Permalink
16-purge-cache-if-missing-metadata-cannot-be-retrieved
Browse files Browse the repository at this point in the history
  • Loading branch information
jgivoni committed Feb 9, 2024
1 parent 7e9b6b4 commit 7655229
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"Flysystem"
]
}
1 change: 0 additions & 1 deletion src/CacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\StorageAttributes;
use League\Flysystem\UnableToCopyFile;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToProvideChecksum;
use League\Flysystem\UnableToReadFile;
Expand Down
8 changes: 7 additions & 1 deletion src/CacheItemsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\UnableToProvideChecksum;
use League\Flysystem\UnableToRetrieveMetadata;
use RuntimeException;

Expand Down Expand Up @@ -140,7 +141,12 @@ protected function getFileAttributes(
}

if ($attributeAccessor($fileAttributes) === null) {
$fileAttributesExtension = $loader();
try {
$fileAttributesExtension = $loader();
} catch (UnableToRetrieveMetadata | UnableToProvideChecksum $e) {
$this->purgeCacheItem($path);
throw $e;
}

$fileAttributes = self::mergeFileAttributes(
fileAttributesBase: $fileAttributes,
Expand Down
1 change: 1 addition & 0 deletions tests/CacheTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function setUp(): void
'fully-cached-file' => new FileAttributes('fully-cached-file', 10, Visibility::PUBLIC),
'partially-cached-file' => new FileAttributes('partially-cached-file'),
'deleted-cached-file' => new FileAttributes('deleted-cached-file', 10, Visibility::PUBLIC),
'partially-cached-deleted-file' => new FileAttributes('partially-cached-deleted-file'),
'overwritten-file' => new FileAttributes('overwritten-file', 20, Visibility::PUBLIC),
'cached-directory' => new DirectoryAttributes('cached-directory', visibility: Visibility::PUBLIC),
'cached-directory/file' => new FileAttributes('cached-directory/file', 10),
Expand Down
18 changes: 18 additions & 0 deletions tests/GetChecksum_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use League\Flysystem\Config;
use League\Flysystem\FileAttributes;
use League\Flysystem\UnableToProvideChecksum;
use League\Flysystem\UnableToRetrieveMetadata;
use Mockery;
use Mockery\MockInterface;

Expand Down Expand Up @@ -95,4 +96,21 @@ public static function errorDataProvider(): iterable
yield 'Path is directory (cached)' => ['cached-directory'];
yield 'Path is directory (non-cached)' => ['non-cached-directory'];
}

/**
* @test
*/
public function cache_is_purged_after_unsuccessful_get(): void
{
$path = 'partially-cached-deleted-file';

try {
$this->cacheAdapter->checksum($path, new Config);
} catch (UnableToProvideChecksum $e) {
}

$this->assertCachedItems([
$path => \null,
]);
}
}
17 changes: 17 additions & 0 deletions tests/GetFileSize_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,21 @@ public static function errorDataProvider(): iterable
yield 'Path is directory (cached)' => ['cached-directory'];
yield 'Path is directory (non-cached)' => ['non-cached-directory'];
}

/**
* @test
*/
public function cache_is_purged_after_unsuccessful_get(): void
{
$path = 'partially-cached-deleted-file';

try {
$this->cacheAdapter->fileSize($path);
} catch (UnableToRetrieveMetadata $e) {
}

$this->assertCachedItems([
$path => \null,
]);
}
}
17 changes: 17 additions & 0 deletions tests/GetLastModified_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ public static function errorDataProvider(): iterable
yield 'Path is directory (cached)' => ['cached-directory'];
yield 'Path is directory (non-cached)' => ['non-cached-directory'];
}

/**
* @test
*/
public function cache_is_purged_after_unsuccessful_get(): void
{
$path = 'partially-cached-deleted-file';

try {
$this->cacheAdapter->lastModified($path);
} catch (UnableToRetrieveMetadata $e) {
}

$this->assertCachedItems([
$path => \null,
]);
}
}
17 changes: 17 additions & 0 deletions tests/GetMimetype_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,21 @@ public static function errorDataProvider(): iterable
yield 'Path is directory (cached)' => ['cached-directory'];
yield 'Path is directory (non-cached)' => ['non-cached-directory'];
}

/**
* @test
*/
public function cache_is_purged_after_unsuccessful_get(): void
{
$path = 'partially-cached-deleted-file';

try {
$this->cacheAdapter->mimeType($path);
} catch (UnableToRetrieveMetadata $e) {
}

$this->assertCachedItems([
$path => \null,
]);
}
}
17 changes: 17 additions & 0 deletions tests/GetVisibility_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,21 @@ public static function errorDataProvider(): iterable
yield 'Path is directory (cached)' => ['cached-directory'];
yield 'Path is directory (non-cached)' => ['non-cached-directory'];
}

/**
* @test
*/
public function cache_is_purged_after_unsuccessful_get(): void
{
$path = 'partially-cached-deleted-file';

try {
$this->cacheAdapter->visibility($path);
} catch (UnableToRetrieveMetadata $e) {
}

$this->assertCachedItems([
$path => \null,
]);
}
}

0 comments on commit 7655229

Please sign in to comment.