Skip to content

Commit

Permalink
feature #18487 [Cache] Add DoctrineProvider, for using PSR-6 pools in…
Browse files Browse the repository at this point in the history
… 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
fabpot committed Apr 15, 2016
2 parents e4177a7 + 5d256dd commit 529d0fe
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/Symfony/Component/Cache/DoctrineProvider.php
@@ -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 src/Symfony/Component/Cache/Tests/DoctrineProviderTest.php
@@ -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));
}
}

0 comments on commit 529d0fe

Please sign in to comment.