diff --git a/README.md b/README.md index 3cc707a..a54fc93 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Artisan commands that queue up entry and glide urls for retrieval, which engages This package can do a few things: 1. Queue up all entry urls for retrieval which automatically engages the static cache, making first time loads quicker. -2. Search for all image and picture sources within your entries and queue them up for a separate retrieval to initiate Glide generation. This means that all images and responsive variants will be pre-generated for your first visitors. This is particularly useful when you have lots of responsive image variants or if you are using Spatie's Statamic Responsive Images package. It can help to avoid server crashes for image heavy websites where Glide has a lot of processing to perform. +2. Search for all image and picture sources within your entries and queue them up for a separate retrieval to initiate Glide generation. This means that all images and responsive variants will be pre-generated for your first visitor. This is particularly useful when you have lots of responsive image variants or if you are using Spatie's Statamic Responsive Images package. It can help to avoid server crashes for image heavy websites where Glide has a lot of processing to perform. 3. Listens for EntrySaved events and automatically queues the entry's url for image requests. **CAUTION:** Glide image manipulation can take a lot of work, especially when using responsive image variants and jpeg fallbacks. For example, a site using Spatie's responsive images addon could have 10 sizes and 2 formats per image. If this site has 1000 images then 20,000 variants will need to be manipulated by Glide. Keep an eye on your CPU usage, especially if using a hosting server that limits CPU (e.g. AWS-EC2). @@ -51,4 +51,4 @@ The queue can be ran manually with this command: `php artisan queue:work redis --queue=cacherequester` -I recommend using a Laravel Forge worker, or something similar, as workers are prone to exit prematurely when using the command line. +I recommend using a Laravel Forge worker, or something similar, as workers are prone to exit prematurely when using the command line. \ No newline at end of file diff --git a/src/Listeners/EntrySavedListener.php b/src/Listeners/EntrySavedListener.php new file mode 100644 index 0000000..b79b114 --- /dev/null +++ b/src/Listeners/EntrySavedListener.php @@ -0,0 +1,24 @@ +entry->url) { + + try{ + RequestUrl::dispatch(url($event->entry->url), true); + } + catch(\Throwable $e){ + Log::warning('Could not queue saved entry for cache requesting.'); + } + + } + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 6a3bdde..82c09dd 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -7,8 +7,7 @@ use Illuminate\Routing\Router; use Illuminate\Support\Facades\Event; use Statamic\Events\EntrySaved; -use Illuminate\Support\Facades\Log; -use stuartcusackie\StatamicCacheRequester\Jobs\RequestUrl; +use stuartcusackie\StatamicCacheRequester\Listeners\EntrySavedListener; use stuartcusackie\StatamicCacheRequester\Console\Commands\RequestEntries; use stuartcusackie\StatamicCacheRequester\Console\Commands\RequestImages; use stuartcusackie\StatamicCacheRequester\Console\Commands\ClearRequestQueue; @@ -20,7 +19,7 @@ public function bootAddon() { $this ->registerCommands() - ->listen(); + ->registerListeners(); } @@ -35,22 +34,9 @@ protected function registerCommands() return $this; } - protected function listen() { + protected function registerListeners() { - Event::listen(function (EntrySaved $event) { - - if($event->entry->url) { - - try{ - RequestUrl::dispatch(url($event->entry->url), true); - } - catch(\Throwable $e){ - Log::warning('Could not queue saved entry for cache requesting.'); - } - - } - - }); + Event::listen(EntrySaved::class, EntrySavedListener::class); return $this;