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

Skip to content

Commit 3cffe56

Browse files
[Cache] Add [Taggable]CacheInterface, the easiest way to use a cache
1 parent 782ffe2 commit 3cffe56

14 files changed

+211
-8
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<argument type="service" id="cache.app" />
1616
</service>
1717

18+
<service id="cache.app.taggable" class="Symfony\Component\Cache\Adapter\TagAwareAdapter">
19+
<argument type="service" id="cache.app" />
20+
</service>
21+
1822
<service id="cache.system" parent="cache.adapter.system" public="true">
1923
<tag name="cache.pool" />
2024
</service>
@@ -122,7 +126,9 @@
122126
<service id="cache.global_clearer" parent="cache.default_clearer" public="true" />
123127
<service id="cache.app_clearer" alias="cache.default_clearer" public="true" />
124128
<service id="Psr\Cache\CacheItemPoolInterface" alias="cache.app" />
129+
<service id="Symfony\Component\Cache\TaggableCacheInterface" alias="cache.app.taggable" />
125130
<service id="Psr\SimpleCache\CacheInterface" alias="cache.app.simple" />
126131
<service id="Symfony\Component\Cache\Adapter\AdapterInterface" alias="cache.app" />
132+
<service id="Symfony\Component\Cache\CacheInterface" alias="cache.app" />
127133
</services>
128134
</container>

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@
1515
use Psr\Log\LoggerAwareInterface;
1616
use Psr\Log\LoggerInterface;
1717
use Psr\Log\NullLogger;
18+
use Symfony\Component\Cache\CacheInterface;
1819
use Symfony\Component\Cache\CacheItem;
1920
use Symfony\Component\Cache\Exception\InvalidArgumentException;
2021
use Symfony\Component\Cache\ResettableInterface;
2122
use Symfony\Component\Cache\Traits\AbstractTrait;
23+
use Symfony\Component\Cache\Traits\GetTrait;
2224

