Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc2ae87

Browse files
[Cache] Add DoctrineProvider, for using PSR-6 pools in Doctrine Cache
1 parent f1d12a1 commit dc2ae87

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache;
13+
14+
use Doctrine\Common\Cache\CacheProvider;
15+
use Psr\Cache\CacheItemPoolInterface;
16+
17+
/**
18+
* @author Nicolas Grekas <[email protected]>
19+
*/
20+
class DoctrineProvider extends CacheProvider
21+
{
22+
private $pool;
23+
24+
public function __construct(CacheItemPoolInterface $pool)
25+
{
26+
$this->pool = $pool;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function doFetch($id)
33+
{
34+
$item = $this->pool->getItem(rawurlencode($id));
35+
36+
return $item->isHit() ? $item->get() : false;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected function doContains($id)
43+
{
44+
return $this->pool->hasItem(rawurlencode($id));
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
protected function doSave($id, $data, $lifeTime = 0)
51+
{
52+
$item = $this->pool->getItem(rawurlencode($id));
53+
54+
if (0 < $lifeTime) {
55+
$item->expiresAfter($lifeTime);
56+
}
57+
58+
return $this->pool->save($item->set($data));
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
protected function doDelete($id)
65+
{
66+
return $this->pool->deleteItem(rawurlencode($id));
67+
}
68+
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
protected function doFlush()
73+
{
74+
$this->pool->clear();
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
protected function doGetStats()
81+
{
82+
}
83+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests;
13+
14+
use Doctrine\Common\Cache\CacheProvider;
15+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
16+
use Symfony\Component\Cache\DoctrineProvider;
17+
18+
class DoctrineProviderTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function testProvider()
21+
{
22+
$pool = new ArrayAdapter();
23+
$cache = new DoctrineProvider($pool);
24+
25+
$this->assertInstanceOf(CacheProvider::class, $cache);
26+
27+
$key = '{}()/\@:';
28+
29+
$this->assertTrue($cache->delete($key));
30+
$this->assertFalse($cache->contains($key));
31+
32+
$this->assertTrue($cache->save($key, 'bar'));
33+
$this->assertTrue($cache->contains($key));
34+
$this->assertSame('bar', $cache->fetch($key));
35+
36+
$this->assertTrue($cache->delete($key));
37+
$this->assertFalse($cache->fetch($key));
38+
$this->assertTrue($cache->save($key, 'bar'));
39+
40+
$cache->flushAll();
41+
$this->assertFalse($cache->fetch($key));
42+
$this->assertFalse($cache->contains($key));
43+
}
44+
}

0 commit comments

Comments
 (0)