Skip to content

[Cache] Add DoctrineProvider, for using PSR-6 pools in Doctrine Cache #18487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/Symfony/Component/Cache/DoctrineProvider.php
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 src/Symfony/Component/Cache/Tests/DoctrineProviderTest.php
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));
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy