Automattic / Automattic/wp-memcached

Support wp_suspend_cache_addition() in object-cache.php to prevent "Allowed memory size exhausted" errors from excessive local cache accumulation

Open
#167 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
172
Forks
52
PR merge metrics
No merged PRs in 30d

Description

## Summary:
The `object-cache.php` in the Memcached Object Cache does not currently support the `wp_suspend_cache_addition()` function, unlike the core WordPress object cache implementation in `class-wp-object-cache.php`. This can lead to memory exhaustion in certain scenarios due to uncontrolled local caching (`$wp_object_cache->cache`).

[wp_suspend_cache_addition() - WordPress Developer Resources](https://developer.wordpress.org/reference/functions/wp_suspend_cache_addition/)

I propose implementing this function in `object-cache.php` to align its functionality with the core object cache class to offer more flexibility in utilizing limited memory effectively.

## Context:
In the WordPress core object cache implementation (`class-wp-object-cache.php`), the method `add()` includes a check for `wp_suspend_cache_addition()`. This allows developers to prevent local caching under certain conditions.

[WordPress/wp-includes/class-wp-object-cache.php - GitHub](https://github.com/WordPress/WordPress/blob/345ff42a6a1a26d94ff5e06d209abab5dcb3fa57/wp-includes/class-wp-object-cache.php#L199)

```
public function add( $key, $data, $group = 'default', $expire = 0 ) {
if ( wp_suspend_cache_addition() ) {
return false;
}
// Rest of the code...
}
```

However, in `object-cache.php`, there is no similar mechanism. While `object-cache.php` has a distinctive feature called `no_mc_groups`, this prevents data from being stored in Memcache but does not prevent it from being stored in the local cache.

## Problems:

- Without the ability to suspend local cache addition, unnecessary accumulation can occur in scenarios like sitemap generation, backups or other large-scale processes involving a massive volume of queries that are not reused.
- Developers lack control over whether data is stored in the local cache, potentially leading to unnecessary memory usage, which can ultimately result in PHP memory exhaustion, triggering a Fatal error and causing processes to halt.

## Proposal:

I propose implementing support for `wp_suspend_cache_addition()` in `object-cache.php`, similar to how it is implemented in the core object cache class. This would give developers the ability to prevent local caching when necessary.

For example, [the add() method in object-cache.php](https://github.com/Automattic/wp-memcached/blob/bb3b9f689dd99df66454b93ece34093556dc37f9/object-cache.php#L217) could be updated as follows:

```
function add( $id, $data, $group = 'default', $expire = 0 ) {
// Check if cache addition is suspended
if ( wp_suspend_cache_addition() ) {
return false;
}

$key = $this->key( $id, $group );

if ( is_object( $data ) ) {
$data = clone $data;
}

if ( in_array( $group, $this->no_mc_groups ) ) {
if ( ! isset( $this->cache[ $key ] ) ) {
$this->cache[ $key ] = [
'value' => $data,
'found' => false,
];
return true;
}
return false;
}

// Rest of the code...
}
```

## Conclusion:
Implementing support for `wp_suspend_cache_addition()` in `object-cache.php` will align it with the WordPress core object cache class and provide a more flexible and memory-efficient caching solution for developers. This will help mitigate issues related to unnecessary memory usage and PHP memory exhaustion, ultimately improving the reliability of resource-heavy processes.

That said, the issue of unnecessary local cache accumulation can be avoided if plugins that provide features like sitemap generation (e.g., Jetpack) choose to bypass cache-heavy functions like `get_posts` or `get_post_meta` and instead directly execute SQL queries using `$wpdb`. However, this is a separate issue that should be addressed at the plugin level. I believe it is still crucial for `object-cache.php` to support `wp_suspend_cache_addition()`, as it would allow developers to have greater control over local cache behavior. It's also worth noting that a simple search for "Allowed memory size exhausted" related to `object-cache.php` brings up numerous unresolved discussions on forums and support channels. This highlights how common and significant this issue is for developers working with resource-heavy WordPress sites.

Thank you for considering this enhancement. I believe that implementing `wp_suspend_cache_addition()` in `object-cache.php` will provide developers with more flexibility and help improve PHP memory management in high-demand scenarios.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in object-cache.php at the add() method and compare its behavior with WordPress's class-wp-object-cache.php implementation linked in the issue. Check the existing no_mc_groups and local cache paths, then verify that suspended cache additions are handled consistently without accumulating local entries.

Written by the indexing model from the issue text.

Assessment

Tech stack
memcached, php, wordpress
Domain
backend
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.