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

Skip to content

Commit 5a89638

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

18 files changed

+283
-9
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: 5 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;
@@ -55,6 +58,7 @@ function ($key, $value, CacheItem $protoItem) {
5558
);
5659
$this->setCacheItemTags = \Closure::bind(
5760
function (CacheItem $item, $key, array &$itemTags) {
61+
$item->isTaggable = true;
5862
if (!$item->isHit) {
5963
return $item;
6064
}

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
{

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* added `CacheInterface` and `TaggableCacheInterface`
8+
* throw `LogicException` when `CacheItem::tag()` is called on an item coming from a non tag-aware pool
9+
410
3.4.0
511
-----
612

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;
13+
14+
use Psr\Cache\CacheItemInterface;
15+
16+
/**
17+
* Gets and stores items from a cache.
18+
*
19+
* On cache misses, a callback is called that should return the missing value.
20+
* It is given two arguments:
21+
* - the missing cache key
22+
* - the corresponding PSR-6 CacheItemInterface object,
23+
* allowing time-based expiration control.
24+
*
25+
* If you need tag-based invalidation, use TaggableCacheInterface instead.
26+
*
27+
* @author Nicolas Grekas <[email protected]>
28+
*/
29+
interface CacheInterface
30+
{
31+
/**
32+
* @param callable(string $key, CacheItemInterface $item):mixed $callback Should return the computed value for the given key/item
33+
*
34+
* @return mixed The value corresponding to the provided key
35+
*/
36+
public function get(string $key, callable $callback);
37+
38+
/**
39+
* @param callable(string $key, CacheItemInterface $item):mixed $callback Should return the computed value for the given key/item
40+
*
41+
* @return iterable The values corresponding to the provided keys
42+
*/
43+
public function getMultiple(iterable $keys, callable $callback): iterable;
44+
}

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Log\LoggerInterface;
1616
use Symfony\Component\Cache\Exception\InvalidArgumentException;
17+
use Symfony\Component\Cache\Exception\LogicException;
1718

1819
/**
1920
* @author Nicolas Grekas <[email protected]>
@@ -29,6 +30,7 @@ final class CacheItem implements CacheItemInterface
2930
protected $prevTags = array();
3031
protected $innerItem;
3132
protected $poolHash;
33+
protected $isTaggable = false;
3234

3335
/**
3436
* {@inheritdoc}
@@ -109,7 +111,10 @@ public function expiresAfter($time)
109111
*/
110112
public function tag($tags)
111113
{
112-
if (!is_array($tags)) {
114+
if (!$this->isTaggable) {
115+
throw new LogicException(sprintf('Cache item "%s" comes from a non tag-aware pool: you cannot tag it.', $this->key));
116+
}
117+
if (!\is_iterable($tags)) {
113118
$tags = array($tags);
114119
}
115120
foreach ($tags as $tag) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\Exception;
13+
14+
use Psr\Cache\InvalidArgumentException as Psr6CacheInterface;
15+
use Psr\SimpleCache\InvalidArgumentException as SimpleCacheInterface;
16+
17+
class LogicException extends \LogicException implements Psr6CacheInterface, SimpleCacheInterface
18+
{
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
* On cache misses, a callback is called that should return the missing value.
18+
* It is given two arguments:
19+
* - the missing cache key
20+
* - the corresponding Symfony CacheItem object,
21+
* allowing time-based *and* tags-based expiration control
22+
*
23+
* If you don't need tags-based invalidation, use CacheInterface instead.
24+
*
25+
* @author Nicolas Grekas <[email protected]>
26+
*/
27+
interface TaggableCacheInterface extends CacheInterface
28+
{
29+
/**
30+
* @param callable(string $key, CacheItem $item):mixed $callback Should return the computed value for the given key/item
31+
*
32+
* @return mixed The value corresponding to the provided key
33+
*/
34+
public function get(string $key, callable $callback);
35+
36+
/**
37+
* @param callable(string $key, CacheItem $item):mixed $callback Should return the computed value for the given key/item
38+
*
39+
* @return iterable The values corresponding to the provided keys
40+
*/
41+
public function getMultiple(iterable $keys, callable $callback): iterable;
42+
}

0 commit comments

Comments
 (0)