Make cache()->get() return a classed CacheItem object

Created on 23 August 2012, almost 12 years ago
Updated 30 January 2023, over 1 year ago

Problem/Motivation

We return an stdClass from cache->get(), but there's no enforcement of which keys are in it (id? created? expires? tags?), only $cached->data is in everything since code will immediately break without it. To be able to consistently chain cache tag implementations, we'd need to be able to know the tags of a cache item when it's retrieved, something that's not currently required or supported by cache backends.

We can leave $cached->data as a public property at the moment for bc, then remove it in a follow-up since that will touch a lot of code.

Proposed resolution

  • Return CacheItem on every cache backend in core
  • Wrap cache backends to ensure they return CacheItem always
  • Allow cache backends to opt out of this decoration

Remaining tasks

  • Write a change record
  • Write a Unit test for CacheItemCacheBackendDecorator
  • Maybe extend \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase?

User interface changes

API changes

Data model changes

πŸ“Œ Task
Status

Needs work

Version

10.1 ✨

Component
CacheΒ  β†’

Last updated 1 day ago

Created by

πŸ‡¬πŸ‡§United Kingdom catch

Live updates comments and jobs are added and updated live.
  • API clean-up

    Refactors an existing API or subsystem for consistency, performance, modularization, flexibility, third-party integration, etc. May imply an API change. Frequently used during the Code Slush phase of the release cycle.

  • needs profiling

    It may affect performance, and thus requires in-depth technical reviews and profiling.

  • Needs framework manager review

    It is used to alert the framework manager core committer(s) that an issue significantly impacts (or has the potential to impact) multiple subsystems or represents a significant change or addition in architecture or public APIs, and their signoff is needed (see the governance policy draft for more information). If an issue significantly impacts only one subsystem, use Needs subsystem maintainer review instead, and make sure the issue component is set to the correct subsystem.

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.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • πŸ‡¬πŸ‡§United Kingdom catch

    This could probably be done with public read-only properties now, and if so, that would let us use a value object without actually changing the public API very much at all.

Production build 0.69.0 2024