Memory cache does not invalidate by cache tags

Created on 12 September 2024, 10 months ago

Problem/Motivation

Default memory implementation Drupal\Core\Cache\MemoryBackend does not invalidate cache tags.
Current implementation of cache invalidation is solely checking for expire time:

<?php
  protected function prepareItem($cache, $allow_invalid) {
    if (!isset($cache->data)) {
      return FALSE;
    }
    // The object passed into this function is the one stored in $this->cache.
    // We must clone it as part of the preparation step so that the actual
    // cache object is not affected by the unserialize() call or other
    // manipulations of the returned object.

    $prepared = clone $cache;
    $prepared->data = unserialize($prepared->data);

    // Check expire time.
    $prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= $this->getRequestTime();

    if (!$allow_invalid && !$prepared->valid) {
      return FALSE;
    }

    return $prepared;
  }
?>

This is very bad for concurrent requests happening, making it possible that the memory cache returns invalid data although not requested when calling get method with $allow_invalid = FALSE (which is the default).
The problem gets more and more severe for example for processes running a couple of seconds and long running processes (such as cron and batch operations).

Steps to reproduce

TBD

Proposed resolution

Memory cache needs to make sure it's not returning invalid data by checking whether the cache tags are diffferent.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Cache 

Last updated 12 days ago

Created by

🇩🇪Germany mxh Offenburg

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

Comments & Activities

Production build 0.71.5 2024