Skip to content

Commit 361489a

Browse files
Merge pull request #4 from padosoft/m-jb-sync-cdn
Sincronizzazione invalidate con CDN
2 parents 371ba8d + c44e86f commit 361489a

File tree

6 files changed

+80
-19
lines changed

6 files changed

+80
-19
lines changed

.circleci/config.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ executors:
88
parameters:
99
php-version:
1010
type: string
11-
default: "8.0"
11+
default: "8.2"
1212
working_directory: ~/laravel-super-cache-invalidate
1313

1414
jobs:
@@ -37,7 +37,7 @@ jobs:
3737
- run:
3838
name: Install Dependencies
3939
command: |
40-
sudo composer require "laravel/framework:^10.0" --no-update
40+
sudo composer require "laravel/framework:^12.0" --no-update
4141
sudo composer install --prefer-dist --no-interaction
4242
- run:
4343
name: Prepare bootstrap/cache directory
@@ -53,14 +53,14 @@ workflows:
5353
test:
5454
jobs:
5555
- test:
56-
name: PHP 8.2 + Laravel 10
56+
name: PHP 8.2 + Laravel 12
5757
php-version: "8.2"
58-
laravel-version: "10"
58+
laravel-version: "12"
5959
- test:
60-
name: PHP 8.3 + Laravel 10
60+
name: PHP 8.3 + Laravel 12
6161
php-version: "8.3"
62-
laravel-version: "10"
62+
laravel-version: "12"
6363
- test:
64-
name: PHP 8.4 + Laravel 10
64+
name: PHP 8.4 + Laravel 12
6565
php-version: "8.4"
66-
laravel-version: "10"
66+
laravel-version: "12"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class () extends Migration {
8+
public function up(): void
9+
{
10+
if (Schema::hasColumn('cache_invalidation_events', 'batch_ID')) {
11+
return;
12+
}
13+
14+
Schema::table('cache_invalidation_events', function (Blueprint $table) {
15+
$table->char('batch_ID', 36)->nullable()->index()->after('id');
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
if (!Schema::hasColumn('cache_invalidation_events', 'batch_ID')) {
25+
return;
26+
}
27+
Schema::table('cache_invalidation_events', function (Blueprint $table) {
28+
$table->dropColumn('batch_ID');
29+
});
30+
}
31+
};

src/Console/ProcessCacheInvalidationEventsCommand.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Illuminate\Support\Carbon;
77
use Illuminate\Support\Facades\Cache;
88
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Support\Str;
10+
use Padosoft\SuperCacheInvalidate\Events\BatchCompletedEvent;
911
use Padosoft\SuperCacheInvalidate\Helpers\SuperCacheInvalidationHelper;
1012

1113
class ProcessCacheInvalidationEventsCommand extends Command
@@ -36,6 +38,7 @@ public function __construct(SuperCacheInvalidationHelper $helper)
3638
private function getEventsToInvalidate(Carbon $processingStartTime): array
3739
{
3840
$partitionCache_invalidation_events = $this->helper->getCacheInvalidationEventsUnprocessedPartitionName($this->shardId, $this->priority);
41+
3942
return DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))
4043
->select(['id', 'type', 'identifier', 'connection_name', 'partition_key', 'event_time'])
4144
->where('processed', '=', 0)
@@ -131,6 +134,7 @@ protected function processEvents(): void
131134
if (count($eventsToUpdate) === 0) {
132135
return;
133136
}
137+
134138
$this->processBatch(array_merge(...$eventsAll), $eventsToUpdate);
135139
}
136140

@@ -139,6 +143,8 @@ protected function processBatch(array $allEvents, array $eventsToInvalidate): vo
139143
// Separo le chiavi dai tags
140144
$keys = [];
141145
$tags = [];
146+
// Prima di processare tutti gli eventi, assegno un batch_ID univoco a tutti gli eventi
147+
$batch_ID = (string) Str::uuid();
142148

