Skip to content

Commit

Permalink
Static caching with invalidation on Magento urls (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobWez98 authored Dec 11, 2024
1 parent 9fc5b7e commit 49f890f
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 9 deletions.
126 changes: 126 additions & 0 deletions src/Commands/InvalidateCacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Rapidez\Statamic\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Rapidez\Core\Facades\Rapidez;
use Statamic\StaticCaching\Cacher;

class InvalidateCacheCommand extends Command
{
protected $signature = 'rapidez-statamic:invalidate-cache';

protected $description = 'Search for invalidatable caches';

public $urls;

public $latestCheck;

public function handle(Cacher $cacher): void
{
$this->latestCheck = Cache::get($this->signature);

if (!$this->latestCheck) {
$this->info('Cleared all urls (as we do not have a latest check date yet)');
$this->setLatestCheckDate();
$cacher->flush();
return;
}

$this->setLatestCheckDate();
$stores = Rapidez::getStores();

foreach ($stores as $store) {
Rapidez::setStore($store);

$this->urls = collect();

$this
->addProductsUrls()
->addCategoryUrls()
->addPageUrls()
->makeAbsolute();

$cacher->invalidateUrls($this->urls);

foreach ($this->urls as $url) {
$this->line($url);
}

$this->info('Done invalidating');
}
}

protected function addProductsUrls(): self
{
$products = config('rapidez.models.product')::withoutGlobalScopes()
->where('updated_at', '>=', $this->latestCheck)
->with(['parent:entity_id' => ['rewrites']])
->with('rewrites')
->get('entity_id');

foreach ($products as $product) {
$this->addUrls($this->getUrlsFromRewrites($product->rewrites));

if ($product->parent) {
$this->addUrls($this->getUrlsFromRewrites($product->parent->rewrites));
}
}

return $this;
}

protected function addCategoryUrls(): self
{
$categories = config('rapidez.models.category')::withoutGlobalScopes()
->where('updated_at', '>=', $this->latestCheck)
->get('entity_id');

foreach ($categories as $category) {
$this->addUrls($this->getUrlsFromRewrites($category->rewrites));
}

return $this;
}

protected function addPageUrls(): self
{
$identifiers = config('rapidez.models.page')::query()
->where('update_time', '>=', $this->latestCheck)
->pluck('identifier');

$this->addUrls($identifiers);

return $this;
}

protected function addUrls($urls): self
{
$this->urls = $this->urls->merge($urls);

return $this;
}

protected function getUrlsFromRewrites($rewrites)
{
return $rewrites->map(fn ($rewrite) => $rewrite->request_path);
}

protected function makeAbsolute(): self
{
$this->urls->transform(fn ($identifier) => url($identifier));

return $this;
}

protected function setLatestCheckDate(): void
{
// With this we're just making sure the comparison
// is done within the same timezone in MySQL.
$now = DB::select('SELECT NOW() AS `current_time`');

Cache::forever($this->signature, $now[0]->current_time);
}
}
27 changes: 18 additions & 9 deletions src/RapidezStatamicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@

namespace Rapidez\Statamic;

use Statamic\Sites\Sites;
use Statamic\Facades\Site;
use Statamic\Facades\Entry;
use Statamic\Facades\Utility;
use Illuminate\Routing\Router;
use Rapidez\Core\Facades\Rapidez;
use Statamic\Events\GlobalSetSaved;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Rapidez\Statamic\Tags\Alternates;
use Statamic\Events\GlobalSetDeleted;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\View as RenderedView;
use Rapidez\Statamic\Actions\GenerateSitemapsAction;
Expand All @@ -22,14 +14,25 @@
use Rapidez\Statamic\Forms\JsDrivers\Vue;
use Rapidez\Statamic\Commands\ImportProducts;
use Rapidez\Statamic\Commands\ImportCategories;
use Rapidez\Core\Facades\Rapidez;
use Rapidez\Statamic\Commands\InvalidateCacheCommand;
use Rapidez\Statamic\Extend\SitesLinkedToMagentoStores;
use Rapidez\Statamic\Http\Controllers\ImportsController;
use Rapidez\Statamic\Http\Controllers\StatamicRewriteController;
use Rapidez\Statamic\Http\ViewComposers\StatamicGlobalDataComposer;
use Rapidez\Statamic\Listeners\ClearNavTreeCache;
use Rapidez\Statamic\Listeners\SetCollectionsForNav;
use Rapidez\Statamic\Tags\Alternates;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\NavCreated;
use Statamic\Events\NavTreeSaved;
use Statamic\Facades\Entry;
use Statamic\Facades\Site;
use Statamic\Facades\Utility;
use Statamic\Http\Controllers\FrontendController;
use Statamic\Sites\Sites;
use Statamic\StaticCaching\Middleware\Cache as StaticCache;
use TorMorten\Eventy\Facades\Eventy;

class RapidezStatamicServiceProvider extends ServiceProvider
Expand All @@ -41,6 +44,11 @@ public function register()
});

$this->app->singleton(RapidezStatamic::class);

$this->app->booted(function () {
$router = app(Router::class);
$router->pushMiddlewareToGroup('web', StaticCache::class);
});
}

public function boot()
Expand Down Expand Up @@ -69,6 +77,7 @@ public function bootCommands() : self
ImportProducts::class,
ImportBrands::class,
InstallCommand::class,
InvalidateCacheCommand::class,
]);

return $this;
Expand Down

0 comments on commit 49f890f

Please sign in to comment.