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

Skip to content

Commit 82df0a6

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

20 files changed

+364
-27
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: 53 additions & 17 deletions
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,17 +28,19 @@
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;
33-
private $saveUp;
37+
private $syncItem;
3438

3539
/**
36-
* @param CacheItemPoolInterface[] $adapters The ordered list of adapters used to fetch cached items
37-
* @param int $maxLifetime The max lifetime of items propagated from lower adapters to upper ones
40+
* @param CacheItemPoolInterface[] $adapters The ordered list of adapters used to fetch cached items
41+
* @param int $defaultLifetime The default lifetime of items propagated from lower adapters to upper ones
3842
*/
39-
public function __construct(array $adapters, int $maxLifetime = 0)
43+
public function __construct(array $adapters, int $defaultLifetime = 0)
4044
{
4145
if (!$adapters) {
4246
throw new InvalidArgumentException('At least one adapter must be specified.');
@@ -55,16 +59,20 @@ public function __construct(array $adapters, int $maxLifetime = 0)
5559
}
5660
$this->adapterCount = count($this->adapters);
5761

58-
$this->saveUp = \Closure::bind(
59-
function ($adapter, $item) use ($maxLifetime) {
60-
$origDefaultLifetime = $item->defaultLifetime;
62+
$this->syncItem = \Closure::bind(
63+
function ($sourceItem, $item) use ($defaultLifetime) {
64+
$item->value = $sourceItem->value;
65+
$item->expiry = $sourceItem->expiry;
66+
$item->isHit = $sourceItem->isHit;
6167

62-
if (0 < $maxLifetime && ($origDefaultLifetime <= 0 || $maxLifetime < $origDefaultLifetime)) {
63-
$item->defaultLifetime = $maxLifetime;
68+
if (0 < $sourceItem->defaultLifetime && $sourceItem->defaultLifetime < $defaultLifetime) {
69+
$defaultLifetime = $sourceItem->defaultLifetime;
70+
}
71+
if (0 < $defaultLifetime && ($item->defaultLifetime <= 0 || $defaultLifetime < $item->defaultLifetime)) {
72+
$item->defaultLifetime = $defaultLifetime;
6473
}
6574

66-
$adapter->save($item);
67-
$item->defaultLifetime = $origDefaultLifetime;
75+
return $item;
6876
},
6977
null,
7078
CacheItem::class
@@ -74,20 +82,47 @@ function ($adapter, $item) use ($maxLifetime) {
7482
/**
7583
* {@inheritdoc}
7684
*/
77-
public function getItem($key)
85+
public function get(string $key, callable $callback)
7886
{
79-
$saveUp = $this->saveUp;
87+
$computedItem = null;
88+
$i = -1;
89+
$wrap = function (CacheItem $item = null) use ($key, $callback, &$wrap, &$i, &$computedItem) {
90+
if (!$adapter = $this->adapters[++$i] ?? null) {
91+
$value = $callback($item);
92+
$computedItem = $item;
93+
} elseif ($adapter instanceof CacheInterface) {
94+
$value = $adapter->get($key, $wrap);
95+
} else {
96+
$value = $this->doGet($adapter, $key, $wrap);
97+
}
98+
if (null !== $item) {
99+
($this->syncItem)($computedItem ?? $item, $item);
100+
}
101+
102+
return $value;
103+
};
80104

105+
return $wrap();
106+
}
107+
108+
/**
109+
* {@inheritdoc}
110+
*/
111+
public function getItem($key)
112+
{
113+
$misses = array();
81114
foreach ($this->adapters as $i => $adapter) {
82115
$item = $adapter->getItem($key);
83116

84117
if ($item->isHit()) {
85118
while (0 <= --$i) {
86-
$saveUp($this->adapters[$i], $item);
119+
$this->adapters[$i]->save(($this->syncItem)($item, $misses[$i]));
87120
}
88121

89122
return $item;
90123
}
124+
125+
$misses[$i] = $item;
91126
}
92127

93128
return $item;
@@ -104,6 +139,7 @@ public function getItems(array $keys = array())
104139
private function generateItems($items, $adapterIndex)
105140
{
106141
$missing = array();
142+
$misses = array();
107143
$nextAdapterIndex = $adapterIndex + 1;
108144
$nextAdapter = isset($this->adapters[$nextAdapterIndex]) ? $this->adapters[$nextAdapterIndex] : null;
109145

@@ -112,17 +148,17 @@ private function generateItems($items, $adapterIndex)
112148
yield $k => $item;
113149
} else {
114150
$missing[] = $k;
151+
$misses[$k] = $item;
115152
}
116153
}
117154

118155
if ($missing) {
119-
$saveUp = $this->saveUp;
120156
$adapter = $this->adapters[$adapterIndex];
121157
$items = $this->generateItems($nextAdapter->getItems($missing), $nextAdapterIndex);
122158

123159
foreach ($items as $k => $item) {
124160
if ($item->isHit()) {
125-
$saveUp($adapter, $item);
161+
$adapter->save(($this->syncItem)($item, $misses[$k]));
126162
}
127163

128164
yield $k => $item;

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

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

1414
use Psr\Cache\CacheItemInterface;
15+
use Symfony\Component\Cache\CacheInterface;
1516
use Symfony\Component\Cache\CacheItem;
17+
use Symfony\Component\Cache\Traits\GetTrait;
1618

1719
/**
1820
* @author Titouan Galopin <[email protected]>
1921
*/
20-
class NullAdapter implements AdapterInterface
22+
class NullAdapter implements AdapterInterface, CacheInterface
2123
{
24+
use GetTrait;
25+
2226
private $createCacheItem;
2327

2428
public function __construct()

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

Lines changed: 32 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

@@ -77,6 +80,34 @@ public static function create($file, CacheItemPoolInterface $fallbackPool)
7780
return $fallbackPool;
7881
}
7982

83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function get(string $key, callable $callback)
87+
{
88+
if (!\is_string($key)) {
89+
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
90+
}
91+
if (null === $this->values) {
92+
$this->initialize();
93+
}
94+
if (null === $value = $this->values[$key] ?? null) {
95+
if ($this->pool instanceof CacheInterface) {
96+
return $this->pool->get($key, $callback);
97+
}
98+
99+
return $this->doGet($this->pool, $key, $callback);
100+
}
101+
if ('N;' === $value) {
102+
return null;
103+
}
104+
if (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
105+
return unserialize($value);
106+
}
107+
108+
return $value;
109+
}
110+
80111
/**
81112
* {@inheritdoc}
82113
*/

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

Lines changed: 20 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;
@@ -54,6 +57,22 @@ function ($key, $innerItem) use ($defaultLifetime, $poolHash) {
5457
);
5558
}
5659

60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function get(string $key, callable $callback)
64+
{
65+
$callback = function ($item) use ($key, $callback) {
66+
return $callback(($this->createCacheItem)($key, $item));
67+
};
68+
69+
if ($this->pool instanceof CacheInterface) {
70+
return $this->pool->get($this->getId($key), $callback);
71+
}
72+
73+
return $this->doGet($this->pool, $key, $callback);
74+
}
75+
5776
/**
5877
* {@inheritdoc}
5978
*/

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
}

0 commit comments

Comments
 (0)