Skip to content

Commit

Permalink
feature #29236 [Cache] deprecate all PSR-16 adapters, provide Psr16Ca…
Browse files Browse the repository at this point in the history
…che instead (nicolas-grekas)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Cache] deprecate all PSR-16 adapters, provide Psr16Cache instead

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

As discussed in #28918, PSR-16 implementations are now mostly useless: either PSR-6 or contracts' CacheInterface is always a better fit.

Let's deprecate them all but keep only a `Psr16Cache` to turn any PSR-6 implementation to a PSR-16 one.

From the changelog:
  * removed `psr/simple-cache` dependency, run `composer require psr/simple-cache` if you need it
  * deprecated all PSR-16 adapters, use `Psr16Cache` or `Symfony\Contracts\Cache\CacheInterface` implementations instead
 * deprecated `SimpleCacheAdapter`, use `Psr16Adapter` instead
 * deprecated the "Psr\SimpleCache\CacheInterface" / "cache.app.simple" service, use "Symfony\Contracts\Cache\CacheInterface" / "cache.app" instead

Commits
-------

5006be6 [Cache] deprecate all PSR-16 adapters, provide Psr16Cache instead
  • Loading branch information
nicolas-grekas committed Jan 26, 2019
2 parents cafbdb7 + 5006be6 commit 4fbb6e5
Show file tree
Hide file tree
Showing 53 changed files with 788 additions and 385 deletions.
8 changes: 8 additions & 0 deletions UPGRADE-4.3.md
Expand Up @@ -8,6 +8,13 @@ BrowserKit
* Deprecated `Response::buildHeader()`
* Deprecated `Response::getStatus()`, use `Response::getStatusCode()` instead

Cache
-----

* The `psr/simple-cache` dependency has been removed - run `composer require psr/simple-cache` if you need it.
* Deprecated all PSR-16 adapters, use `Psr16Cache` or `Symfony\Contracts\Cache\CacheInterface` implementations instead.
* Deprecated `SimpleCacheAdapter`, use `Psr16Adapter instead.

Config
------

Expand All @@ -18,6 +25,7 @@ FrameworkBundle

* Not passing the project directory to the constructor of the `AssetsInstallCommand` is deprecated. This argument will
be mandatory in 5.0.
* Deprecated the "Psr\SimpleCache\CacheInterface" / "cache.app.simple" service, use "Symfony\Contracts\Cache\CacheInterface" / "cache.app" instead.

HttpFoundation
--------------
Expand Down
3 changes: 3 additions & 0 deletions UPGRADE-5.0.md
Expand Up @@ -13,6 +13,8 @@ Cache
-----

* Removed `CacheItem::getPreviousTags()`, use `CacheItem::getMetadata()` instead.
* Removed all PSR-16 adapters, use `Psr16Cache` or `Symfony\Contracts\Cache\CacheInterface` implementations instead.
* Removed `SimpleCacheAdapter`, use `Psr16Adapter instead.

Config
------
Expand Down Expand Up @@ -163,6 +165,7 @@ FrameworkBundle
* The `Templating\Helper\TranslatorHelper::transChoice()` method has been removed, use the `trans()` one instead with a `%count%` parameter.
* Removed support for legacy translations directories `src/Resources/translations/` and `src/Resources/<BundleName>/translations/`, use `translations/` instead.
* Support for the legacy directory structure in `translation:update` and `debug:translation` commands has been removed.
* Removed the "Psr\SimpleCache\CacheInterface" / "cache.app.simple" service, use "Symfony\Contracts\Cache\CacheInterface" / "cache.app" instead.

HttpFoundation
--------------
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
be mandatory in 5.0.
* Added `ControllerTrait::isFormValid()`
* Added an `help_html` form option to display the `help` text as HTML
* Deprecated the "Psr\SimpleCache\CacheInterface" / "cache.app.simple" service, use "Symfony\Contracts\Cache\CacheInterface" / "cache.app" instead

* [BC Break] When using Messenger, the default transport changed from
using Symfony's serializer service to use `PhpSerializer`, which uses
Expand Down
Expand Up @@ -11,7 +11,8 @@
<tag name="cache.pool" clearer="cache.app_clearer" reset="reset" />
</service>

<service id="cache.app.simple" class="Symfony\Component\Cache\Simple\Psr6Cache">
<service id="cache.app.simple" class="Symfony\Component\Cache\Psr16Cache">
<deprecated>The "Psr\SimpleCache\CacheInterface" / "%service_id%" service is deprecated since Symfony 4.3. Use "Symfony\Contracts\Cache\CacheInterface" / "cache.app" instead.</deprecated>
<argument type="service" id="cache.app" />
</service>

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": "^7.1.3",
"ext-xml": "*",
"symfony/cache": "~4.2",
"symfony/cache": "~4.3",
"symfony/config": "~4.2",
"symfony/contracts": "^1.0.2",
"symfony/dependency-injection": "^4.2",
Expand Down
81 changes: 81 additions & 0 deletions src/Symfony/Component/Cache/Adapter/Psr16Adapter.php
@@ -0,0 +1,81 @@
<?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\Component\Cache\Adapter;

use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\ProxyTrait;

