Skip to content

[Cache] ChainAdapter accepts CacheItemPoolInterface, so it should work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface #60160

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

Closed
PatchRanger opened this issue Apr 7, 2025 · 1 comment

Comments

@PatchRanger
Copy link
Contributor

Symfony version(s) affected

7.*

Description

ChainAdapter accepts list of CacheItemPoolInterface.
It makes me assuming that I can use it together with either AdapterInterface or any external class implementing CacheItemPoolInterface.
If I got it correct, I can do such thing in my DI as CacheItemPoolInterface:

new ChainAdapter(
  adapters: [
    new \Symfony\Component\Cache\Adapter\ArrayAdapter(storeSerialized: false),
    YiiHelper::get(\Yii2Extended\Yii2Cache\Psr6ToYii2Cache::class), // it implements CacheItemPoolInterface
  ],
);

But I can't. It leads to the TypeError (see below in Additonal context).

How to reproduce

I've created a minimal script to reproduce. it's attached.
reproducer.php.txt

Here's the text of the script, if that's more convenient.

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Symfony\Component\Cache\Adapter\ChainAdapter;
use Yii2Extended\Yii2Cache\Psr6ToYii2Cache;

$chainAdapter = new ChainAdapter(
    adapters: [
        new \Symfony\Component\Cache\Adapter\ArrayAdapter(storeSerialized: false),
        new Psr6ToYii2Cache(new \yii\caching\ArrayCache()),
    ],
);

$cacheKey = 'foo';
$item = $chainAdapter->getItem($cacheKey);
if ($item->isHit()) {
    echo 'isHit', "\n";
    echo $item->get(), "\n";
}
$item->set('bar');

echo 'before save', "\n";
$chainAdapter->save($item);
echo 'after save', "\n";

In place of the Psr6ToYii2Cache can be any external (i.e., not implementing the \Symfony\Component\Cache\Adapter\AdapterInterface) class that implements the CacheItemPoolInterface.

Possible Solution

I've fixed it locally by such fix:
vendor/symfony/cache/CacheItem.php

- protected ?ItemInterface $innerItem = null;
+ protected ?\Psr\Cache\CacheItemInterface $innerItem = null;

In my view it's adequate: as long as ChainAdapter accepts CacheItemPoolInterface, it's normal that innerItem should be of CacheItemInterface.

Additional Context

reproducer.php without fix leads to such error message:

PHP Fatal error: Uncaught TypeError: Cannot assign Yii2Extended\Yii2Cache\Psr6ToYii2CacheItem to property Symfony\Component\Cache\CacheItem::$innerItem of type ?Symfony\Contracts\Cache\ItemInterface in /code/vendor/symfony/cache/Adapter/ProxyAdapter.php:60

@PatchRanger PatchRanger added the Bug label Apr 7, 2025
@PatchRanger PatchRanger changed the title ChainAdapter accepts CacheItemPoolInterface, so it should work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface [Cache] ChainAdapter accepts CacheItemPoolInterface, so it should work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface Apr 7, 2025
@xabbuh xabbuh added the Cache label Apr 7, 2025
@derrabus
Copy link
Member

derrabus commented Apr 7, 2025

Sounds legit, please send a PR.

PatchRanger pushed a commit to PatchRanger/symfony that referenced this issue Apr 7, 2025
… should work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface
PatchRanger pushed a commit to PatchRanger/symfony that referenced this issue Apr 7, 2025
… should work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface
nicolas-grekas added a commit that referenced this issue Apr 7, 2025
… Danilson)

This PR was merged into the 6.4 branch.

Discussion
----------

[Cache] Fix proxying third party PSR-6 cache items

...work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #60160
| License       | MIT

Commits
-------

74debe4 Fix #60160: ChainAdapter accepts CacheItemPoolInterface, so it should work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface
alexandre-daubois added a commit that referenced this issue Apr 8, 2025
* 6.4:
  Add tests
  bug #60121[Cache] ArrayAdapter serialization exception clean $expiries
  Fix #60160: ChainAdapter accepts CacheItemPoolInterface, so it should work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface
alexandre-daubois added a commit that referenced this issue Apr 8, 2025
* 7.2:
  Add tests
  bug #60121[Cache] ArrayAdapter serialization exception clean $expiries
  Fix #60160: ChainAdapter accepts CacheItemPoolInterface, so it should work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants