8000 feature #30370 [Cache] Add optimized FileSystem & Redis TagAware Adap… · symfony/symfony@fba11b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit fba11b4

Browse files
committed
feature #30370 [Cache] Add optimized FileSystem & Redis TagAware Adapters (andrerom)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Cache] Add optimized FileSystem & Redis TagAware Adapters | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes _TODO: src/**/CHANGELOG.md_ | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28250 | License | MIT | Doc PR | symfony/symfony-docs#... TODO Reduces cache lookups by 50% when using TagAware, by changing logic of how tag information is stored to avoid having to look it up on getItem(s) calls. For Filesystem symlinks are used, for Redis "Set" datatype is used. Commits ------- 3278cb1 [Cache] Add optimized FileSystem & Redis TagAware Adapters
2 parents a7d2019 + 3278cb1 commit fba11b4

21 files changed

+1269
-256
lines changed

phpunit.xml.dist

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@
7171
<element key="1"><string>Doctrine\Common\Cache</string></element>
7272
<element key="2"><string>Symfony\Component\Cache</string></element>
7373
<element key="3"><string>Symfony\Component\Cache\Tests\Fixtures</string></element>
74-
<element key="4"><string>Symfony\Component\Cache\Traits</string></element>
75-
<element key="5"><string>Symfony\Component\Console</string></element>
76-
<element key="6"><string>Symfony\Component\HttpFoundation</string></element>
74+
<element key="4"><string>Symfony\Component\Cache\Tests\Traits</string></element>
75+
<element key="5"><string>Symfony\Component\Cache\Traits</string></element>
76+
<element key="6"><string>Symfony\Component\Console</string></element>
77+
<element key="7"><string>Symfony\Component\HttpFoundation</string></element>
7778
</array>
7879
</element>
7980
</array>

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

Lines changed: 2 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111

1212
namespace Symfony\Component\Cache\Adapter;
1313

14-
use Psr\Cache\CacheItemInterface;
1514
use Psr\Log\LoggerAwareInterface;
1615
use Psr\Log\LoggerInterface;
1716
use Psr\Log\NullLogger;
1817
use Symfony\Component\Cache\CacheItem;
1918
use Symfony\Component\Cache\Exception\InvalidArgumentException;
2019
use Symfony\Component\Cache\ResettableInterface;
21-
use Symfony\Component\Cache\Traits\AbstractTrait;
20+
use Symfony\Component\Cache\Traits\AbstractAdapterTrait;
2221
use Symfony\Component\Cache\Traits\ContractsTrait;
2322
use Symfony\Contracts\Cache\CacheInterface;
2423

@@ -27,15 +26,12 @@
2726
*/
2827
abstract class AbstractAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
2928
{
30-
use AbstractTrait;
29+
use AbstractAdapterTrait;
3130
use ContractsTrait;
3231

3332
private static $apcuSupported;
3433
private static $phpFilesSupported;
3534

36-
private $createCacheItem;
37-
private $mergeByLifetime;
38-
3935
protected function __construct(string $namespace = '', int $defaultLifetime = 0)
4036
{
4137
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).':';
@@ -142,81 +138,6 @@ public static function createConnection($dsn, array $options = [])
142138
throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn));
143139
}
144140

145-
/**
146-
* {@inheritdoc}
147-
*/
148-
public function getItem($key)
149-
{
150-
if ($this->deferred) {
151-
$this->commit();
152-
}
153-
$id = $this->getId($key);
154-
155-
$f = $this->createCacheItem;
156-
$isHit = false;
157-
$value = null;
158-
159-
try {
160-
foreach ($this->doFetch([$id]) as $value) {
161-
$isHit = true;
162-
}
163-
} catch (\Exception $e) {
164-
CacheItem::log($this->logger, 'Failed to fetch key "{key}"', ['key' => $key, 'exception' => $e]);
165-
}
166-
167-
return $f($key, $value, $isHit);
168-
}
169-
170-
/**
171-
* {@inheritdoc}
172-
*/
173-
public function getItems(array $keys = [])
174-
{
175-
if ($this->deferred) {
176-
$this->commit();
177-
}
178-
$ids = [];
179-
180-
foreach ($keys as $key) {
181-
$ids[] = $this->getId($key);
182-
}
183-
try {
184-
$items = $this->doFetch($ids);
185-
} catch (\Exception $e) {
186-
CacheItem::log($this->logger, 'Failed to fetch requested items', ['keys' => $keys, 'exception' => $e]);
187-
$items = [];
188-
}
189-
$ids = array_combine($ids, $keys);
190-
191-
return $this->generateItems($items, $ids);
192-
}
193-
194-
/**
195-
* {@inheritdoc}
196-
*/
197-
public function save(CacheItemInterface $item)
198-
{
199-
if (!$item instanceof CacheItem) {
200-
return false;
201-
}
202-
$this->deferred[$item->getKey()] = $item;
203-
204-
return $this->commit();
205-
}
206-
207-
/**
208-
* {@inheritdoc}
209-
*/
210-
public function saveDeferred(CacheItemInterface $item)
211-
{
212-
if (!$item instanceof CacheItem) {
213-
return false;
214-
}
215-
$this->deferred[$item->getKey()] = $item;
216-
217-
return true;
218-
}
219-
220141
/**
221142
* {@inheritdoc}
222143
*/
@@ -271,33 +192,4 @@ public function commit()
271192

272193
return $ok;
273194
}
274-
275-
public function __destruct()
276-
{
277-
if ($this->deferred) {
278-
$this->commit();
279-
}
280-
}
281-
282-
private function generateItems($items, &$keys)
283-
{
284-
$f = $this->createCacheItem;
285-
286-
try {
287-
foreach ($items as $id => $value) {
288-
if (!isset($keys[$id])) {
289-
$id = key($keys);
290-
}
291-
$key = $keys[$id];
292-
unset($keys[$id]);
293-
yield $key => $f($key, $value, true);
294-
}
295-
} catch (\Exception $e) {
296-
CacheItem::log($this->logger, 'Failed to fetch requested items', ['keys' => array_values($keys), 'exception' => $e]);
297-
}
298-
299-
foreach ($keys as $key) {
300-
yield $key => $f($key, null, false);
301-
}
302-
}
303195
}

0 commit comments

Comments
 (0)
0