/**
* Turns a PSR-16 cache into a PSR-6 one.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class Psr16Adapter extends AbstractAdapter implements PruneableInterface, ResettableInterface
{
use ProxyTrait;

private $miss;

public function __construct(CacheInterface $pool, string $namespace = '', int $defaultLifetime = 0)
{
parent::__construct($namespace, $defaultLifetime);

$this->pool = $pool;
$this->miss = new \stdClass();
}

/**
* {@inheritdoc}
*/
protected function doFetch(array $ids)
{
foreach ($this->pool->getMultiple($ids, $this->miss) as $key => $value) {
if ($this->miss !== $value) {
yield $key => $value;
}
}
}

/**
* {@inheritdoc}
*/
protected function doHave($id)
{
return $this->pool->has($id);
}

/**
* {@inheritdoc}
*/
protected function doClear($namespace)
{
return $this->pool->clear();
}

/**
* {@inheritdoc}
*/
protected function doDelete(array $ids)
{
return $this->pool->deleteMultiple($ids);
}

/**
* {@inheritdoc}
*/
protected function doSave(array $values, $lifetime)
{
return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime);
}
}
64 changes: 3 additions & 61 deletions src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php
Expand Up @@ -11,69 +11,11 @@

namespace Symfony\Component\Cache\Adapter;

use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\ProxyTrait;
@trigger_error(sprintf('The "%s" class is @deprecated since Symfony 4.3, use "Psr16Adapter" instead.', SimpleCacheAdapter::class), E_USER_DEPRECATED);

/**
* @author Nicolas Grekas <p@tchwork.com>
* @deprecated since Symfony 4.3, use Psr16Adapter instead.
*/
class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface, ResettableInterface
class SimpleCacheAdapter extends Psr16Adapter
{
use ProxyTrait;

private $miss;

public function __construct(CacheInterface $pool, string $namespace = '', int $defaultLifetime = 0)
{
parent::__construct($namespace, $defaultLifetime);

$this->pool = $pool;
$this->miss = new \stdClass();
}

/**
* {@inheritdoc}
*/
protected function doFetch(array $ids)
{
foreach ($this->pool->getMultiple($ids, $this->miss) as $key => $value) {
if ($this->miss !== $value) {
yield $key => $value;
}
}
}

/**
* {@inheritdoc}
*/
protected function doHave($id)
{
return $this->pool->has($id);
}

/**
* {@inheritdoc}
*/
protected function doClear($namespace)
{
return $this->pool->clear();
}

/**
* {@inheritdoc}
*/
protected function doDelete(array $ids)
{
return $this->pool->deleteMultiple($ids);
}

/**
* {@inheritdoc}
*/
protected function doSave(array $values, $lifetime)
{
return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime);
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
@@ -1,6 +1,13 @@
CHANGELOG
=========

4.3.0
-----

* removed `psr/simple-cache` dependency, run `composer require psr/simple-cache` if you need it
* deprecated all PSR-16 adapters, use `Psr16Cache` or `Symfony\Contracts\Cache\CacheInterface` implementations instead
* deprecated `SimpleCacheAdapter`, use `Psr16Adapter` instead

4.2.0
-----

Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/Cache/Exception/CacheException.php
Expand Up @@ -14,6 +14,12 @@
use Psr\Cache\CacheException as Psr6CacheInterface;
use Psr\SimpleCache\CacheException as SimpleCacheInterface;

class CacheException extends \Exception implements Psr6CacheInterface, SimpleCacheInterface
{
if (interface_exists(SimpleCacheInterface::class)) {
class CacheException extends \Exception implements Psr6CacheInterface, SimpleCacheInterface
{
}
} else {
class CacheException extends \Exception implements Psr6CacheInterface
{
}
}
Expand Up @@ -14,6 +14,12 @@
use Psr\Cache\InvalidArgumentException as Psr6CacheInterface;
use Psr\SimpleCache\InvalidArgumentException as SimpleCacheInterface;

class InvalidArgumentException extends \InvalidArgumentException implements Psr6CacheInterface, SimpleCacheInterface
{
if (interface_exists(SimpleCacheInterface::class)) {
class InvalidArgumentException extends \InvalidArgumentException implements Psr6CacheInterface, SimpleCacheInterface
{
}
} else {
class InvalidArgumentException extends \InvalidArgumentException implements Psr6CacheInterface
{
}
}
10 changes: 8 additions & 2 deletions src/Symfony/Component/Cache/Exception/LogicException.php
Expand Up @@ -14,6 +14,12 @@
use Psr\Cache\CacheException as Psr6CacheInterface;
use Psr\SimpleCache\CacheException as SimpleCacheInterface;

class LogicException extends \LogicException implements Psr6CacheInterface, SimpleCacheInterface
{
if (interface_exists(SimpleCacheInterface::class)) {
class LogicException extends \LogicException implements Psr6CacheInterface, SimpleCacheInterface
{
}
} else {
class LogicException extends \LogicException implements Psr6CacheInterface
{
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/LockRegistry.php
Expand Up @@ -45,6 +45,7 @@ class LockRegistry
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PhpArrayAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PhpFilesAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ProxyAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'Psr16Adapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'RedisAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'SimpleCacheAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TagAwareAdapter.php',
Expand Down

0 comments on commit 4fbb6e5

Please sign in to comment.