From 0a1220fd9699b9bf90dbb329ff32cfcd7421e8f8 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 1 Aug 2018 10:02:13 +0200 Subject: [PATCH] [Cache] leverage Contracts\Cache --- composer.json | 5 +- .../Resources/config/cache.xml | 3 +- .../Cache/Adapter/AbstractAdapter.php | 2 +- .../Component/Cache/Adapter/ArrayAdapter.php | 2 +- .../Component/Cache/Adapter/ChainAdapter.php | 2 +- .../Component/Cache/Adapter/NullAdapter.php | 2 +- .../Cache/Adapter/PhpArrayAdapter.php | 2 +- .../Component/Cache/Adapter/ProxyAdapter.php | 2 +- .../Cache/Adapter/TagAwareAdapter.php | 4 +- .../Cache/Adapter/TraceableAdapter.php | 2 +- .../Adapter/TraceableTagAwareAdapter.php | 4 +- .../Component/Cache/CacheInterface.php | 37 --------------- src/Symfony/Component/Cache/CacheItem.php | 47 +++++-------------- .../Cache/Exception/LogicException.php | 4 +- .../Component/Cache/Traits/GetTrait.php | 5 ++ src/Symfony/Component/Cache/composer.json | 3 +- .../DependencyInjection/composer.json | 3 +- .../Component/Translation/composer.json | 3 ++ 18 files changed, 43 insertions(+), 89 deletions(-) delete mode 100644 src/Symfony/Component/Cache/CacheInterface.php diff --git a/composer.json b/composer.json index 0e4357ad9ee24..d6be6c5fb448d 100644 --- a/composer.json +++ b/composer.json @@ -114,7 +114,10 @@ "psr/cache-implementation": "1.0", "psr/container-implementation": "1.0", "psr/log-implementation": "1.0", - "psr/simple-cache-implementation": "1.0" + "psr/simple-cache-implementation": "1.0", + "symfony/cache-contracts": "1.0", + "symfony/service-contracts": "1.0", + "symfony/translation-contracts": "1.0" }, "autoload": { "psr-4": { diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml index 533b3780cf15f..e0ada956e75fa 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml @@ -149,6 +149,7 @@ - + + diff --git a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php index 18cfa08e3ac40..c7ee3bcddfb21 100644 --- a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php @@ -15,12 +15,12 @@ use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; -use Symfony\Component\Cache\CacheInterface; 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\GetTrait; +use Symfony\Contracts\Cache\CacheInterface; /** * @author Nicolas Grekas diff --git a/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php b/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php index 92d9d09c3e48d..78960020e8c8e 100644 --- a/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php @@ -13,10 +13,10 @@ use Psr\Cache\CacheItemInterface; use Psr\Log\LoggerAwareInterface; -use Symfony\Component\Cache\CacheInterface; use Symfony\Component\Cache\CacheItem; use Symfony\Component\Cache\ResettableInterface; use Symfony\Component\Cache\Traits\ArrayTrait; +use Symfony\Contracts\Cache\CacheInterface; /** * @author Nicolas Grekas diff --git a/src/Symfony/Component/Cache/Adapter/ChainAdapter.php b/src/Symfony/Component/Cache/Adapter/ChainAdapter.php index 1b0a9011ec9fc..51a778bb69d65 100644 --- a/src/Symfony/Component/Cache/Adapter/ChainAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/ChainAdapter.php @@ -13,12 +13,12 @@ use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; -use Symfony\Component\Cache\CacheInterface; use Symfony\Component\Cache\CacheItem; use Symfony\Component\Cache\Exception\InvalidArgumentException; use Symfony\Component\Cache\PruneableInterface; use Symfony\Component\Cache\ResettableInterface; use Symfony\Component\Cache\Traits\GetTrait; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Service\ResetInterface; /** diff --git a/src/Symfony/Component/Cache/Adapter/NullAdapter.php b/src/Symfony/Component/Cache/Adapter/NullAdapter.php index d4e7598f330fd..38e1133675d52 100644 --- a/src/Symfony/Component/Cache/Adapter/NullAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/NullAdapter.php @@ -12,8 +12,8 @@ namespace Symfony\Component\Cache\Adapter; use Psr\Cache\CacheItemInterface; -use Symfony\Component\Cache\CacheInterface; use Symfony\Component\Cache\CacheItem; +use Symfony\Contracts\Cache\CacheInterface; /** * @author Titouan Galopin diff --git a/src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php b/src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php index 8bd135d84d085..1a11e69f05b5b 100644 --- a/src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php @@ -13,13 +13,13 @@ use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; -use Symfony\Component\Cache\CacheInterface; use Symfony\Component\Cache\CacheItem; 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\PhpArrayTrait; +use Symfony\Contracts\Cache\CacheInterface; /** * Caches items at warm up time using a PHP array that is stored in shared memory by OPCache since PHP 7.0. diff --git a/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php b/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php index ff86f51805e8d..32d9f90adca7f 100644 --- a/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php @@ -13,12 +13,12 @@ use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; -use Symfony\Component\Cache\CacheInterface; 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\ProxyTrait; +use Symfony\Contracts\Cache\CacheInterface; /** * @author Nicolas Grekas diff --git a/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php index 5dccb345ab5f6..b527a635ceadc 100644 --- a/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php @@ -13,17 +13,17 @@ use Psr\Cache\CacheItemInterface; use Psr\Cache\InvalidArgumentException; -use Symfony\Component\Cache\CacheInterface; 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\ProxyTrait; +use Symfony\Contracts\Cache\TagAwareCacheInterface; /** * @author Nicolas Grekas */ -class TagAwareAdapter implements CacheInterface, TagAwareAdapterInterface, PruneableInterface, ResettableInterface +class TagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface { const TAGS_PREFIX = "\0tags\0"; diff --git a/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php b/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php index 2bd10b5ca5676..3b97294475178 100644 --- a/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php @@ -12,10 +12,10 @@ namespace Symfony\Component\Cache\Adapter; use Psr\Cache\CacheItemInterface; -use Symfony\Component\Cache\CacheInterface; use Symfony\Component\Cache\CacheItem; use Symfony\Component\Cache\PruneableInterface; use Symfony\Component\Cache\ResettableInterface; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Service\ResetInterface; /** diff --git a/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php index c597c81c386ea..69461b8b6bb32 100644 --- a/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php @@ -11,12 +11,12 @@ namespace Symfony\Component\Cache\Adapter; -use Symfony\Component\Cache\CacheInterface; +use Symfony\Contracts\Cache\TagAwareCacheInterface; /** * @author Robin Chalas */ -class TraceableTagAwareAdapter extends TraceableAdapter implements CacheInterface, TagAwareAdapterInterface +class TraceableTagAwareAdapter extends TraceableAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface { public function __construct(TagAwareAdapterInterface $pool) { diff --git a/src/Symfony/Component/Cache/CacheInterface.php b/src/Symfony/Component/Cache/CacheInterface.php deleted file mode 100644 index 7a149d71a9268..0000000000000 --- a/src/Symfony/Component/Cache/CacheInterface.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Cache; - -/** - * Gets and stores items from a cache. - * - * On cache misses, a callback is called that should return the missing value. - * It is given two arguments: - * - the missing cache key - * - the corresponding PSR-6 CacheItemInterface object, - * allowing time-based expiration control. - * - * @author Nicolas Grekas - */ -interface CacheInterface -{ - /** - * @param callable(CacheItem):mixed $callback Should return the computed value for the given key/item - * @param float|null $beta A float that 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. - * - * @return mixed The value corresponding to the provided key - */ - public function get(string $key, callable $callback, float $beta = null); -} diff --git a/src/Symfony/Component/Cache/CacheItem.php b/src/Symfony/Component/Cache/CacheItem.php index 4ded2227c2a24..e0500756a4063 100644 --- a/src/Symfony/Component/Cache/CacheItem.php +++ b/src/Symfony/Component/Cache/CacheItem.php @@ -11,31 +11,16 @@ namespace Symfony\Component\Cache; -use Psr\Cache\CacheItemInterface; use Psr\Log\LoggerInterface; use Symfony\Component\Cache\Exception\InvalidArgumentException; use Symfony\Component\Cache\Exception\LogicException; +use Symfony\Contracts\Cache\ItemInterface; /** * @author Nicolas Grekas */ -final class CacheItem implements CacheItemInterface +final class CacheItem implements ItemInterface { - /** - * 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'; - private const METADATA_EXPIRY_OFFSET = 1527506807; protected $key; @@ -118,15 +103,9 @@ public function expiresAfter($time) } /** - * Adds a tag to a cache item. - * - * @param string|string[] $tags A tag or array of tags - * - * @return static - * - * @throws InvalidArgumentException When $tag is not valid + * {@inheritdoc} */ - public function tag($tags) + public function tag($tags): ItemInterface { if (!$this->isTaggable) { throw new LogicException(sprintf('Cache item "%s" comes from a non tag-aware pool: you cannot tag it.', $this->key)); @@ -153,6 +132,14 @@ public function tag($tags) return $this; } + /** + * {@inheritdoc} + */ + public function getMetadata(): array + { + return $this->metadata; + } + /** * Returns the list of tags bound to the value coming from the pool storage if any. * @@ -167,16 +154,6 @@ public function getPreviousTags() return $this->metadata[self::METADATA_TAGS] ?? array(); } - /** - * Returns a list of metadata info that were saved alongside with the cached value. - * - * See public CacheItem::METADATA_* consts for keys potentially found in the returned array. - */ - public function getMetadata(): array - { - return $this->metadata; - } - /** * Validates a cache key according to PSR-6. * diff --git a/src/Symfony/Component/Cache/Exception/LogicException.php b/src/Symfony/Component/Cache/Exception/LogicException.php index 042f73e6a5040..d299673eb2246 100644 --- a/src/Symfony/Component/Cache/Exception/LogicException.php +++ b/src/Symfony/Component/Cache/Exception/LogicException.php @@ -11,8 +11,8 @@ namespace Symfony\Component\Cache\Exception; -use Psr\Cache\InvalidArgumentException as Psr6CacheInterface; -use Psr\SimpleCache\InvalidArgumentException as SimpleCacheInterface; +use Psr\Cache\CacheException as Psr6CacheInterface; +use Psr\SimpleCache\CacheException as SimpleCacheInterface; class LogicException extends \LogicException implements Psr6CacheInterface, SimpleCacheInterface { diff --git a/src/Symfony/Component/Cache/Traits/GetTrait.php b/src/Symfony/Component/Cache/Traits/GetTrait.php index a870881300b10..d55f3ae0dfb08 100644 --- a/src/Symfony/Component/Cache/Traits/GetTrait.php +++ b/src/Symfony/Component/Cache/Traits/GetTrait.php @@ -13,6 +13,7 @@ use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Cache\CacheItem; +use Symfony\Component\Cache\Exception\InvalidArgumentException; use Symfony\Component\Cache\LockRegistry; /** @@ -31,6 +32,10 @@ trait GetTrait */ 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); } diff --git a/src/Symfony/Component/Cache/composer.json b/src/Symfony/Component/Cache/composer.json index ab642debc7a17..bf3532559cbb2 100644 --- a/src/Symfony/Component/Cache/composer.json +++ b/src/Symfony/Component/Cache/composer.json @@ -17,7 +17,8 @@ ], "provide": { "psr/cache-implementation": "1.0", - "psr/simple-cache-implementation": "1.0" + "psr/simple-cache-implementation": "1.0", + "symfony/cache-contracts": "1.0" }, "require": { "php": "^7.1.3", diff --git a/src/Symfony/Component/DependencyInjection/composer.json b/src/Symfony/Component/DependencyInjection/composer.json index ec48d8ac55fdd..0f8e7227855be 100644 --- a/src/Symfony/Component/DependencyInjection/composer.json +++ b/src/Symfony/Component/DependencyInjection/composer.json @@ -39,7 +39,8 @@ "symfony/yaml": "<3.4" }, "provide": { - "psr/container-implementation": "1.0" + "psr/container-implementation": "1.0", + "symfony/service-contracts": "1.0" }, "autoload": { "psr-4": { "Symfony\\Component\\DependencyInjection\\": "" }, diff --git a/src/Symfony/Component/Translation/composer.json b/src/Symfony/Component/Translation/composer.json index 56b97725230ce..50b90ce06ae02 100644 --- a/src/Symfony/Component/Translation/composer.json +++ b/src/Symfony/Component/Translation/composer.json @@ -34,6 +34,9 @@ "symfony/dependency-injection": "<3.4", "symfony/yaml": "<3.4" }, + "provide": { + "symfony/translation-contracts": "1.0" + }, "suggest": { "symfony/config": "", "symfony/yaml": "",