-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Contracts] Add Cache contract to extend PSR-6 with tag invalidation, callback-based computation and stampede protection #28096
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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\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. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
interface CacheInterface | ||
{ | ||
/** | ||
* @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 | ||
* | ||
* @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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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; | ||
|
||
/** | ||
* A simple implementation of CacheInterface::get() for PSR-6 CacheItemPoolInterface classes. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
trait GetForCacheItemPoolTrait | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get(string $key, callable $callback, float $beta = null) | ||
{ | ||
if (0 > $beta) { | ||
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 = $this->getItem($key); | ||
|
||
if (INF === $beta || !$item->isHit()) { | ||
$value = $callback($item); | ||
$item->set($value); | ||
} | ||
|
||
return $item->get(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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\CacheException; | ||
use Psr\Cache\CacheItemInterface; | ||
use Psr\Cache\InvalidArgumentException; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
interface ItemInterface extends CacheItemInterface | ||
{ | ||
/** | ||
* References the Unix timestamp stating when the item will expire. | ||
*/ | ||
const METADATA_EXPIRY = 'expiry'; | ||
|
||
/** | ||
* References the time the item took to be created, in milliseconds. | ||
*/ | ||
const METADATA_CTIME = 'ctime'; | ||
|
||
/** | ||
* References the list of tags that were assigned to the item, as string[]. | ||
*/ | ||
const METADATA_TAGS = 'tags'; | ||
|
||
/** | ||
* Adds a tag to a cache item. | ||
* | ||
* Tags are strings that follow the same validation rules as keys. | ||
* | ||
* @param string|string[] $tags A tag or array of tags | ||
* | ||
* @return $this | ||
* | ||
* @throws InvalidArgumentException When $tag is not valid | ||
* @throws CacheException When the item comes from a pool that is not tag-aware | ||
*/ | ||
public function tag($tags): self; | ||
|
||
/** | ||
* Returns a list of metadata info that were saved alongside with the cached value. | ||
* | ||
* See ItemInterface::METADATA_* consts for keys potentially found in the returned array. | ||
*/ | ||
public function getMetadata(): array; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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\InvalidArgumentException; | ||
|
||
/** | ||
* Interface for invalidating cached items using tags. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
interface TagAwareCacheInterface extends CacheInterface | ||
{ | ||
/** | ||
* Invalidates cached items using tags. | ||
* | ||
* When implemented on a PSR-6 pool, invalidation should not apply | ||
* to deferred items. Instead, they should be committed as usual. | ||
* This allows replacing old tagged values by new ones without | ||
* race conditions. | ||
* | ||
* @param string[] $tags An array of tags to invalidate | ||
* | ||
* @return bool True on success | ||
* | ||
* @throws InvalidArgumentException When $tags is not valid | ||
*/ | ||
public function invalidateTags(array $tags); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
that's the declaration for the return type of the callable