2325
/**
2426
* @author Nicolas Grekas <[email protected]>
2527
*/
26-
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface
28+
abstract class AbstractAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
2729
{
2830
use AbstractTrait;
31+
use GetTrait;
2932

3033
private static $apcuSupported;
3134
private static $phpFilesSupported;

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313

1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Log\LoggerAwareInterface;
16+
use Symfony\Component\Cache\CacheInterface;
1617
use Symfony\Component\Cache\CacheItem;
1718
use Symfony\Component\Cache\ResettableInterface;
1819
use Symfony\Component\Cache\Traits\ArrayTrait;
20+
use Symfony\Component\Cache\Traits\GetTrait;
1921

2022
/**
2123
* @author Nicolas Grekas <[email protected]>
2224
*/
23-
class ArrayAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface
25+
class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
2426
{
2527
use ArrayTrait;
28+
use GetTrait;
2629

2730
private $createCacheItem;
2831

src/Symfony/Component/Cache/Adapter/ChainAdapter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Cache\CacheItemPoolInterface;
16+
use Symfony\Component\Cache\CacheInterface;
1617
use Symfony\Component\Cache\CacheItem;
1718
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1819
use Symfony\Component\Cache\PruneableInterface;
1920
use Symfony\Component\Cache\ResettableInterface;
21+
use Symfony\Component\Cache\Traits\GetTrait;
2022

2123
/**
2224
* Chains several adapters together.
@@ -26,8 +28,10 @@
2628
*
2729
* @author Kévin Dunglas <[email protected]>
2830
*/
29-
class ChainAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
31+
class ChainAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
3032
{
33+
use GetTrait;
34+
3135
private $adapters = array();
3236
private $adapterCount;
3337
private $saveUp;

src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Cache\CacheItemPoolInterface;
16+
use Symfony\Component\Cache\CacheInterface;
1617
use Symfony\Component\Cache\CacheItem;
1718
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1819
use Symfony\Component\Cache\PruneableInterface;
1920
use Symfony\Component\Cache\ResettableInterface;
21+
use Symfony\Component\Cache\Traits\GetTrait;
2022
use Symfony\Component\Cache\Traits\PhpArrayTrait;
2123

2224
/**
@@ -26,9 +28,10 @@
2628
* @author Titouan Galopin <[email protected]>
2729
* @author Nicolas Grekas <[email protected]>
2830
*/
29-
class PhpArrayAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
31+
class PhpArrayAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
3032
{
3133
use PhpArrayTrait;
34+
use GetTrait;
3235

3336
private $createCacheItem;
3437

src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313

1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Cache\CacheItemPoolInterface;
16+
use Symfony\Component\Cache\CacheInterface;
1617
use Symfony\Component\Cache\CacheItem;
1718
use Symfony\Component\Cache\PruneableInterface;
1819
use Symfony\Component\Cache\ResettableInterface;
20+
use Symfony\Component\Cache\Traits\GetTrait;
1921
use Symfony\Component\Cache\Traits\ProxyTrait;
2022

2123
/**
2224
* @author Nicolas Grekas <[email protected]>
2325
*/
24-
class ProxyAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
26+
class ProxyAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
2527
{
2628
use ProxyTrait;
29+
use GetTrait;
2730

2831
private $namespace;
2932
private $namespaceLen;

src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
use Symfony\Component\Cache\CacheItem;
1717
use Symfony\Component\Cache\PruneableInterface;
1818
use Symfony\Component\Cache\ResettableInterface;
19+
use Symfony\Component\Cache\TaggableCacheInterface;
20+
use Symfony\Component\Cache\Traits\GetTrait;
1921
use Symfony\Component\Cache\Traits\ProxyTrait;
2022

2123
/**
2224
* @author Nicolas Grekas <[email protected]>
2325
*/
24-
class TagAwareAdapter implements TagAwareAdapterInterface, PruneableInterface, ResettableInterface
26+
class TagAwareAdapter implements TagAwareAdapterInterface, TaggableCacheInterface, PruneableInterface, ResettableInterface
2527
{
2628
const TAGS_PREFIX = "\0tags\0";
2729

2830
use ProxyTrait;
31+
use GetTrait;
2932

3033
private $deferred = array();
3134
private $createCacheItem;

src/Symfony/Component/Cache/Adapter/TraceableAdapter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
namespace Symfony\Component\Cache\Adapter;
1313

1414
use Psr\Cache\CacheItemInterface;
15+
use Symfony\Component\Cache\CacheInterface;
1516
use Symfony\Component\Cache\PruneableInterface;
1617
use Symfony\Component\Cache\ResettableInterface;
18+
use Symfony\Component\Cache\Traits\GetTrait;
1719

1820
/**
1921
* An adapter that collects data about all cache calls.
@@ -22,8 +24,10 @@
2224
* @author Tobias Nyholm <[email protected]>
2325
* @author Nicolas Grekas <[email protected]>
2426
*/
25-
class TraceableAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
27+
class TraceableAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
2628
{
29+
use GetTrait;
30+
2731
protected $pool;
2832
private $calls = array();
2933

src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace Symfony\Component\Cache\Adapter;
1313

14+
use Symfony\Component\Cache\TaggableCacheInterface;
15+
1416
/**
1517
* @author Robin Chalas <[email protected]>
1618
*/
17-
class TraceableTagAwareAdapter extends TraceableAdapter implements TagAwareAdapterInterface
19+
class TraceableTagAwareAdapter extends TraceableAdapter implements TaggableCacheInterface, TagAwareAdapterInterface
1820
{
1921
public function __construct(TagAwareAdapterInterface $pool)
2022
{
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 Psr\Cache\CacheItemInterface;
15+
16+
/**
17+
* Gets and stores items from a cache.
18+
*
19+
* @author Nicolas Grekas <[email protected]>
20+
*/
21+
interface CacheInterface
22+
{
23+
/**
24+
* @param callable(string, CacheItemInterface):mixed $callback Should return the computed value for the given key/item
25+
*
26+
* @return mixed The value corresponding to the provided key
27+
*/
28+
public function get(string $key, callable $callback);
29+
30+
/**
31+
* @param callable(string, CacheItemInterface):mixed $callback Should return the computed value for the given key/item
32+
*
33+
* @return iterable The values corresponding to the provided keys
34+
*/
35+
public function getMultiple(iterable $keys, callable $callback): iterable;
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
/**
15+
* Gets and stores items from a tag-aware cache.
16+
*
17+
* @author Nicolas Grekas <[email protected]>
18+
*/
19+
interface TaggableCacheInterface extends CacheInterface
20+
{
21+
/**
22+
* @param callable(string, CacheItem):mixed $callback Should return the computed value for the given key/item
23+
*
24+
* @return mixed The value corresponding to the provided key
25+
*/
26+
public function get(string $key, callable $callback);
27+
28+
/**
29+
* @param callable(string, CacheItem):mixed $callback Should return the computed value for the given key/item
30+
*
31+
* @return iterable The values corresponding to the provided keys
32+
*/
33+
public function getMultiple(iterable $keys, callable $callback): iterable;
34+
}

src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Cache\IntegrationTests\CachePoolTest;
1515
use Psr\Cache\CacheItemPoolInterface;
16+
use Symfony\Component\Cache\CacheItem;
1617
use Symfony\Component\Cache\PruneableInterface;
1718

1819
abstract class AdapterTestCase extends CachePoolTest
@@ -26,6 +27,51 @@ protected function setUp()
2627
}
2728
}
2829

30+
public function testGet()
31+
{
32+
if (isset($this->skippedTests[__FUNCTION__])) {
33+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
34+
}
35+
36+
$cache = $this->createCachePool();
37+
38+
$value = mt_rand();
39+
40+
$this->assertSame($value, $cache->get('foo', function (string $key, CacheItem $item) use ($value) {
41+
$this->assertSame('foo', $key);
42+
43+
return $value;
44+
}));
45+
46+
$item = $cache->getItem('foo');
47+
$this->assertSame($value, $item->get());
48+
}
49+
50+
public function testGetMultiple()
51+
{
52+
if (isset($this->skippedTests[__FUNCTION__])) {
53+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
54+
}
55+
56+
$cache = $this->createCachePool();
57+
58+
$foo = mt_rand();
59+
$cache->save($cache->getItem('foo')->set($foo));
60+
61+
$bar = mt_rand();
62+
63+
$values = $cache->getMultiple(array('foo', 'bar'), function (string $key, CacheItem $item) use ($bar) {
64+
$this->assertSame('bar', $key);
65+
66+
return $bar;
67+
});
68+
69+
$this->assertSame(compact('foo', 'bar'), $values);
70+
71+
$item = $cache->getItem('bar');
72+
$this->assertSame($bar, $item->get());
73+
}
74+
2975
public function testDefaultLifeTime()
3076
{
3177
if (isset($this->skippedTests[__FUNCTION__])) {

src/Symfony/Component/Cache/Tests/Adapter/PhpArrayAdapterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
class PhpArrayAdapterTest extends AdapterTestCase
2222
{
2323
protected $skippedTests = array(
24+
'testGetMultiple' => 'PhpArrayAdapter is read-only.',
2425
'testBasicUsage' => 'PhpArrayAdapter is read-only.',
2526
'testBasicUsageWithLongKey' => 'PhpArrayAdapter is read-only.',
2627
'testClear' => 'PhpArrayAdapter is read-only.',
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Traits;
13+
14+
/**
15+
* @author Nicolas Grekas <[email protected]>
16+
*/
17+
trait GetTrait
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function get(string $key, callable $callback)
23+
{
24+
$item = $this->getItem($key);
25+
26+
if (!$item->isHit()) {
27+
$this->save($item->set($callback($key, $item)));
28+
}
29+
30+
return $item->get();
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function getMultiple(iterable $keys, callable $callback): iterable
37+
{
38+
if ($keys instanceof \Traversable) {
39+
$keys = iterator_to_array($keys, false);
40+
}
41+
$values = array();
42+
43+
foreach ($this->getItems($keys) as $key => $item) {
44+
if (!$item->isHit()) {
45+
$this->saveDeferred($item->set($callback($key, $item)));
46+
}
47+
48+
$values[$key] = $item->get();
49+
}
50+
51+
$this->commit();
52+
53+
return $values;
54+
}
55+
}

0 commit comments

Comments
 (0)