Skip to content
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

Psr16Adapter fails to cache any item if a namespace is used #32019

Closed

Conversation

moufmouf
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
License MIT

The Psr16Adapter extends AdapterTestCase.
When adding a namespace, the AdapterTestCase adds ":" after the namespace:

https://github.com/symfony/symfony/blob/v4.3.1/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php#L37

The namespace is prepended to the cache key.
But in PSR-16, the ":" is a forbidden character.

As a result, the cache key is invalid and cache is not persisted. If you use Psr16Adapter + a namespace, the cache simply does not work.

The first commit of this PR starts with an additional test and no fix (to showcase the problem).

@moufmouf moufmouf force-pushed the broken_psr16adapter_with_namespace branch from 635206e to 500fba8 Compare June 13, 2019 08:12
@moufmouf moufmouf changed the title Psr16Adapter fails to cache any item if a namespace is used [WIP] Psr16Adapter fails to cache any item if a namespace is used Jun 13, 2019
@moufmouf moufmouf force-pushed the broken_psr16adapter_with_namespace branch from b8158ea to 02fd552 Compare June 13, 2019 09:23
@moufmouf moufmouf changed the title [WIP] Psr16Adapter fails to cache any item if a namespace is used Psr16Adapter fails to cache any item if a namespace is used Jun 13, 2019
@nicolas-grekas
Copy link
Member

Wow, thanks for spotting. The bug exists on 3.4, in SimpleCacheAdapter.
The fix is wrong: the currently attached patch changes AbstractAdapter which is used by all adapters.
Only Psr16Adapter/SimpleCacheAdapter is affected, so that's where the fix should go.
It also doesn't work when versioning is enabled since versioning adds another :.
Instead, we could maybe make the : an @internal NS_SEPARATOR const (because PHP5.5 on 3.4 doesn't allow protected consts) that we reference in AbstractAdapter using static::NS_SEPARATOR?

@stof
Copy link
Member

stof commented Jun 13, 2019

Why would we need to use different separators per implementation, rather than using a separator accepted by all ?

@nicolas-grekas
Copy link
Member

Two reasons, on a very different scale:

  • changing the separator means invalidating all existing caches, better not when it's not needed
  • using a separator that is forbidden makes it 100% sure that no collision is possible, that's desired.

@moufmouf
Copy link
Contributor Author

Ok, opened a new PR #32025 that targets Symfony 3.4.

Shall I close this one or continue working on it too?

@nicolas-grekas
Copy link
Member

Nope, no need, thanks.

fabpot added a commit that referenced this pull request Jun 14, 2019
…is used (moufmouf)

This PR was squashed before being merged into the 3.4 branch (closes #32025).

Discussion
----------

SimpleCacheAdapter fails to cache any item if a namespace is used

| Q             | A
| ------------- | ---
| Branch?       |3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

This is a backport of #32019

The SimpleCacheAdapter extends AdapterTestCase.
When adding a namespace, the AdapterTestCase adds ":" after the namespace:

https://github.com/symfony/symfony/blob/v4.3.1/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php#L37

The namespace is prepended to the cache key.
But in PSR-16, the ":" is a forbidden character.

As a result, the cache key is invalid and cache is not persisted. If you use Psr16Adapter + a namespace, the cache simply does not work.

As per @nicolas-grekas advices, a NS_SEPARATOR const is added to change the namespace separator for the `SimpleCacheAdapter` to "_" (that is compatible with PSR-16).

The first commit of this PR starts with an additional test and no fix (to showcase the problem).

Commits
-------

ffd3469 SimpleCacheAdapter fails to cache any item if a namespace is used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants