Skip to content

Commit

Permalink
Merge pull request #1923 from bakaphp/feat-optimize-graph-customfield…
Browse files Browse the repository at this point in the history
…-cache

refact: clear cache to use queue
  • Loading branch information
kaioken authored Sep 6, 2024
2 parents 2044b62 + 162903a commit 833f641
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function handle()
'app' => $people->app,
]
);
$people->clearLightHouseCache();
$people->clearLightHouseCacheJob();

$currentHourlyCount++;
$currentDailyCount++;
Expand Down
32 changes: 32 additions & 0 deletions src/Baka/Jobs/LightHouseCacheCleanUpJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Baka\Jobs;

use Baka\Traits\KanvasJobsTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;

class LightHouseCacheCleanUpJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use KanvasJobsTrait;

public function __construct(
protected Model $model
) {
}

public function handle(): void
{
if (method_exists($this->model, 'clearLightHouseCache')) {
$this->model->clearLightHouseCache();
}
}
}
6 changes: 6 additions & 0 deletions src/Baka/Traits/HasLightHouseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Baka\Traits;

use Baka\Jobs\LightHouseCacheCleanUpJob;
use Illuminate\Support\Facades\Redis;
use Nuwave\Lighthouse\Cache\CacheKeyAndTagsGenerator;

Expand Down Expand Up @@ -31,6 +32,11 @@ public function clearLightHouseCache(): void
$this->generateFilesLighthouseCache();
}

public function clearLightHouseCacheJob(): void
{
LightHouseCacheCleanUpJob::dispatch($this);
}

public function generateRelationshipLighthouseCache(string $relationship, int $items = 25): void
{
$separator = CacheKeyAndTagsGenerator::SEPARATOR;
Expand Down
2 changes: 1 addition & 1 deletion src/Domains/Guild/Customers/Actions/UpdatePeopleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function execute(): People
}
}

$this->people->clearLightHouseCache();
$this->people->clearLightHouseCacheJob();
return $this->people;
}
}
4 changes: 2 additions & 2 deletions src/Domains/Guild/Customers/Observers/PeopleObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function created(People $people): void
]
);

$people->clearLightHouseCache();
$people->clearLightHouseCacheJob();
}

public function updated(People $people): void
Expand All @@ -32,6 +32,6 @@ public function updated(People $people): void
]
);

$people->clearLightHouseCache();
$people->clearLightHouseCacheJob();
}
}
4 changes: 2 additions & 2 deletions src/Domains/Guild/Leads/Observers/LeadObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public function created(Lead $lead): void
)->execute();
}

$lead->clearLightHouseCache();
$lead->clearLightHouseCacheJob();
}

public function updated(Lead $lead): void
{
$lead->fireWorkflow(WorkflowEnum::UPDATED->value);
Subscription::broadcast('leadUpdate', $lead, true);
$lead->clearLightHouseCache();
$lead->clearLightHouseCacheJob();
}
}
4 changes: 2 additions & 2 deletions src/Domains/Inventory/Products/Observers/ProductsObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function saved(Products $products): void
$products->productsTypes->setTotalProducts();
}

$products->clearLightHouseCache();
$products->clearLightHouseCacheJob();
}

public function created(Products $products): void
Expand All @@ -23,6 +23,6 @@ public function created(Products $products): void
$products->productsTypes->setTotalProducts();
}

$products->clearLightHouseCache();
$products->clearLightHouseCacheJob();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class VariantObserver
{
public function saved(Variants $variant): void
{
$variant->clearLightHouseCache();
$variant->clearLightHouseCacheJob();
}

public function deleting(Variants $variant): void
Expand Down
4 changes: 2 additions & 2 deletions src/Domains/Social/Messages/Observers/MessageObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class MessageObserver
public function created(Message $message): void
{
$message->fireWorkflow(WorkflowEnum::CREATED->value, true, ['app' => $message->app]);
$message->clearLightHouseCache();
$message->clearLightHouseCacheJob();
}

public function updated(Message $message): void
{
$message->fireWorkflow(WorkflowEnum::UPDATED->value, true, ['app' => $message->app]);
$message->clearLightHouseCache();
$message->clearLightHouseCacheJob();
}
}
4 changes: 2 additions & 2 deletions src/Kanvas/CustomFields/Traits/HasCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public function saveCustomFields(): bool
}

if (method_exists($this, 'generateCustomFieldsLighthouseCache')) {
$this->generateCustomFieldsLighthouseCache();
$this->clearLightHouseCacheJob();
}

return true;
Expand Down Expand Up @@ -394,7 +394,7 @@ public static function getByCustomField(string $name, mixed $value, ?Companies $
protected function clearCustomFieldsCacheIfNeeded(): void
{
if (method_exists($this, 'clearLightHouseCache')) {
$this->clearLightHouseCache();
$this->clearLightHouseCacheJob();
}
}
}
6 changes: 1 addition & 5 deletions src/Kanvas/Filesystem/Actions/AttachFilesystemAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public function execute(string $fieldName, ?int $id = null): FilesystemEntities
}

if (method_exists($this->entity, 'clearLightHouseCache')) {
$this->entity->clearLightHouseCache();
}

if (method_exists($this->entity, 'generateFilesLighthouseCache')) {
$this->entity->generateFilesLighthouseCache();
$this->entity->clearLightHouseCacheJob();
}

return $fileEntity;
Expand Down

0 comments on commit 833f641

Please sign in to comment.