Skip to content
Permalink
Browse files

[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.
  • Loading branch information...
andrerom authored and nicolas-grekas committed Feb 22, 2019
1 parent 7e56ef1 commit 3278cb1c7e002374ba63e5edcb832a77ace69ed6
Showing with 1,269 additions and 256 deletions.
  1. +4 −3 phpunit.xml.dist
  2. +2 −110 src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
  3. +302 −0 src/Symfony/Component/Cache/Adapter/AbstractTagAwareAdapter.php
  4. +149 −0 src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php
  5. +209 −0 src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php
  6. +6 −0 src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php
  7. +3 −0 src/Symfony/Component/Cache/LockRegistry.php
  8. +28 −0 src/Symfony/Component/Cache/Tests/Adapter/FilesystemTagAwareAdapterTest.php
  9. +34 −0 src/Symfony/Component/Cache/Tests/Adapter/PredisTagAwareAdapterTest.php
  10. +34 −0 src/Symfony/Component/Cache/Tests/Adapter/PredisTagAwareClusterAdapterTest.php
  11. +34 −0 src/Symfony/Component/Cache/Tests/Adapter/PredisTagAwareRedisClusterAdapterTest.php
  12. +35 −0 src/Symfony/Component/Cache/Tests/Adapter/RedisTagAwareAdapterTest.php
  13. +34 −0 src/Symfony/Component/Cache/Tests/Adapter/RedisTagAwareArrayAdapterTest.php
  14. +35 −0 src/Symfony/Component/Cache/Tests/Adapter/RedisTagAwareClusterAdapterTest.php
  15. +5 −117 src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
  16. +22 −0 src/Symfony/Component/Cache/Tests/DependencyInjection/CachePoolPassTest.php
  17. +160 −0 src/Symfony/Component/Cache/Tests/Traits/TagAwareTestTrait.php
  18. +139 −0 src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php
  19. +2 −2 src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php
  20. +30 −23 src/Symfony/Component/Cache/Traits/RedisTrait.php
  21. +2 −1 src/Symfony/Component/Cache/phpunit.xml.dist
@@ -71,9 +71,10 @@
<element key="1"><string>Doctrine\Common\Cache</string></element>
<element key="2"><string>Symfony\Component\Cache</string></element>
<element key="3"><string>Symfony\Component\Cache\Tests\Fixtures</string></element>
<element key="4"><string>Symfony\Component\Cache\Traits</string></element>
<element key="5"><string>Symfony\Component\Console</string></element>
<element key="6"><string>Symfony\Component\HttpFoundation</string></element>
<element key="4"><string>Symfony\Component\Cache\Tests\Traits</string></element>
<element key="5"><string>Symfony\Component\Cache\Traits</string></element>
<element key="6"><string>Symfony\Component\Console</string></element>
<element key="7"><string>Symfony\Component\HttpFoundation</string></element>
</array>
</element>
</array>
@@ -11,14 +11,13 @@
namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\AbstractTrait;
use Symfony\Component\Cache\Traits\AbstractAdapterTrait;
use Symfony\Component\Cache\Traits\ContractsTrait;
use Symfony\Contracts\Cache\CacheInterface;
@@ -27,15 +26,12 @@
*/
abstract class AbstractAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
{
use AbstractTrait;
use AbstractAdapterTrait;
use ContractsTrait;
private static $apcuSupported;
private static $phpFilesSupported;
private $createCacheItem;
private $mergeByLifetime;
protected function __construct(string $namespace = '', int $defaultLifetime = 0)
{
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).':';
@@ -142,81 +138,6 @@ public static function createConnection($dsn, array $options = [])
throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn));
}
/**
* {@inheritdoc}
*/
public function getItem($key)
{
if ($this->deferred) {
$this->commit();
}
$id = $this->getId($key);
$f = $this->createCacheItem;
$isHit = false;
$value = null;
try {
foreach ($this->doFetch([$id]) as $value) {
$isHit = true;
}
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to fetch key "{key}"', ['key' => $key, 'exception' => $e]);
}
return $f($key, $value, $isHit);
}
/**
* {@inheritdoc}
*/
public function getItems(array $keys = [])
{
if ($this->deferred) {
$this->commit();
}
$ids = [];
foreach ($keys as $key) {
$ids[] = $this->getId($key);
}
try {
$items = $this->doFetch($ids);
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to fetch requested items', ['keys' => $keys, 'exception' => $e]);
$items = [];
}
$ids = array_combine($ids, $keys);
return $this->generateItems($items, $ids);
}
/**
* {@inheritdoc}
*/
public function save(CacheItemInterface $item)
{
if (!$item instanceof CacheItem) {
return false;
}
$this->deferred[$item->getKey()] = $item;
return $this->commit();
}
/**
* {@inheritdoc}
*/
public function saveDeferred(CacheItemInterface $item)
{
if (!$item instanceof CacheItem) {
return false;
}
$this->deferred[$item->getKey()] = $item;
return true;
}
/**
* {@inheritdoc}
*/
@@ -271,33 +192,4 @@ public function commit()
return $ok;
}
public function __destruct()
{
if ($this->deferred) {
$this->commit();
}
}
private function generateItems($items, &$keys)
{
$f = $this->createCacheItem;
try {
foreach ($items as $id => $value) {
if (!isset($keys[$id])) {
$id = key($keys);
}
$key = $keys[$id];
unset($keys[$id]);
yield $key => $f($key, $value, true);
}
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to fetch requested items', ['keys' => array_values($keys), 'exception' => $e]);
}
foreach ($keys as $key) {
yield $key => $f($key, null, false);
}
}
}
Oops, something went wrong.

0 comments on commit 3278cb1

Please sign in to comment.
You can’t perform that action at this time.