autoptimize icon indicating copy to clipboard operation
autoptimize copied to clipboard

Proposition regarding flushing cache

Open unnamedfeeling opened this issue 3 years ago • 7 comments

The problem: We use your plugin in a docker environment with load balancer and cdn before multiple containers. Docker containers are underpowered virtual machines. There is a need to recreate cahes every now and then. When the cache is flushed there are situations when machine just cannot generate all the code it needs in time. This leads to buggy code generated and sent to cdn -> site is broken.

The proposition: To fix the issue we do not need to delete current cache instantly. Just offload this process at the point of time when we have a warm cache state. So the process should look somethink like this:

  1. cache invalidation initiated
  2. new temp directory created (eg. cache/autoptimize_temp)
  3. temp directory gets filled with warm cache files (css and js)
  4. current working directory renamed to artifact dir (eg. cache/autoptimize-artifact-*)
  5. temp directory is renamed to working directory (cache/autoptimize)

I would be able to test this mechanism if I had an ability to override cache directory dynamically. How do you think?

unnamedfeeling avatar Nov 05 '21 09:11 unnamedfeeling

That's more or less what is behind the "advanced cache clear" method which can be enabled with this filter;

add_filter( 'autoptimize_filter_cache_clear_advanced', '__return_true' );

futtta avatar Nov 05 '21 10:11 futtta

The difference here is that in case of "advanced cache clear" you just create a new empty directory without warming it up and immediately switch to it. This can lead to broken sites in case of underpowered hosts. But the proposition is to create new directory and prefill it with warmed up cache files and then switch directories.

unnamedfeeling avatar Nov 05 '21 15:11 unnamedfeeling

the warmup (for the homepage) happens immediately after actually :)

futtta avatar Nov 05 '21 15:11 futtta

Ok. Then my problem is as follows: I managed to configure everything to give me 85-92 pagespeed points on desktop with ads enabled. This is acheived via "aggregate inline js" option enabled (and css one too). I have a heavily underpowered docker containers - lets say it is something like 1 core @2.0Ghz with 512Mb memory. My cache resides on a remote machine and is mount via NFS. -> There are plenty bottlenecks (but it is for purpose - about 99% of all requests are either served from cdn or redis cache). When there is a first request for a non-existent cache file (the autoptimize-snippet-*) my little cute docker containers struggle to serve all of the needed cache and there is a risk that my final user will get js without jQuery inside of it. This renders a site useless combination of high-tech code.

I think that I can fix my issue with the proposed plan.

I could try it myself, but there is no way for me to do it without editing (and maintaining) the plugin myself.

Could you please consider at least adding filters for cache directories, so that I could try my theory?

unnamedfeeling avatar Nov 05 '21 16:11 unnamedfeeling

don't think I full grasp the problem, but cache directories can be overruled with constants (but always relative to wp-content), maybe give that a try? :-)

futtta avatar Nov 05 '21 17:11 futtta

Sorry, but this is not what I was thinking of. My idea is that I can switch cache directories from my plugin or theme code. I cannot redefine constants there, but some filter that allows altering this code would be very handy. Something like - classes/autoptimizeCache.php:48 $this->cachedir = apply_filters('ao_cache_dir', AUTOPTIMIZE_CACHE_DIR);

unnamedfeeling avatar Nov 08 '21 11:11 unnamedfeeling

Feel free to make the change locally at your side to begin with and see to what extent it helps you to attain your goals. If it does allow you to do what you need to do, then I'll certainly consider this as a PR :)

On 08/11/2021 12:22, Oleksander Yarosh wrote:

Sorry, but this is not what I was thinking of. My idea is that I can switch cache directories from my plugin or theme code. I cannot redefine constants there, but some filter that allows altering this code would be very handy. Something like - classes/autoptimizeCache.php:48 |$this->cachedir = apply_filters('ao_cache_dir', AUTOPTIMIZE_CACHE_DIR);|

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/futtta/autoptimize/issues/373#issuecomment-963054506, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABMIMNWNAR37SVOTSFBP53UK6XFVANCNFSM5HNLTOBA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

futtta avatar Nov 09 '21 21:11 futtta

closing as no further feedback.

futtta avatar Dec 27 '22 09:12 futtta