Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #18487 [Cache] Add DoctrineProvider, for using PSR-6 pools in…
… Doctrine Cache (nicolas-grekas) This PR was merged into the 3.1-dev branch. Discussion ---------- [Cache] Add DoctrineProvider, for using PSR-6 pools in Doctrine Cache | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This would allow cache pool configuration as usual (see #17290) before injecting the resulting doctrine cache provider anywhere such an interface is required. Commits ------- 5d256dd [Cache] Add DoctrineProvider, for using PSR-6 pools in Doctrine Cache
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache; | ||
|
||
use Doctrine\Common\Cache\CacheProvider; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class DoctrineProvider extends CacheProvider | ||
{ | ||
private $pool; | ||
|
||
public function __construct(CacheItemPoolInterface $pool) | ||
{ | ||
$this->pool = $pool; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doFetch($id) | ||
{ | ||
$item = $this->pool->getItem(rawurlencode($id)); | ||
|
||
return $item->isHit() ? $item->get() : false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doContains($id) | ||
{ | ||
return $this->pool->hasItem(rawurlencode($id)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doSave($id, $data, $lifeTime = 0) | ||
{ | ||
$item = $this->pool->getItem(rawurlencode($id)); | ||
|
||
if (0 < $lifeTime) { | ||
$item->expiresAfter($lifeTime); | ||
} | ||
|
||
return $this->pool->save($item->set($data)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doDelete($id) | ||
{ | ||
return $this->pool->deleteItem(rawurlencode($id)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doFlush() | ||
{ | ||
$this->pool->clear(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doGetStats() | ||
{ | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Symfony/Component/Cache/Tests/DoctrineProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Tests; | ||
|
||
use Doctrine\Common\Cache\CacheProvider; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Cache\DoctrineProvider; | ||
|
||
class DoctrineProviderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testProvider() | ||
{ | ||
$pool = new ArrayAdapter(); | ||
$cache = new DoctrineProvider($pool); | ||
|
||
$this->assertInstanceOf(CacheProvider::class, $cache); | ||
|
||
$key = '{}()/\@:'; | ||
|
||
$this->assertTrue($cache->delete($key)); | ||
$this->assertFalse($cache->contains($key)); | ||
|
||
$this->assertTrue($cache->save($key, 'bar')); | ||
$this->assertTrue($cache->contains($key)); | ||
$this->assertSame('bar', $cache->fetch($key)); | ||
|
||
$this->assertTrue($cache->delete($key)); | ||
$this->assertFalse($cache->fetch($key)); | ||
$this->assertTrue($cache->save($key, 'bar')); | ||
|
||
$cache->flushAll(); | ||
$this->assertFalse($cache->fetch($key)); | ||
$this->assertFalse($cache->contains($key)); | ||
} | ||
} |