Drop MemoryCacheInterface as service alias of entity.memory_cache

Created on 16 April 2025, 6 days ago

Problem/Motivation

entity.memory_cache:
  class: Drupal\Core\Cache\MemoryCache\MemoryCache
  arguments: ['@datetime.time']
Drupal\Core\Cache\MemoryCache\MemoryCacheInterface: '@entity.memory_cache'

The service alias Drupal\Core\Cache\MemoryCache\MemoryCacheInterface is incorrectly assigned to the entity memory cache. This is architecturally unsound since MemoryCacheInterface is a generic interface that could be implemented by various memory cache services, not exclusively for entity caching. The entity memory cache is just one specific implementation/use case of the broader memory cache concept.
This creates potential DI issues if other services need to use a different memory cache implementation while still type-hinting against the interface.

Steps to reproduce

Proposed resolution

For the sake of BC introduce the following change in a new minor version of Drupal

  1. Introduce a generic memory cache service
  2. Change the Drupal\Core\Cache\MemoryCache\MemoryCacheInterface service alias to point to that new generic cache
  3. Publish a change record that calls downstream dev's attention that if they used to use the service alias to inject the memory cache and do something with it - like forcibly flush it out to avoid allowed memory exceeded issues - they must change to the entity.memory_cache service id instead

Remaining tasks

  1. Agree with core maintainers if this change can be introduced in a minor version with the above described approach.

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

base system

Created by

🇭🇺Hungary mxr576 Hungary

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

Merge Requests

Comments & Activities

  • Issue created by @mxr576
  • 🇭🇺Hungary mxr576 Hungary

    (Original Slack thread with the problem statement: https://drupal.slack.com/archives/C079NQPQUEN/p1744729430123259)

  • Merge request !11851Introduce generic memory cache service → (Open) created by mxr576
  • Pipeline finished with Success
    6 days ago
    Total: 335s
    #474808
  • 🇭🇺Hungary mxr576 Hungary
  • 🇭🇺Hungary mxr576 Hungary
  • 🇭🇺Hungary mxr576 Hungary
  • 🇬🇧United Kingdom catch

    Since the entries in the cache only last for one request, it shouldn't really make a difference if the service is 'different' - e.g. it's not like if we swapped a persistent cache service around. So I can't think of too much that would go wrong in the issue summary except perhaps someone using autowire in their runtime code, but using the entity.memory_cache service name in a test. However even this would only cause a test failure and not a runtime issue.

    In general, code that wants to use a memory cache should create its own service and inject it anyway, rather than chucking everything in the same place.

    The one thing I'm wondering about though is whether we should also deprecate the new service - then we could tell people not to use it an define their own memory cache service instead. If this is controversial it'd be easy to split that to a follow-up for more discussion though.

Production build 0.71.5 2024