keycdn / cache-enabler

A lightweight caching plugin for WordPress that makes your website faster by generating static HTML files.
https://wordpress.org/plugins/cache-enabler/
123 stars 46 forks source link

Hook cache_enabler_complete_cache_cleared isn't being called after cache_enabler_clear_complete_cache #172

Closed janvitos closed 3 years ago

janvitos commented 3 years ago

Hi,

So I used to use the ce_action_cache_cleared hook to call stuff once the complete had been cleared with ce_clear_cache. I noticed those have been replaced with cache_enabler_complete_cache_cleared and cache_enabler_clear_complete_cache respectively in version 1.6.0. Calling cache_enabler_clear_complete_cache does clear the entire cache, but code I have written that is normally called with cache_enabler_complete_cache_cleared simply does not get called.

Thanks for your help.

janvitos commented 3 years ago

So I got my code to be called by using the cache_enabler_site_cache_cleared hook instead. I figured this out by looking at the code and noticing clear_complete_cache calls clear_site_cache.

I think it should be mentioned somewhere in the docs that cache_enabler_complete_cache_cleared has no effect anymore and needs to be replaced with cache_enabler_site_cache_cleared, unless that isn't really the intended behavior and there is a bug.

coreykn commented 3 years ago

Docs are being updated with examples. 🙂 cache_enabler_clear_complete_cache clears the complete cache as it replaces the now deprecated ce_clear_cache:

do_action( 'cache_enabler_clear_complete_cache' );
do_action( 'ce_clear_cache' ); // deprecated in 1.6.0

If a single site it's everything. If a multisite it's the entire network. cache_enabler_complete_cache_cleared will be fired as soon as the complete cache is cleared (when the wp-content/cache/cache-enabler directory is deleted). If local environment make sure there are no hidden files that may cause the empty directory to not be deleted.

cache_enabler_site_cache_cleared will fire whenever a site cache is cleared (when the wp-content/cache/cache-enabler/www.example.com or wp-content/cache/cache-enabler/www.example.com/site2 is deleted). If a single site this will be every time the complete cache is cleared. If a multisite it will fire for each site cleared. If only one site is cleared the hook will be fired for that site. If the complete cache is cleared it will be fired for each site that is cleared. These two hooks are different and provide a flexible way to integrate with Cache Enabler, but if there are suggestions on how to improve this that's always welcome and appreciated.

It's important to know that the cache cleared hooks will now only be fired if there was actually cache to be cleared and not each time any of the public clear cache hooks are fired.

janvitos commented 3 years ago

Hi,

Thanks for the detailed clarification.

Since I have a single site, then I understand I now have to use the cache_enabler_site_cache_cleared hook when my site's complete cache is cleared.

This is what I have implemented and it works like a charm.

Thanks for your continued support!

coreykn commented 3 years ago

You're welcome! I apologize if that wasn't clear above, but both cache_enabler_complete_cache_cleared and cache_enabler_site_cache_cleared are fired for a single site installation.

janvitos commented 3 years ago

Hi @coreykn, cache_enabler_complete_cache_cleared never fired on my single site installation. That's why I had to use cache_enabler_site_cache_cleared instead.

The hook I'm using to clear the complete cache is cache_enabler_clear_complete_cache.

coreykn commented 3 years ago

All of my tests on a single site installation fire the cache_enabler_complete_cache_cleared hook when the complete cache is cleared. It won't fire if the cache was already empty, which may have been what you experienced before. This was done to ensure cache cleared hooks are only fired if cached pages were actually cleared.

janvitos commented 3 years ago

That's really strange @coreykn. Cache was definitely NOT empty.

I will do more tests and see how it goes. But I already tested this multiple times and could NOT get cache_enabler_complete_cache_cleared to fire after calling cache_enabler_clear_complete_cache. Only cache_enabler_site_cache_cleared would fire.