-
-
Notifications
You must be signed in to change notification settings - Fork 592
[6.x] Enable background re-caching of static cache #9396
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
[6.x] Enable background re-caching of static cache #9396
Conversation
|
Do I understand correctly, that this will serve the old cached file until the new cached file is ready? And then it gets rid of the old cached file? Or what exactly is the purpose of this? Sounds interesting for sure. Also, from a configuration point of view, it might be easier to just have the config to be a boolean and then assign a random string automatically if the config is true. |
When the new cached file is ready, it'll just stomp over the top of the old one.
We need to know the value ahead of time, so it can't just be a randomly changing string. |
|
@aerni the idea of it being a fixed string was to provide some sort of security - that someone malicious couldn’t just constantly force your site to recache. |
|
Personally, I'd be a little intimidated by the instruction to "add a unique string here that should not be easy to guess". Like, what is "easy to guess"? Couldn't we just use the
By "stomp over" I suppose you mean the old file will just not be served anymore? Won't this result in a lot of abandoned files pretty fast? Should there maybe be some sort of cleanup? |
|
Maybe we could make it a Boolean config and the token could be a Crypt or Sha of the url. The app key is used in the Crypt so it wouldn’t be game-able. |
it literally replaces the same file. So no clean up will be needed. |
Oh yeah, makes sense. It's early morning here 😄 |
|
@aerni here you go - its a boolean now. Feels simpler. |
|
Love it, thanks! Curious as to why you decided to make this opt-in, though. Feels like everyone who uses static caching would want to enable this feature. Or is there any potential downside to it? |
|
I make all my PRs opt in until Jason changes them to not be :) |
An edge case idea: |
|
@Krzemo Are those things not already a potential issue with the existing implementation? My understanding is modern filesystems will lock the file during writing, so any read operations will wait until the file is unlocked, so it shouldn't be an issue. |
|
@ryanmitchell Im not worried about OS not doing its job. Maybe asking different questions will help explain what I mean (I feel like I should run tests myself first - will do next week if still needed):
|
|
What's the status of this @jasonvarga ? We're having the same issues as #8410 and hope this solves it but it's been draging on for a while it seems |
|
We haven't had time to look at it yet. We're currently focused on Statamic 6. If you need these changes now, you could try applying this PR as a composer patch. |
# Conflicts: # src/Console/Commands/StaticWarm.php # src/StaticCaching/DefaultInvalidator.php # src/StaticCaching/Invalidate.php # tests/StaticCaching/DefaultInvalidatorTest.php
|
Forgive the delay on this. I like the no-config solution to the If you're using full measure, you'd need to add something to your nginx to bypass the cached page (which is fine) but we need to make sure that every request that has So maybe we have a config option for the token itself. Then you can put Does that sound right? (You don't need to implement it. I just want a sanity check.) Or... if picking a token is the issue, what if we do just use some consistent string and you get the value by running an artisan command? Under the hood it's just something like |
Having just dealt with doing load balancing in nginx it is much easier/reliable to switch an nginx config based off a cookie. Maybe the cookie could get set via a backend call via JS (similar to nocache)? This would make it compatible with full cache too. |
If youre using full measure with If you're using In both cases if the token is invalid it would serve the static HTML page if it exists (via the StaticCache middleware) |
|
Whether its a cookie, header, or query param, the server needs to check not only if it has the thing, but that it matches a value. Otherwise anyone could craft a request to invalidate the pages.
True, but if you enable full measure static caching, the point is to bypass PHP. @godismyjudge95 I'm curious to hear more about the cookie thing. Why would reading a cookie be any better than a query param? |
…esnt complain even though it worked fine
It could have been just me having a long day but I could not get NGINX in a reverse proxy/node balancer setup to consistently recognize a query param. It was getting lost or something along the way. By contrast, NGINX makes cookies and their contents available via variables like Again could just be skill issue on my part but I found the cookie method to be much easier to work with. 🤷♂️ |
|
I'm not sure of the difficulty you had using a query parameter. Using a cookie sounds more complicated to me honestly. This seems to work well and just requires adding a small block your nginx config. if ($args ~* "live-preview=(.*)") {
set $try_location @not_static;
}
+if ($arg___recache = "abc123def456") {
+ set $try_location @not_static;
+}
location / {
try_files $uri $try_location;
}You can get your token by running That'll be a consistent hash based off your app key. Or if you prefer, you can set the token explicitly in your config: // config/statamic/static_caching.php
'recache_token' => 'my-explicit-token',Since your nginx config is only allowing requests with that explicit key to bypass, visiting |
|
[edit] GitHub sucks.... [/edit] a normal thank you will do. |
|
Thank you! |
|
Awesome! 🚀 Thanks @ryanmitchell and @jasonvarga! |
This PR enables background re-caching of the static cache through a new
static_caching.background_recachevariable (or STATAMIC_BACKGROUND_RECACHE .env value).When set to true the static cache will re-cache the page without invalidating the previous cache, so at every point a static file will exist and end users will not encounter slow loading pages.
Invalidation continues to occur as it currently does when content is deleted, or when this variable is set to false (the default).