diff --git a/src/Psr6Store.php b/src/Psr6Store.php index beb75e7..98ac10a 100644 --- a/src/Psr6Store.php +++ b/src/Psr6Store.php @@ -423,6 +423,21 @@ public function prune() } } + /** + * {@inheritdoc} + */ + public function clear() + { + // Make sure we do not have multiple pruning processes running + $lock = $this->lockFactory->createLock('clear-lock'); + + if ($lock->acquire()) { + $this->cache->clear(); + + $lock->release(); + } + } + /** * @return string */ diff --git a/tests/Psr6StoreTest.php b/tests/Psr6StoreTest.php index ce996ee..a7b3c16 100644 --- a/tests/Psr6StoreTest.php +++ b/tests/Psr6StoreTest.php @@ -498,20 +498,58 @@ public function testInvalidate() public function testPurge() { - $request = Request::create('https://foobar.com/'); - $response = new Response('hello world', 200); - $response->headers->set('Foobar', 'whatever'); + // Request 1 + $request1 = Request::create('https://foobar.com/'); + $response1 = new Response('hello world', 200); - $this->store->write($request, $response); - $cacheKey = $this->store->getCacheKey($request); + // Request 2 + $request2 = Request::create('https://foobar.com/foobar'); + $response2 = new Response('hello world', 200); - $cacheItem = $this->getCache()->getItem($cacheKey); - $this->assertTrue($cacheItem->isHit()); + $this->store->write($request1, $response1); + $this->store->write($request2, $response2); + $cacheKey1 = $this->store->getCacheKey($request1); + $cacheKey2 = $this->store->getCacheKey($request2); + + $cacheItem1 = $this->getCache()->getItem($cacheKey1); + $cacheItem2 = $this->getCache()->getItem($cacheKey2); + $this->assertTrue($cacheItem1->isHit()); + $this->assertTrue($cacheItem2->isHit()); $this->store->purge('https://foobar.com/'); - $cacheItem = $this->getCache()->getItem($cacheKey); - $this->assertFalse($cacheItem->isHit()); + $cacheItem1 = $this->getCache()->getItem($cacheKey1); + $cacheItem2 = $this->getCache()->getItem($cacheKey2); + $this->assertFalse($cacheItem1->isHit()); + $this->assertTrue($cacheItem2->isHit()); + } + + public function testClear() + { + // Request 1 + $request1 = Request::create('https://foobar.com/'); + $response1 = new Response('hello world', 200); + + // Request 2 + $request2 = Request::create('https://foobar.com/foobar'); + $response2 = new Response('hello world', 200); + + $this->store->write($request1, $response1); + $this->store->write($request2, $response2); + $cacheKey1 = $this->store->getCacheKey($request1); + $cacheKey2 = $this->store->getCacheKey($request2); + + $cacheItem1 = $this->getCache()->getItem($cacheKey1); + $cacheItem2 = $this->getCache()->getItem($cacheKey2); + $this->assertTrue($cacheItem1->isHit()); + $this->assertTrue($cacheItem2->isHit()); + + $this->store->clear(); + + $cacheItem1 = $this->getCache()->getItem($cacheKey1); + $cacheItem2 = $this->getCache()->getItem($cacheKey2); + $this->assertFalse($cacheItem1->isHit()); + $this->assertFalse($cacheItem2->isHit()); } public function testPruneIgnoredIfCacheBackendDoesNotImplementPrunableInterface()