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

Use wp_cache_flush_runtime() when available #2915

Merged
merged 8 commits into from
Aug 25, 2022
Merged

Conversation

tillkruss
Copy link
Contributor

Description of the Change

The wp_cache_flush_runtime() was added in WordPress 6.0 and is safer to call than trying to unset the $cache property.

Changelog Entry

Added - Call wp_cache_flush_runtime() when available

Credits

Props @tillkruss

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@tillkruss
Copy link
Contributor Author

See also #2916.

@cmcandrew
Copy link

If this could be treated with some urgency, we have been unable to upgrade beyond ElasticPress 3.6.6

Since 4.x with Object Cache Pro plugin exhibits run-away Memory Usage whilst indexing

@felipeelia
Copy link
Member

The problem with wp_cache_flush_runtime() as it is right now is that for sites using Redis, for example, it will end up flushing everything that is in the cache, which may cause an undesired side-effect.

One thing we could do is to add a new action at the bottom of the stop_the_insanity() method, let's say ep_sync_stop_the_insanity or similar and add that wp_cache_flush_runtime() call in the other plugin.

@tillkruss is that a change you would like to make? Thanks in advance!

@felipeelia felipeelia added this to the 4.3.0 milestone Aug 3, 2022
@tillkruss
Copy link
Contributor Author

tillkruss commented Aug 3, 2022

The problem with wp_cache_flush_runtime() as it is right now is that for sites using Redis, for example, it will end up flushing everything that is in the cache, which may cause an undesired side-effect.

That's not correct. Setting $cache to an empty array and calling wp_cache_flush_runtime() is the exact same action.

For your example, Redis data is not touched.

One thing we could do is to add a new action at the bottom of the stop_the_insanity() method, let's say ep_sync_stop_the_insanity or similar and add that wp_cache_flush_runtime() call in the other plugin.

No, it's up to this plugin to manage the object cache. Redis/Memcached plugins only supply the API to communicate with the external object cache.

Added in WordPress 6.0
@tillkruss
Copy link
Contributor Author

@felipeelia: For more context, the Action Scheduler plugin had a similar issue: woocommerce/action-scheduler#790

And this is how they solved it using wp_cache_flush_runtime(): woocommerce/action-scheduler#827

@tillkruss tillkruss changed the title Call wp_cache_flush_runtime() Use wp_cache_flush_runtime() when available Aug 3, 2022
@felipeelia
Copy link
Member

Hey @tillkruss,

Thanks for the quick reply. I didn't notice that Redis Object Cache would overwrite the wp_cache_flush_runtime() implementation, so we should be good here.

You made me curious about what you meant with it's up to this plugin to manage the object cache. Do you mean ElasticPress should manage object cache?

I still think it is a good idea to have an action there just to give people the chance to do any extra steps they want but we can take care of that in another PR.

@tillkruss
Copy link
Contributor Author

You made me curious about what you meant with it's up to this plugin to manage the object cache. Do you mean ElasticPress should manage object cache?

Sorry, I kept it brief due to being on my phone.

I meant that object caching plugins shouldn't hook into 1000s of actions of other plugins, but rather that each individual plugin should manage its own cache data itself with calls like wp_cache_flush_runtime() and wp_cache_flush_group(). That way me and other authors don't need to keep track of changes in other plugins.

ElasticPress is a bit special (like Action Scheduler), since you're running long tasks.

I still think it is a good idea to have an action there just to give people the chance to do any extra steps they want but we can take care of that in another PR.

That makes sense, I'll add an action to the PR.

@felipeelia
Copy link
Member

That all makes sense @tillkruss, thanks. If you are going to add the action, can you please add @since 4.3.0 to its docblock?

@tillkruss
Copy link
Contributor Author

That all makes sense @tillkruss, thanks. If you are going to add the action, can you please add @since 4.3.0 to its docblock?

Added. Feel free to rename of course.

@felipeelia felipeelia self-assigned this Aug 23, 2022
@felipeelia felipeelia merged commit 9099e3e into 10up:develop Aug 25, 2022
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

Successfully merging this pull request may close these issues.

3 participants