pantheon-systems / wp-redis

WordPress Object Cache using Redis.
https://wordpress.org/plugins/wp-redis/
GNU General Public License v2.0
226 stars 68 forks source link

_should_persist based on more than group, like $data and $key #266

Closed stijnjanmaat closed 4 years ago

stijnjanmaat commented 4 years ago

Hi, thanks for a great simple redis plugin. I have feature request. I would like to have more fine grained control on why something should end up in persistent cache.

I would like to be able to check $data and $key as well. I can imagine it being a filter on the _should_persist method, so an alternative can be supplied. Or add a filter inside the _should_persist method with these extra arguments.

Let me know what you think. I can make a PR.

danielbachhuber commented 4 years ago

Hi @stijnjanmaat,

Thanks for the request.

The existing _should_persist() implementation follows WordPress core's existing wp_cache_add_non_persistent_groups() abstraction, which only supports groups.

I'm not sure adding a filter in this context makes sense, given _should_persist() can be called thousands or tens of thousands of times per request, and a filter could negatively impact performance.

Could you describe your use case in a bit more detail? If you have control over the calling function, you could possibly use an alternative, non-persistent object cache to store the data.

stijnjanmaat commented 4 years ago

Hi @danielbachhuber ,

Thanks for your reply.

True, the method gets called a lot, also in our case.

The use case is as follows: I would like to not store post_meta which contains lots of data in Redis. So I thought I check for particular post_meta keys to whether or not persist. For our use it makes more sense to store this in non-persistent object cache.

I also considered using an alternative non-persistent cache, but I need some way to only use that for some post_meta, so built-in WP functionality. Not much control of the calling function. Couldn't figure it out without a filter or action in the methods in your plugin.

danielbachhuber commented 4 years ago

Makes sense, thanks for clarifying @stijnjanmaat.

In this particular case, I'd recommend using the get_post_metadata filter in WordPress core:

https://github.com/WordPress/wordpress-develop/blob/db4f746b4aa13ce87b065038c8dfea6f7dc9253b/src/wp-includes/meta.php#L513

You can bypass the use of get_post_meta()'s internals and use your own functionality for specific meta keys/values.

Hope this helps.