SessionCacheContext class should implement CacheContextInterface

Created on 11 October 2017, over 6 years ago
Updated 30 April 2023, about 1 year ago

Problem/Motivation

This is a follow-up to #2881348: SessionCacheContext calls getId() on null β†’ .

@larowlan commented (#9),

Also I note that SessionCacheContext is used as a cache context, but doesn't implement \Drupal\Core\Cache\Context\CacheContextInterface - perhaps it should, otherwise it's missing methods might cause other fatals namely getCacheableMetadata. Perhaps that is a different issue though.

Later, @alexpott said (#44),

I think the addition of the interface should go in a separate issue. Afaics from the code because the ID does not contain a "." or ":" we'll never call getCacheableMetadata(). If this change doesn't have the interface addition then it is a bugfix that can get resolved in 8.4.x as well as 8.5.x. Which is good because it is fixing a problem that people are facing.

Proposed resolution

Declare the interface and implement the getCacheableMetadata() method.

Compare with patches already on the original issue, such as 2881348-39.patch β†’ .

Remaining tasks

User interface changes

None

API changes

Code can rely on SessionCacheContext to implement CacheContextInterface.

Data model changes

None

πŸ“Œ Task
Status

Needs work

Version

10.1 ✨

Component
CacheΒ  β†’

Last updated about 18 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States benjifisher Boston area

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Found this following πŸ“Œ Enable service autoconfiguration for core event subscribers Fixed

    If we enable autoconfiguration, that means we can automatically add the cache.context tag to services that implement a specific interface. But as I found over there, not all cache contexts actually implement CacheContextInterface.

  • last update about 1 year ago
    29,367 pass
  • πŸ‡«πŸ‡·France andypost

    I think we should start throw deprecation if cache contexts does not implement context interface

    +++ b/core/tests/Drupal/KernelTests/Core/Cache/CacheCollectorTest.php
    @@ -60,4 +62,15 @@ public function providerTestInvalidCharacters() {
    +   * Tests that cache contexts implement the right interfaces.
    ...
    +  public function testContextImplementsInterface() {
    +    $cache_contexts = \Drupal::service('service_container')->findTaggedServiceIds('cache.context');
    +    foreach (array_keys($cache_contexts) as $context) {
    +      $service = \Drupal::service($context);
    +      $this->assertTrue($service instanceof CalculatedCacheContextInterface || $service instanceof CacheContextInterface);
    

    This test still makes sense but needs to throw deprecation if collector discovers cache context without interface

  • πŸ‡«πŸ‡·France andypost
    +++ b/core/lib/Drupal/Core/Cache/Context/RequestStackCacheContextBase.php
    @@ -11,7 +12,7 @@
    -abstract class RequestStackCacheContextBase {
    +abstract class RequestStackCacheContextBase implements CacheContextInterface {
    

    The change in #18 is BC break so collector should care about it

    Looking at usage of CalculatedCacheContextInterface there's only one check for interface in \Drupal\Core\Cache\Context\CacheContextsManager::getLabels()

Production build 0.69.0 2024