Skip to content

Commit f50abe5

Browse files
committed
fix methods with connection
1 parent 0475d10 commit f50abe5

8 files changed

+162
-134
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ return [
9494
'lock_timeout' => 600,
9595
'key_invalidation_callback' => null,
9696
'tag_invalidation_callback' => null,
97+
'default_connection_name' => null,
9798
];
9899
```
99100

@@ -108,10 +109,10 @@ use Padosoft\SuperCacheInvalidate\Helpers\SuperCacheInvalidationHelper;
108109
$helper = app(Padosoft\SuperCacheInvalidate\Helpers\SuperCacheInvalidationHelper::class);
109110

110111
// Invalidate a tag
111-
$helper->insertInvalidationEvent('tag', 'category:sport', 'Product updated in category sport');
112+
$helper->insertInvalidationEvent('tag', 'category:sport', 'cache', 'Product updated in category sport');
112113

113114
// Invalidate a key
114-
$helper->insertInvalidationEvent('key', 'cache_key_xyz', 'Specific cache key invalidated');
115+
$helper->insertInvalidationEvent('key', 'cache_key_xyz', 'fullpage-cache', 'Specific cache key invalidated');
115116

116117
// Invalidate a key with associated tags
117118
// In this example, when the event for article_ID:7 is processed,
@@ -121,10 +122,12 @@ $helper->insertInvalidationEvent('key', 'cache_key_xyz', 'Specific cache key inv
121122
$helper->insertInvalidationEvent(
122123
'tag',
123124
'article_ID:7',
125+
'fullpage-cache',
124126
'Article 7 removed from sale',
125127
0,
128+
0,
126129
[
127-
['type' => 'tag', 'identifier' => 'plp:sport']
130+
['type' => 'tag', 'identifier' => 'plp:sport', 'connection_name' => 'cache']
128131
]
129132
);
130133
```
@@ -134,7 +137,7 @@ $helper->insertInvalidationEvent(
134137
Schedule the processing command to run at desired intervals:
135138

136139
```bash
137-
php artisan supercache:process-invalidation --shard=0 --priority=0
140+
php artisan supercache:process-invalidation --shard=0 --priority=0 --connection_name=cache
138141
```
139142

140143
You can add it to your schedule method in App\Console\Kernel.php:

config/super_cache_invalidate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@
6969
*/
7070
'key_invalidation_callback' => env('SUPERCACHE_INVALIDATE_KEY_INVALIDATION_CALLBACK', null),
7171
'tag_invalidation_callback' => env('SUPERCACHE_INVALIDATE_TAG_INVALIDATION_CALLBACK', null),
72+
'default_connection_name' => env('SUPERCACHE_INVALIDATE_DEFAULT_CONNECTION_NAME', 'cache'),
7273

7374
];

