Skip to content

Commit 3278cb1

Browse files
andreromnicolas-grekas
authored andcommitted
[Cache] Add optimized FileSystem & Redis TagAware Adapters
Reduces cache lookups by 50% 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.
1 parent 7e56ef1 commit 3278cb1

21 files changed

+1269
-256
lines changed

phpunit.xml.dist

+4-3
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

+2-110
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)