Skip to content

Commit aff320c

Browse files
committed
Testing for expedition creation and invalidating cache.
1 parent 8acc5aa commit aff320c

File tree

1 file changed

+3
-50
lines changed

1 file changed

+3
-50
lines changed

app/Services/Expedition/ExpeditionService.php

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
use Illuminate\Support\Collection;
3131
use Illuminate\Support\Facades\Log;
3232
use Illuminate\Support\Facades\Notification;
33-
use Illuminate\Support\Facades\Redis;
3433

3534
class ExpeditionService
3635
{
@@ -83,7 +82,7 @@ public function store(Project $project, array $request): mixed
8382
});
8483

8584
// Post-commit: Clear caches (outside tx to ensure commit first)
86-
$this->clearExpeditionCache($project->id, $expedition->id ?? null);
85+
\Artisan::call('lada-cache:flush');
8786

8887
return $expedition;
8988

@@ -370,8 +369,8 @@ public function update(Expedition $expedition, array $request): Expedition
370369
return $expedition;
371370
});
372371

373-
// Clear only expedition-related cache
374-
$this->clearExpeditionCache($expedition->project_id, $expedition->id);
372+
// Post-commit: Clear caches (outside tx to ensure commit first)
373+
\Artisan::call('lada-cache:flush');
375374

376375
return $expedition;
377376
}
@@ -523,50 +522,4 @@ private function removeOldLogo(Expedition $expedition): void
523522
\Log::error("Failed to remove old logo for expedition {$expedition->id}: ".$e->getMessage());
524523
}
525524
}
526-
527-
/**
528-
* Clear targeted Lada-cache for expeditions (table/project/row level).
529-
* Lada-cache was not clearing the project-level expeditions list.
530-
*/
531-
private function clearExpeditionCache(int $projectId, ?int $expeditionId = null): void
532-
{
533-
try {
534-
$redisConnection = config('lada-cache.redis_connection', 'cache');
535-
$prefix = config('lada-cache.prefix', 'lada:');
536-
$redis = Redis::connection($redisConnection);
537-
538-
// Patterns: Broaden for list queries (e.g., project expeditions collection)
539-
$patterns = [
540-
$prefix.'*expeditions*', // Any expedition queries
541-
$prefix.'*actor_expedition*', // Pivot relations
542-
$prefix.'*expedition_stats*', // Stats
543-
$prefix.'*projects*expeditions*', // Project->expeditions lists
544-
$prefix.'*project_id:'.$projectId.'*', // Project-specific (e.g., lists)
545-
];
546-
547-
if ($expeditionId) {
548-
$patterns[] = $prefix.'*expedition_id:'.$expeditionId.'*'; // Row-specific if hashed that way
549-
}
550-
551-
$totalCleared = 0;
552-
foreach ($patterns as $pattern) {
553-
$keys = $redis->keys($pattern);
554-
if (! empty($keys)) {
555-
$redis->del($keys);
556-
$totalCleared += count($keys);
557-
Log::info('Cleared Lada-cache keys', ['pattern' => $pattern, 'count' => count($keys)]);
558-
}
559-
}
560-
561-
if ($totalCleared > 0) {
562-
Log::info('Expedition cache cleared post-create', ['project_id' => $projectId, 'total_keys' => $totalCleared]);
563-
}
564-
565-
} catch (\Exception $e) {
566-
Log::warning('Failed to clear expedition cache', [
567-
'project_id' => $projectId,
568-
'error' => $e->getMessage(),
569-
]);
570-
}
571-
}
572525
}

0 commit comments

Comments
 (0)