clearPool example

$failure = false;
        foreach ($pools as $id => $pool) {
            $io->comment(sprintf('Clearing cache pool: <info>%s</info>', $id));

            if ($pool instanceof CacheItemPoolInterface) {
                if (!$pool->clear()) {
                    $io->warning(sprintf('Cache pool "%s" could not be cleared.', $pool));
                    $failure = true;
                }
            } else {
                if (false === $this->poolClearer->clearPool($id)) {
                    $io->warning(sprintf('Cache pool "%s" could not be cleared.', $pool));
                    $failure = true;
                }
            }
        }

        if ($failure) {
            return 1;
        }

        $io->success('Cache was successfully cleared.');

        
$this->poolClearer = $poolClearer;
        $this->pools = $pools;
    }

    /** * @return string[] */
    public function warmUp(string $cacheDirectory): array
    {
        foreach ($this->pools as $pool) {
            if ($this->poolClearer->hasPool($pool)) {
                $this->poolClearer->clearPool($pool);
            }
        }

        return [];
    }

    public function isOptional(): bool
    {
        // optional cache warmers are not run when handling the request         return false;
    }
}


    public function testClearPool()
    {
        $pool = $this->createMock(CacheItemPoolInterface::class);
        $pool
            ->expects($this->once())
            ->method('clear')
            ->willReturn(true)
        ;

        $this->assertTrue((new Psr6CacheClearer(['pool' => $pool]))->clearPool('pool'));
    }

    public function testClearPoolThrowsExceptionOnUnreferencedPool()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Cache pool not found: "unknown"');
        (new Psr6CacheClearer())->clearPool('unknown');
    }
}
Home | Imprint | This part of the site doesn't use cookies.