Skip to content

[Cache] add CacheInterface::delete() + improve CacheTrait #28718

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
Oct 10, 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
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\AbstractTrait;
use Symfony\Component\Cache\Traits\GetTrait;
use Symfony\Component\Cache\Traits\ContractsTrait;
use Symfony\Contracts\Cache\CacheInterface;

/**
Expand All @@ -28,7 +28,7 @@
abstract class AbstractAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
{
use AbstractTrait;
use GetTrait;
use ContractsTrait;

private static $apcuSupported;
private static $phpFilesSupported;
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,12 @@ public function commit()
{
return true;
}

/**
* {@inheritdoc}
*/
public function delete(string $key): bool
{
return $this->deleteItem($key);
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\GetTrait;
use Symfony\Component\Cache\Traits\ContractsTrait;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Service\ResetInterface;

Expand All @@ -31,7 +31,7 @@
*/
class ChainAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
{
use GetTrait;
use ContractsTrait;

private $adapters = array();
private $adapterCount;
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Cache/Adapter/NullAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ public function commit()
return false;
}

/**
* {@inheritdoc}
*/
public function delete(string $key): bool
{
return $this->deleteItem($key);
}

private function generateItems(array $keys)
{
$f = $this->createCacheItem;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\GetTrait;
use Symfony\Component\Cache\Traits\ContractsTrait;
use Symfony\Component\Cache\Traits\PhpArrayTrait;
use Symfony\Contracts\Cache\CacheInterface;

Expand All @@ -31,7 +31,7 @@
class PhpArrayAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
{
use PhpArrayTrait;
use GetTrait;
use ContractsTrait;

private $createCacheItem;

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\GetTrait;
use Symfony\Component\Cache\Traits\ContractsTrait;
use Symfony\Component\Cache\Traits\ProxyTrait;
use Symfony\Contracts\Cache\CacheInterface;

Expand All @@ -26,7 +26,7 @@
class ProxyAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
{
use ProxyTrait;
use GetTrait;
use ContractsTrait;

private $namespace;
private $namespaceLen;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\GetTrait;
use Symfony\Component\Cache\Traits\ContractsTrait;
use Symfony\Component\Cache\Traits\ProxyTrait;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

Expand All @@ -28,7 +28,7 @@ class TagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterfac
const TAGS_PREFIX = "\0tags\0";

use ProxyTrait;
use GetTrait;
use ContractsTrait;

private $deferred = array();
private $createCacheItem;
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Cache/Adapter/TraceableAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@ public function reset()
}
}

/**
* {@inheritdoc}
*/
public function delete(string $key): bool
{
$event = $this->start(__FUNCTION__);
try {
return $event->result[$key] = $this->pool->deleteItem($key);
} finally {
$event->end = microtime(true);
}
}

public function getCalls()
{
return $this->calls;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\LockRegistry;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\CacheTrait;
use Symfony\Contracts\Cache\ItemInterface;

/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
trait GetTrait
trait ContractsTrait
{
use CacheTrait {
doGet as private contractsGet;
}

private $callbackWrapper = array(LockRegistry::class, 'compute');

/**
Expand All @@ -42,47 +47,12 @@ public function setCallbackWrapper(callable $callbackWrapper): callable
return $previousWrapper;
}

/**
* {@inheritdoc}
*/
public function get(string $key, callable $callback, float $beta = null)
{
return $this->doGet($this, $key, $callback, $beta);
}

private function doGet(AdapterInterface $pool, string $key, callable $callback, ?float $beta)
{
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 (0 < $beta) {
if ($recompute) {
$t = microtime(true);
} else {
$metadata = $item->getMetadata();
$expiry = $metadata[ItemInterface::METADATA_EXPIRY] ?? false;
$ctime = $metadata[ItemInterface::METADATA_CTIME] ?? false;

if ($ctime && $expiry) {
$t = microtime(true);
$recompute = $expiry <= $t - $ctime / 1000 * $beta * log(random_int(1, PHP_INT_MAX) / PHP_INT_MAX);
}
}
if ($recompute) {
// force applying defaultLifetime to expiry
$item->expiresAt(null);
}
}

if (!$recompute) {
return $item->get();
}

static $save;

$save = $save ?? \Closure::bind(
Expand All @@ -99,16 +69,19 @@ function (AdapterInterface $pool, ItemInterface $item, $value, float $startTime)
CacheItem::class
);

// don't wrap nor save recursive calls
if (null === $callbackWrapper = $this->callbackWrapper) {
return $callback($item);
}
$this->callbackWrapper = null;
return $this->contractsGet($pool, $key, function (CacheItem $item) use ($pool, $callback, $save) {
// don't wrap nor save recursive calls
if (null === $callbackWrapper = $this->callbackWrapper) {
return $callback($item);
}
$this->callbackWrapper = null;
$t = microtime(true);

try {
return $save($pool, $item, $callbackWrapper($item, $callback, $pool), $t);
} finally {
$this->callbackWrapper = $callbackWrapper;
}
try {
return $save($pool, $item, $callbackWrapper($item, $callback, $pool), $t);
} finally {
$this->callbackWrapper = $callbackWrapper;
}
}, $beta);
}
}
39 changes: 27 additions & 12 deletions src/Symfony/Contracts/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,46 @@

namespace Symfony\Contracts\Cache;

use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;

/**
* Gets/Stores items from/to a cache.
*
* On cache misses, a callback is called that should return the missing value.
* This callback is given an ItemInterface object corresponding to the needed key,
* that could be used e.g. for expiration control.
* Covers most simple to advanced caching needs.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
interface CacheInterface
{
/**
* @param string $key The key of the item to retrieve from the cache
* @param callable(ItemInterface):mixed $callback Should return the computed value for the given key/item
* @param float|null $beta A float that, as it grows, controls the likeliness of triggering
* early expiration. 0 disables it, INF forces immediate expiration.
* The default (or providing null) is implementation dependent but should
* typically be 1.0, which should provide optimal stampede protection.
* See https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
* Fetches a value from the pool or computes it if not found.
*
* On cache misses, a callback is called that should return the missing value.
* This callback is given a PSR-6 CacheItemInterface instance corresponding to the
* requested key, that could be used e.g. for expiration control. It could also
* be an ItemInterface instance when its additional features are needed.
*
* @param string $key The key of the item to retrieve from the cache
* @param callable(CacheItemInterface):mixed $callback Should return the computed value for the given key/item
* @param float|null $beta A float that, as it grows, controls the likeliness of triggering
* early expiration. 0 disables it, INF forces immediate expiration.
* The default (or providing null) is implementation dependent but should
* typically be 1.0, which should provide optimal stampede protection.
* See https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
*
* @return mixed The value corresponding to the provided key
*
* @throws InvalidArgumentException When $key is not valid or when $beta is negative
*/
public function get(string $key, callable $callback, float $beta = null);

/**
* Removes an item from the pool.
*
* @param string $key The key to delete
*
* @throws InvalidArgumentException When $key is not valid
*
* @return bool True if the item was successfully removed, false if there was any error
*/
public function delete(string $key): bool;
}
67 changes: 67 additions & 0 deletions src/Symfony/Contracts/Cache/CacheTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Contracts\Cache;

use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;

/**
* An implementation of CacheInterface for PSR-6 CacheItemPoolInterface classes.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
trait CacheTrait
{
/**
* {@inheritdoc}
*/
public function get(string $key, callable $callback, float $beta = null)
{
return $this->doGet($this, $key, $callback, $beta);
}

/**
* {@inheritdoc}
*/
public function delete(string $key): bool
{
return $this->deleteItem($key);
}

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

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

if (!$recompute && $item instanceof ItemInterface) {
$metadata = $item->getMetadata();
$expiry = $metadata[ItemInterface::METADATA_EXPIRY] ?? false;
$ctime = $metadata[ItemInterface::METADATA_CTIME] ?? false;

if ($recompute = $ctime && $expiry && $expiry <= microtime(true) - $ctime / 1000 * $beta * log(random_int(1, PHP_INT_MAX) / PHP_INT_MAX)) {
// force applying defaultLifetime to expiry
$item->expiresAt(null);
}
}

if ($recompute) {
$pool->save($item->set($callback($item)));
}

return $item->get();
}
}
Loading