8000 [Cache] add "setCallbackWrapper()" on adapters implementing CacheInterface for more flexibility by nicolas-grekas · Pull Request #28588 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] add "setCallbackWrapper()" on adapters implementing CacheInterface for more flexibility #28588

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

Merged
merged 1 commit into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function get(string $key, callable $callback, float $beta = null)
if ($adapter instanceof CacheInterface) {
$value = $adapter->get($key, $callback, $beta);
} else {
$value = $this->doGet($adapter, $key, $callback, $beta ?? 1.0);
$value = $this->doGet($adapter, $key, $callback, $beta);
}
if (null !== $item) {
($this->syncItem)($lastItem = $lastItem ?? $item, $item);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function get(string $key, callable $callback, float $beta = null)
return $this->pool->get($key, $callback, $beta);
}

return $this->doGet($this->pool, $key, $callback, $beta ?? 1.0);
return $this->doGet($this->pool, $key, $callback, $beta);
}
$value = $this->values[$this->keys[$key]];

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function (CacheItemInterface $innerItem, array $item) {
public function get(string $key, callable $callback, float $beta = null)
{
if (!$this->pool instanceof CacheInterface) {
return $this->doGet($this, $key, $callback, $beta ?? 1.0);
return $this->doGet($this, $key, $callback, $beta);
}

return $this->pool->get($this->getId($key), function ($innerItem) use ($key, $callback) {
Expand Down
34 changes: 7 additions & 27 deletions src/Symfony/Component/Cache/LockRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Component\Cache;

use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

/**
* LockRegistry is used internally by existing adapters to protect against cache stampede.
Expand Down Expand Up @@ -75,40 +75,20 @@ public static function setFiles(array $files): array
return $previousFiles;
}

/**
* @internal
*/
public static function save(string $key, CacheItemPoolInterface $pool, CacheItemInterface $item, callable $callback, float $startTime, &$value): bool
public static function compute(ItemInterface $item, callable $callback, CacheInterface $pool)
{
self::$save = self::$save ?? \Closure::bind(
function (CacheItemPoolInterface $pool, CacheItemInterface $item, $value, float $startTime) {
if ($item instanceof CacheItem && $startTime && $item->expiry > $endTime = microtime(true)) {
$item->newMetadata[CacheItem::METADATA_EXPIRY] = $item->expiry;
$item->newMetadata[CacheItem::METADATA_CTIME] = 1000 * (int) ($endTime - $startTime);
}
$pool->save($item->set($value));

return $value;
},
null,
CacheItem::class
);

$key = self::$files ? crc32($key) % \count(self::$files) : -1;
$key = self::$files ? crc32($item->getKey()) % \count(self::$files) : -1;

if ($key < 0 || (self::$lockedFiles[$key] ?? false) || !$lock = self::open($key)) {
$value = (self::$save)($pool, $item, $callback($item), $startTime);

return true;
return $callback($item);
}

try {
// race to get the lock in non-blocking mode
if (flock($lock, LOCK_EX | LOCK_NB)) {
self::$lockedFiles[$key] = true;
$value = (self::$save)($pool, $item, $callback($item), $startTime);

return true;
return $callback($item);
}
// if we failed the race, retry locking in blocking mode to wait for the winner
flock($lock, LOCK_SH);
Expand All @@ -117,7 +97,7 @@ function (CacheItemPoolInterface $pool, CacheItemInterface $item, $value, float
unset(self::$lockedFiles[$key]);
}

return false;
return $pool->get($item->getKey(), $callback, 0);
}

private static function open(int $key)
Expand Down
73 changes: 54 additions & 19 deletions src/Symfony/Component/Cache/Traits/GetTrait.php
6D4E
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,62 @@

namespace Symfony\Component\Cache\Traits;

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\LockRegistry;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

/**
* An implementation for CacheInterface that provides stampede protection via probabilistic early expiration.
*
* @see https://en.wikipedia.org/wiki/Cache_stampede
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should move the comment where you moved the implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is on the contracts interface now, that's why I removed it here.

* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
trait GetTrait
{
private $callbackWrapper = array(LockRegistry::class, 'compute');

/**
* Wraps the callback passed to ->get() in a callable.
*
* @param callable(ItemInterface, callable, CacheInterface):mixed $callbackWrapper
*
* @return callable the previous callback wrapper
*/
public function setCallbackWrapper(callable $callbackWrapper): callable
{
$previousWrapper = $this->callbackWrapper;
$this->callbackWrapper = $callbackWrapper;

return $previousWrapper;
}

/**
* {@inheritdoc}
*/
public function get(string $key, callable $callback, float $beta = null)
{
if (0 > $beta) {
throw new InvalidArgumentException(sprintf('Argument "$beta" provided to "%s::get()" must be a positive number, %f given.', \get_class($this), $beta));
}

return $this->doGet($this, $key, $callback, $beta ?? 1.0);
return $this->doGet($this, $key, $callback, $beta);
}

private function doGet(CacheItemPoolInterface $pool, string $key, callable $callback, float $beta)
private function doGet(AdapterInterface $pool, string $key, callable $callback, ?float $beta)
{
retry:
if (0 > $beta = $beta ?? 1.0) {
throw new InvalidArgumentException(sprintf('Argument "$beta" provided to "%s::get()" must be a positive number, %f given.', \get_class($this), $beta));
}

$t = 0;
$item = $pool->getItem($key);
$recompute = !$item->isHit() || INF === $beta;

if ($item instanceof CacheItem && 0 < $beta) {
if (0 < $beta) {
if ($recompute) {
$t = microtime(true);
} else {
$metadata = $item->getMetadata();
$expiry = $metadata[CacheItem::METADATA_EXPIRY] ?? false;
$ctime = $metadata[CacheItem::METADATA_CTIME] ?? false;
$expiry = $metadata[ItemInterface::METADATA_EXPIRY] ?? false;
$ctime = $metadata[ItemInterface::METADATA_CTIME] ?? false;

if ($ctime && $expiry) {
$t = microtime(true);
Expand All @@ -69,11 +83,32 @@ private function doGet(CacheItemPoolInterface $pool, string $key, callable $call
return $item->get();
}

if (!LockRegistry::save($key, $pool, $item, $callback, $t, $value)) {
$beta = 0;
goto retry;
static $save;

$save = $save ?? \Closure::bind(
function (AdapterInterface $pool, ItemInterface $item, $value, float $startTime) {
if ($startTime && $item->expiry > $endTime = microtime(true)) {
$item->newMetadata[ItemInterface::METADATA_EXPIRY] = $item->expiry;
$item->newMetadata[ItemInterface::METADATA_CTIME] = 1000 * (int) ($endTime - $startTime);
}
$pool->save($item->set($value));

return $value;
},
null,
CacheItem::class
);

// don't wrap nor save recursive calls
if (null === $callbackWrapper = $this->callbackWrapper) {
return $callback($item);
}
$this->callbackWrapper = null;

return $value;
try {
return $save($pool, $item, $callbackWrapper($item, $callback, $pool), $t);
} finally {
$this->callbackWrapper = $callbackWrapper;
}
}
}
0