143149
foreach ($eventsToInvalidate as $item) {
144150
switch ($item->type) {
@@ -170,11 +176,14 @@ protected function processBatch(array $allEvents, array $eventsToInvalidate): vo
170176
DB::table('cache_invalidation_events')
171177
->whereIn('id', array_map(fn ($event) => $event->id, $allEvents))
172178
->whereIn('partition_key', array_map(fn ($event) => $event->partition_key, $allEvents))
173-
->update(['processed' => 1, 'updated_at' => now()])
179+
->update(['processed' => 1, 'batch_ID' => $batch_ID, 'updated_at' => now()])
174180
;
175181
// Riattiva i controlli
176182
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
177183
DB::statement('SET UNIQUE_CHECKS=1;');
184+
185+
// A questo punto avviso il gescat che le chiavi/tags sono stati puliti, per cui può procedere alla pulizia della CDN
186+
event(new BatchCompletedEvent($batch_ID, $this->shardId));
178187
}
179188

180189
/**
@@ -263,7 +272,7 @@ public function handle(): void
263272
$this->logIf('Starting Elaborazione ...' . $this->invalidation_window);
264273
// if (!$lockValue) {
265274
// return;
266-
//}
275+
// }
267276
$startTime = microtime(true);
268277
try {
269278
$this->processEvents();
@@ -275,8 +284,6 @@ public function handle(): void
275284
$e->getLine(),
276285
$e->getTraceAsString()
277286
), 'error');
278-
} finally {
279-
// $this->helper->releaseShardLock($this->shardId, $this->priority, $lockValue, $this->connection_name);
280287
}
281288
$executionTime = (microtime(true) - $startTime) * 1000;
282289
$this->logIf('Fine Elaborazione - Tempo di esecuzione: ' . $executionTime . ' millisec.');

src/Events/BatchCompletedEvent.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Padosoft\SuperCacheInvalidate\Events;
4+
5+
class BatchCompletedEvent
6+
{
7+
use \Illuminate\Foundation\Events\Dispatchable, \Illuminate\Queue\SerializesModels;
8+
9+
public string $batch_ID;
10+
public int $shard;
11+
12+
public function __construct(string $batch_ID, int $shard)
13+
{
14+
$this->batch_ID = $batch_ID;
15+
$this->shard = $shard;
16+
}
17+
}

src/Helpers/SuperCacheInvalidationHelper.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ public function insertInvalidationEvent(
3030
?int $priority = 0,
3131
?int $processed = 0,
3232
?Carbon $event_time = null,
33-
/*?array $associatedIdentifiers = [],*/
33+
/* ?array $associatedIdentifiers = [], */
34+
?int $shard = -1,
3435
): void {
35-
$shard = crc32($identifier) % ($totalShards > 0 ? $totalShards : config('super_cache_invalidate.total_shards', 10));
36+
if ($shard < 0) {
37+
$shard = crc32($identifier) % ($totalShards > 0 ? $totalShards : config('super_cache_invalidate.total_shards', 10));
38+
}
3639
$redisConnectionName = $connection_name ?? config('super_cache_invalidate.default_connection_name');
3740
$data = [
3841
'type' => $type,
@@ -50,7 +53,7 @@ public function insertInvalidationEvent(
5053
$insertOk = false;
5154

5255
while ($attempts < $maxAttempts && !$insertOk) {
53-
//DB::beginTransaction();
56+
// DB::beginTransaction();
5457

5558
try {
5659
// Cerca di bloccare il record per l'inserimento
@@ -65,7 +68,7 @@ public function insertInvalidationEvent(
6568
$attempts = 5; // Mi fermo
6669
throw new \RuntimeException('Invalid value for processed');
6770
}
68-
//$eventId = DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))->insertGetId($data);
71+
// $eventId = DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))->insertGetId($data);
6972
DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))->insert($data);
7073
// Insert associated identifiers
7174
// TODO JB 31/12/2024: per adesso commentato, da riattivare quando tutto funziona alla perfezione usando la partizione,
@@ -86,9 +89,9 @@ public function insertInvalidationEvent(
8689
}
8790
*/
8891
$insertOk = true;
89-
//DB::commit(); // Completa la transazione
92+
// DB::commit(); // Completa la transazione
9093
} catch (\Throwable $e) {
91-
//DB::rollBack(); // Annulla la transazione in caso di errore
94+
// DB::rollBack(); // Annulla la transazione in caso di errore
9295
$attempts++;
9396
Log::error("SuperCacheInvalidate: impossibile eseguire insert, tentativo $attempts di $maxAttempts: " . $e->getMessage());
9497
// Logica per gestire i tentativi falliti
@@ -146,12 +149,14 @@ public function releaseShardLock(int $shardId, int $priority, string $lockValue,
146149
public function getCacheInvalidationEventsUnprocessedPartitionName(int $shardId, int $priorityId): string
147150
{
148151
$partitionValue = ($priorityId * 10) + $shardId;
152+
149153
return "p_unprocessed_{$partitionValue}";
150154
}
151155

152156
public function getCacheInvalidationEventsProcessedPartitionName(int $shardId, int $priorityId, Carbon $event_time): string
153157
{
154158
$partitionValue = ($event_time->year * 10000) + ($event_time->weekOfYear * 100) + ($priorityId * 10) + $shardId;
159+
155160
return "p_processed_{$partitionValue}";
156161
}
157162
}

tests/Unit/SuperCacheInvalidationHelperTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public function test_insert_invalidation_event(): void
6060
1,
6161
0,
6262
0,
63-
$now
63+
$now,
64+
0,
6465
);
6566
}
6667

0 commit comments

Comments
 (0)