-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
caching improvements, enable webhooks support
- Loading branch information
Showing
15 changed files
with
121 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Ikoncept\Fabriq\Listeners; | ||
|
||
use Ikoncept\Fabriq\Fabriq; | ||
use Illuminate\Support\Facades\RateLimiter; | ||
use Infab\TranslatableRevisions\Events\DefinitionsUpdated; | ||
use Spatie\WebhookServer\WebhookCall; | ||
|
||
class CallCacheBustingWebhook | ||
{ | ||
/** | ||
* Create the event listener. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param object $event | ||
* @return void | ||
*/ | ||
public function handle($event) | ||
{ | ||
|
||
$model = $event->model; | ||
|
||
if (! config('fabriq.webhooks.enabled')) { | ||
return; | ||
} | ||
|
||
// Case for pages, skip busting on update | ||
if (get_class($event) === DefinitionsUpdated::class && Fabriq::getFqnModel('page') === get_class($model)) { | ||
return; | ||
} | ||
|
||
$tagsToFlush = collect($event->model->getRevisionOptions()->cacheTagsToFlush)->map(function ($tag) use ($model) { | ||
Check failure on line 42 in src/Listeners/CallCacheBustingWebhook.php GitHub Actions / Laravel (PHP 8.3 on ubuntu-latest)
|
||
$parts = explode('|', $tag); | ||
if (isset($parts[1])) { | ||
$key = $parts[1]; | ||
|
||
if ($key === 'slug' && $model->slugs) { | ||
|
||
$slugs = collect([]); | ||
foreach ($model->slugs as $slug) { | ||
$slugs->push("{$parts[0]}_{$parts[1]}_{$slug->slug}"); | ||
} | ||
|
||
return $slugs->toArray(); | ||
} | ||
|
||
return "{$parts[0]}_{$parts[1]}_{$model->$key}"; | ||
} | ||
|
||
return $tag; | ||
})->flatten(); | ||
|
||
if (! $tagsToFlush->count()) { | ||
return; | ||
} | ||
|
||
// 1 per 5 seconds for the same key | ||
RateLimiter::attempt( | ||
key: hash('adler32', json_encode($tagsToFlush)), | ||
maxAttempts: 1, | ||
callback: function () use ($tagsToFlush) { | ||
WebhookCall::create() | ||
->url(config('fabriq.webhooks.endpoint')) | ||
->payload([ | ||
'type' => 'cache_expiration', | ||
'invalid_cache_tags' => $tagsToFlush->toArray(), | ||
]) | ||
->useSecret(config('fabriq.webhooks.secret')) | ||
->dispatch(); | ||
}, | ||
decaySeconds: 1 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.