Closed cmalven closed 4 years ago
Thanks for describing your use-case, however I'm going to recommend you use a custom module/plugin to accopmplish this. The EVENT_BEFORE_WARM_CACHE
event allows you to override the URIs that are about to be warmed, so you'd just need to filter this based on your site's individual needs (see https://github.com/putyourlightson/craft-blitz/blob/3.6.5/src/drivers/warmers/BaseCacheWarmer.php#L130).
@bencroker Thanks for the response, @bencroker. I'll go that route.
Great, let me know if you need any pointers!
@bencroker I'm having some trouble figuring out how to actually modify the list of caches that are to be warmed:
Event::on(
BaseCacheWarmer::class,
BaseCacheWarmer::EVENT_BEFORE_WARM_CACHE,
function ($event) {
return array_filter($event->siteUris, function ($item) {
preg_match('/^properties/', $item->uri, $matches);
return count($matches) > 0;
});
}
);
In this example I'm attempting to warm only uris that start with the string properties
. What do I need to do or return from this function to modify the original list of URIs? Thanks!
I'd expect this to work:
$event->siteUris = array_filter($event->siteUris, function ($item) {
preg_match('/^properties/', $item->uri, $matches);
return count($matches) > 0;
});
Thanks, @bencroker. I tried that, too, but it doesn't seem to work either. With or without this event, 180 uris are warmed (the total number of valid URIs for caching based on Blitz rules).
Event::on(
BaseCacheWarmer::class,
BaseCacheWarmer::EVENT_BEFORE_WARM_CACHE,
function ($event) {
$filteredUris = array_filter($event->siteUris, function ($item) {
preg_match('/^properties/', $item->uri, $matches);
return count($matches) > 0;
});
echo count($filteredUris);
$event->siteUris = $filteredUris;
}
);
That will echo
with 90
before continuing to warm 180 URIs.
Ok, I think I see the issue, working on a fix...
Fixed in https://github.com/putyourlightson/craft-blitz/commit/7d7fad9b16b303b96f37be66a4c4f687da618226 for the next release.
Thank you, @bencroker. I'll be sure to test in the next release when it's available.
If you want to help test locally in the meantime:
composer require putyourlightson/craft-blitz:dev-develop
@bencroker Just tested locally. Worked perfectly.
Perfect!
Is it still possible to do this now that Warmers have been replaced with Generators?
It is, you will need to replace the class and event names with the new base generator class and event names, however.
Perfect, thank you @bencroker.
If anybody is looking for the updated code for this:
Event::on(
BaseCacheGenerator::class,
BaseCacheGenerator:: EVENT_BEFORE_GENERATE_CACHE,
function ($event) {
$filteredUris = array_filter($event->siteUris, function ($item) {
preg_match('/^properties/', $item->uri, $matches);
return count($matches) > 0;
});
$event->siteUris = $filteredUris;
}
);
Is your feature request related to a problem? Please describe.
On larger sites, warming the cache for all pages – after a deploy and cache clear, for instance – can be expensive for the server, and often unnecessary as many of those pages are unlikely to be visited, but it would be nice to have some critical top-level URLs warmed immediately.
Describe the solution you would like
Blitz provides the ability to specify additional URLs to warm, but does not provide the ability to specify only specific URLs to warm. Probably the easiest UI to implement would be to add the ability to define URLs to exclude from cache warming, which would narrow down the URLs that Blitz already knows about and warms automatically. This list of regexp patterns would be similar to the list used to exclude URLs from caching entirely in the first tab of Blitz settings.
In most of my use cases, the list of patterns I want to exclude from automatic warming is shorter than the list I want to include, so subtracting patterns would work nicely. For instance I might want to exclude
/blog/.*
and/news/.*
from warming and everything else would be warmed automatically.Describe alternatives you have considered
This could probably be handled using a custom cache warmer, but that seems like overkill for something I'd likely use on many sites.