Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
frkly opened this issue Sep 20, 2024 · 0 comments

Comments

@frkly
Copy link

frkly commented Sep 20, 2024

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

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

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 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant