diff --git a/src/Illuminate/Cache/FileStore.php b/src/Illuminate/Cache/FileStore.php index 3933a824bec2..4105582d44c6 100755 --- a/src/Illuminate/Cache/FileStore.php +++ b/src/Illuminate/Cache/FileStore.php @@ -248,9 +248,11 @@ public function restoreLock($name, $owner) public function forget($key) { if ($this->files->exists($file = $this->path($key))) { - $this->files->delete($this->path("illuminate:cache:flexible:created:{$key}")); - - return $this->files->delete($file); + return tap($this->files->delete($file), function ($forgotten) use ($key) { + if ($forgotten && $this->files->exists($file = $this->path("illuminate:cache:flexible:created:{$key}"))) { + $this->files->delete($file); + } + }); } return false; diff --git a/tests/Cache/CacheFileStoreTest.php b/tests/Cache/CacheFileStoreTest.php index 737c1f3bf521..66ff0b94a8f7 100755 --- a/tests/Cache/CacheFileStoreTest.php +++ b/tests/Cache/CacheFileStoreTest.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Carbon; +use Illuminate\Support\Str; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -330,6 +331,49 @@ public function testFlushIgnoreNonExistingDirectory() $this->assertFalse($result, 'Flush should not clean directory'); } + public function testItHandlesForgettingNonFlexibleKeys() + { + $store = new FileStore(new Filesystem, __DIR__); + + $key = Str::random(); + $path = $store->path($key); + $flexiblePath = "illuminate:cache:flexible:created:{$key}"; + + $store->put($key, 'value', 5); + + $this->assertFileExists($path); + $this->assertFileDoesNotExist($flexiblePath); + + $store->forget($key); + + $this->assertFileDoesNotExist($path); + $this->assertFileDoesNotExist($flexiblePath); + } + + public function itOnlyForgetsFlexibleKeysIfParentIsForgotten() + { + $store = new FileStore(new Filesystem, __DIR__); + + $key = Str::random(); + $path = $store->path($key); + $flexiblePath = "illuminate:cache:flexible:created:{$key}"; + + touch($flexiblePath); + + $this->assertFileDoesNotExist($path); + $this->assertFileExists($flexiblePath); + + $store->forget($key); + + $this->assertFileDoesNotExist($path); + $this->assertFileExists($flexiblePath); + + $store->put($key, 'value', 5); + + $this->assertFileDoesNotExist($path); + $this->assertFileDoesNotExist($flexiblePath); + } + protected function mockFilesystem() { return $this->createMock(Filesystem::class);