database/migrations/2024_11_20_200800_create_cache_invalidation_events_table.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,38 @@ protected function generatePartitionSQL(): string
5353
*/
5454
public function up(): void
5555
{
56-
if (!Schema::hasTable('cache_invalidation_events')) {
57-
Schema::create('cache_invalidation_events', function (Blueprint $table) {
58-
//$table->bigIncrements('id');
59-
$table->bigInteger('id')->unsigned(); // Definiamo l'ID come bigInteger senza autoincrement sennò la primarykey multipla non funziona
60-
$table->enum('type', ['key', 'tag'])->comment('Indicates whether the event is for a cache key or tag');
61-
$table->string('identifier')->comment('The cache key or tag to invalidate');
62-
$table->string('reason')->nullable()->comment('Reason for the invalidation (for logging purposes)');
63-
$table->tinyInteger('priority')->default(0)->comment('Priority of the event');
64-
$table->dateTime('event_time')->default(DB::raw('CURRENT_TIMESTAMP'))->comment('Timestamp when the event was created');
65-
$table->boolean('processed')->default(0)->comment('Flag indicating whether the event has been processed');
66-
$table->integer('shard')->comment('Shard number for parallel processing');
56+
if (Schema::hasTable('cache_invalidation_events')) {
57+
return;
58+
}
6759

68-
// Partition key as a generated stored column
69-
$table->integer('partition_key')->storedAs('
70-
CASE
71-
WHEN `processed` = 0 THEN
72-
(`priority` * `shard`) + `shard` + 1
73-
ELSE
74-
(YEAR(`event_time`) * 10000) + (WEEK(`event_time`, 3) * 100) + (`priority` * `shard`) + `shard`
75-
END
76-
')->comment('Partition key for efficient querying and partitioning');
60+
Schema::create('cache_invalidation_events', function (Blueprint $table) {
61+
//$table->bigIncrements('id');
62+
$table->bigInteger('id')->unsigned(); // Definiamo l'ID come bigInteger senza autoincrement sennò la primarykey multipla non funziona
63+
$table->enum('type', ['key', 'tag'])->comment('Indicates whether the event is for a cache key or tag');
64+
$table->string('identifier')->comment('The cache key or tag to invalidate');
65+
$table->string('connection_name')->comment('Redis Connection name');
66+
$table->string('reason')->nullable()->comment('Reason for the invalidation (for logging purposes)');
67+
$table->tinyInteger('priority')->default(0)->comment('Priority of the event');
68+
$table->dateTime('event_time')->default(DB::raw('CURRENT_TIMESTAMP'))->comment('Timestamp when the event was created');
69+
$table->boolean('processed')->default(0)->comment('Flag indicating whether the event has been processed');
70+
$table->integer('shard')->comment('Shard number for parallel processing');
71+
72+
// Partition key as a generated stored column
73+
$table->integer('partition_key')->storedAs('
74+
CASE
75+
WHEN `processed` = 0 THEN
76+
(`priority` * `shard`) + `shard` + 1
77+
ELSE
78+
(YEAR(`event_time`) * 10000) + (WEEK(`event_time`, 3) * 100) + (`priority` * `shard`) + `shard`
79+
END
80+
')->comment('Partition key for efficient querying and partitioning');
81+
82+
// Indexes
83+
$table->index(['processed', 'shard', 'priority', 'partition_key', 'event_time'], 'idx_processed_shard_priority');
84+
$table->index(['type', 'identifier'], 'idx_type_identifier');
85+
$table->primary(['id', 'partition_key']);
86+
});
7787

78-
// Indexes
79-
$table->index(['processed', 'shard', 'priority', 'partition_key', 'event_time'], 'idx_processed_shard_priority');
80-
$table->index(['type', 'identifier'], 'idx_type_identifier');
81-
$table->primary(['id', 'partition_key']);
82-
});
83-
}
8488
// Abilitare l'autoincrement manualmente
8589
DB::statement('ALTER TABLE cache_invalidation_events MODIFY id BIGINT UNSIGNED AUTO_INCREMENT');
8690

database/migrations/2024_11_20_200801_create_cache_invalidation_timestamps_table.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
*/
1212
public function up(): void
1313
{
14+
if (Schema::hasTable('cache_invalidation_timestamps')) {
15+
return;
16+
}
1417
Schema::create('cache_invalidation_timestamps', function (Blueprint $table) {
1518
$table->enum('identifier_type', ['key', 'tag'])->comment('Indicates whether the identifier is a cache key or tag');
1619
$table->string('identifier')->comment('The cache key or tag');
1720
$table->dateTime('last_invalidated')->comment('Timestamp of the last invalidation');
18-
1921
// Partition key as a generated stored column
2022
$table->integer('partition_key')->storedAs('YEAR(`last_invalidated`) * 100 + WEEK(`last_invalidated`, 3)')->comment('Partition key based on last_invalidated');
21-
2223
$table->primary(['identifier_type', 'identifier', 'partition_key']);
2324
$table->index(['identifier_type', 'identifier'], 'idx_identifier_type_identifier');
2425
$table->index('partition_key', 'idx_partition_key');
@@ -49,7 +50,7 @@ public function down(): void
4950
protected function generatePartitionSQL(): string
5051
{
5152
$startYear = 2024;
52-
$endYear = 2050;
53+
$endYear = 2030;
5354

5455
$partitionStatements = [];
5556

database/migrations/2024_11_20_200802_create_cache_invalidation_event_associations_table.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111
*/
1212
public function up(): void
1313
{
14+
if (Schema::hasTable('cache_invalidation_event_associations')) {
15+
return;
16+
}
1417
Schema::create('cache_invalidation_event_associations', function (Blueprint $table) {
1518
//$table->bigIncrements('id');
1619
$table->bigInteger('id')->unsigned(); // Definiamo l'ID come bigInteger senza autoincrement sennò la primarykey multipla non funziona
1720
$table->unsignedBigInteger('event_id')->comment('Reference to cache_invalidation_events.id');
1821
$table->enum('associated_type', ['key', 'tag'])->comment('Indicates if the associated identifier is a cache key or tag');
1922
$table->string('associated_identifier')->comment('The associated cache key or tag');
23+
$table->string('connection_name')->comment('Redis Connection name');
2024
$table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'))->comment('Timestamp of association creation');
21-
2225
// Partition key as a generated stored column
2326
$table->integer('partition_key')->storedAs('YEAR(`created_at`) * 100 + WEEK(`created_at`, 3)')->comment('Partition key based on created_at');
24-
2527
// Indexes
2628
$table->index('event_id', 'idx_event_id');
2729
$table->index(['associated_type', 'associated_identifier'], 'idx_associated_type_identifier');
@@ -33,7 +35,7 @@ public function up(): void
3335
});
3436

3537
// Abilitare l'autoincrement manualmente
36-
DB::statement('ALTER TABLE cache_invalidation_events MODIFY id BIGINT UNSIGNED AUTO_INCREMENT');
38+
DB::statement('ALTER TABLE cache_invalidation_event_associations MODIFY id BIGINT UNSIGNED AUTO_INCREMENT');
3739

3840
// Generate partitions
3941
$partitionSQL = $this->generatePartitionSQL();
@@ -60,7 +62,7 @@ public function down(): void
6062
protected function generatePartitionSQL(): string
6163
{
6264
$startYear = 2024;
63-
$endYear = 2050;
65+
$endYear = 2030;
6466

6567
$partitionStatements = [];
6668

0 commit comments

Comments
 (0)