Skip to content

Commit

Permalink
Added a clear() method to allow purging the store completely
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Jan 16, 2020
1 parent fe0aa7c commit 1cd902d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
15 changes: 15 additions & 0 deletions src/Psr6Store.php
Expand Up @@ -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
*/
Expand Down
56 changes: 47 additions & 9 deletions tests/Psr6StoreTest.php
Expand Up @@ -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()
Expand Down

0 comments on commit 1cd902d

Please sign in to comment.