-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Cache] Add a Chain adapter #18215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Cache] Add a Chain adapter #18215
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,27 +13,30 @@ | |
|
||
use Psr\Cache\CacheItemInterface; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Component\Cache\CacheItem; | ||
use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Chains adapters together. | ||
* Chains several adapters together. | ||
* | ||
* Saves, deletes and clears all registered adapter. | ||
* Gets data from the first chained adapter having it in cache. | ||
* Cached items are fetched from the first adapter having them in its data store. | ||
* They are saved and deleted in all adapters at once. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class ChainAdapter implements AdapterInterface | ||
{ | ||
private $adapters = array(); | ||
private $saveUp; | ||
|
||
/** | ||
* @param AdapterInterface[] $adapters | ||
* @param CacheItemPoolInterface[] $adapters The ordered list of adapters used to fetch cached items. | ||
* @param int $maxLifetime The max lifetime of items propagated from lower adapters to upper ones. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dots at the end of @param lines should be removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
*/ | ||
public function __construct(array $adapters) | ||
public function __construct(array $adapters, $maxLifetime = 0) | ||
{ | ||
if (2 > count($adapters)) { | ||
throw new InvalidArgumentException('At least two adapters must be chained.'); | ||
if (!$adapters) { | ||
throw new InvalidArgumentException('At least one adapter must be specified.'); | ||
} | ||
|
||
foreach ($adapters as $adapter) { | ||
|
@@ -47,17 +50,38 @@ public function __construct(array $adapters) | |
$this->adapters[] = new ProxyAdapter($adapter); | ||
} | ||
} | ||
|
||
$this->saveUp = \Closure::bind( | ||
function ($adapter, $item) use ($maxLifetime) { | ||
$origDefaultLifetime = $item->defaultLifetime; | ||
|
||
if (0 < $maxLifetime && ($origDefaultLifetime <= 0 || $maxLifetime < $origDefaultLifetime)) { | ||
$item->defaultLifetime = $maxLifetime; | ||
} | ||
|
||
$adapter->save($item); | ||
$item->defaultLifetime = $origDefaultLifetime; | ||
}, | ||
$this, | ||
CacheItem::class | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItem($key) | ||
{ | ||
foreach ($this->adapters as $adapter) { | ||
$saveUp = $this->saveUp; | ||
|
||
foreach ($this->adapters as $i => $adapter) { | ||
$item = $adapter->getItem($key); | ||
|
||
if ($item->isHit()) { | ||
while (0 <= --$i) { | ||
$saveUp($this->adapters[$i], $item); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicolas-grekas I don't understand why you save the item with a new lifetime for a fetch operation. Can you explain it to me plz ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because there is no way to know the remaining lifetime of the item in its original pool... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, thx |
||
} | ||
|
||
return $item; | ||
} | ||
} | ||
|
@@ -70,12 +94,36 @@ public function getItem($key) | |
*/ | ||
public function getItems(array $keys = array()) | ||
{ | ||
$items = array(); | ||
foreach ($keys as $key) { | ||
$items[$key] = $this->getItem($key); | ||
return $this->generateItems($this->adapters[0]->getItems($keys), 0); | ||
} | ||
|
||
private function generateItems($items, $adapterIndex) | ||
{ | ||
$missing = array(); | ||
$nextAdapterIndex = $adapterIndex + 1; | ||
$nextAdapter = isset($this->adapters[$nextAdapterIndex]) ? $this->adapters[$nextAdapterIndex] : null; | ||
|
||
foreach ($items as $k => $item) { | ||
if (!$nextAdapter || $item->isHit()) { | ||
yield $k => $item; | ||
} else { | ||
$missing[] = $k; | ||
} | ||
} | ||
|
||
return $items; | ||
if ($missing) { | ||
$saveUp = $this->saveUp; | ||
$adapter = $this->adapters[$adapterIndex]; | ||
$items = $this->generateItems($nextAdapter->getItems($missing), $nextAdapterIndex); | ||
|
||
foreach ($items as $k => $item) { | ||
if ($item->isHit()) { | ||
$saveUp($adapter, $item); | ||
} | ||
|
||
yield $k => $item; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PHPdoc of this class could also be reworded a bit. It's confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated