Add a WeakReference memory cache implementation

Created on 5 January 2021, over 4 years ago
Updated 28 May 2023, about 2 years ago

Problem/Motivation

Core has a MemoryCache implementation, which provides the same API as cache backends but with everything held in PHP memory.

This is already used for the entity static cache after #1596472: Replace hard coded static cache of entities with cache backends .

The long term goal is to solve #2558857: Migrations invalidate entity caches when trying to reclaim memory, should flush 'automatically' - i.e. that core would statically cache things, but that our implementation would prevent memory leaks without intervention. This will solve a lot of issues with processes that need to load lots of entities, with migrate as the main core example #2558857: Migrations invalidate entity caches when trying to reclaim memory, should flush .

Back in 2011, @catch suggested an implementation using weakreferences - at that time only a PECL extension #1237636-7: Entity lazy multiple front loading .

There's an issue open to add a basic LRU cache 📌 Add an in-memory LRU cache Needs work implementation (also from 2011), but that has limitations in that there has to be a hard-coded numeric limit on the entities added, which could still lead to out of memory issues in some situations, or entities being ejected from the cache when there's plenty of memory left.

WeakReferences are now in PHP 7.4: https://www.php.net/manual/en/class.weakreference.php

WeakMap is now in PHP 8: https://www.php.net/manual/en/class.weakmap.php / https://wiki.php.net/rfc/weak_maps (can't find actual PHP 8 docs for it yet). With a PHP 7.4 polyfill https://github.com/BenMorel/weakmap-polyfill

Since Drupal 10 will require PHP 8 or higher, we could add an implementation to core in a 9.x minor release, then Drupal 10 could switch the default for entity caching to it.

Steps to reproduce

Load thousands of entities in one go.

Proposed resolution

Start with a WeakReference implementation for now.

WeakMaps may end up being useful for entity or other caching, but this will require further investigation.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Needs work

Version

11.0 🔥

Component
Cache 

Last updated 12 days ago

Created by

🇬🇧United Kingdom catch

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.

Production build 0.71.5 2024