Needs review
Project:
Drupal core
Version:
main
Component:
cache system
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
25 Oct 2024 at 08:46 UTC
Updated:
15 Oct 2026 at 20:16 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
chi commentedComment #5
chi commentedI actually think all cache backends should use
currentTime.Comment #6
smustgrave commentedAppears to have test failures. Probably would need test coverage to show the issue?
Comment #8
marcusml commentedTests should pass now. 🤞
We've been using this patch for a while now in order to fix issues with cache invalidation on long running jobs .
Comment #9
berdirThe child class \Drupal\Core\Cache\MemoryCache\MemoryCache should probably also be considered as it has the same check.
FWIW, this might come with a non-trivial cost, getting the current time is more expensive than the request time, that's why we default to request time.
Also, per #3047289: Standardize how we implement in-memory caches, MemoryBackend isn't really a good backend for in-memory caching as it is designed to be a drop-in replacement for real backends that has the same serialize behavior.
Comment #11
parth_sinha commentedMy apologies for the initial confusion - I mistakenly pushed to the 11.x.c branch instead of 11.x due to a typo.
Added unit tests to verify the fix works correctly.
Files added:
- core/tests/Drupal/Tests/Core/Cache/MemoryBackendExpirationTest.php
- core/tests/Drupal/Tests/Core/Cache/MemoryCacheExpirationTest.php
Test coverage includes:
- testCacheExpirationWithCurrentTime(): Verifies cache items properly expire after their TTL passes, using mocked time progression (1000 → 1005 seconds) to simulate elapsed time in long-running processes
- testPermanentCacheDoesNotExpire(): Confirms permanent cache entries never expire regardless of time, even with `PHP_INT_MAX` timestamp
Both tests use mocked
TimeInterfaceto control time progression without relying onsleep(), ensuring fast and reliable test execution.All tests pass with the
getCurrentTime()fix applied.Comment #15
kksandr commentedI’ve updated the merge request to be compatible with the current core.
Some notes on the changes:
I introduced
\Drupal\Core\Cache\CacheTimeInterfacewith a singlegetTime(): intmethod. Cache backends that don’t use the request time for expiration (e.g.MemoryBackendandMemoryCache, which now rely on the current time) implement it, so that consumers can find out which time the backend actually uses.This is needed because some code assumes that all cache backends use the request time. For example,
\Drupal\Core\Cache\VariationCacheduplicated the expiration logic and relied on that assumption, which is no longer true. It now asks the backend for its time instead.