Skip to content

Commit

Permalink
fix methods with connection
Browse files Browse the repository at this point in the history
  • Loading branch information
januabisconti committed Dec 3, 2024
1 parent 0475d10 commit f50abe5
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 134 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ return [
'lock_timeout' => 600,
'key_invalidation_callback' => null,
'tag_invalidation_callback' => null,
'default_connection_name' => null,
];
```

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

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

// Invalidate a key
$helper->insertInvalidationEvent('key', 'cache_key_xyz', 'Specific cache key invalidated');
$helper->insertInvalidationEvent('key', 'cache_key_xyz', 'fullpage-cache', 'Specific cache key invalidated');

// Invalidate a key with associated tags
// In this example, when the event for article_ID:7 is processed,
Expand All @@ -121,10 +122,12 @@ $helper->insertInvalidationEvent('key', 'cache_key_xyz', 'Specific cache key inv
$helper->insertInvalidationEvent(
'tag',
'article_ID:7',
'fullpage-cache',
'Article 7 removed from sale',
0,
0,
[
['type' => 'tag', 'identifier' => 'plp:sport']
['type' => 'tag', 'identifier' => 'plp:sport', 'connection_name' => 'cache']
]
);
```
Expand All @@ -134,7 +137,7 @@ $helper->insertInvalidationEvent(
Schedule the processing command to run at desired intervals:

```bash
php artisan supercache:process-invalidation --shard=0 --priority=0
php artisan supercache:process-invalidation --shard=0 --priority=0 --connection_name=cache
```

You can add it to your schedule method in App\Console\Kernel.php:
Expand Down
1 change: 1 addition & 0 deletions config/super_cache_invalidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@
*/
'key_invalidation_callback' => env('SUPERCACHE_INVALIDATE_KEY_INVALIDATION_CALLBACK', null),
'tag_invalidation_callback' => env('SUPERCACHE_INVALIDATE_TAG_INVALIDATION_CALLBACK', null),
'default_connection_name' => env('SUPERCACHE_INVALIDATE_DEFAULT_CONNECTION_NAME', 'cache'),

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

// Partition key as a generated stored column
$table->integer('partition_key')->storedAs('
CASE
WHEN `processed` = 0 THEN
(`priority` * `shard`) + `shard` + 1
ELSE
(YEAR(`event_time`) * 10000) + (WEEK(`event_time`, 3) * 100) + (`priority` * `shard`) + `shard`
END
')->comment('Partition key for efficient querying and partitioning');
Schema::create('cache_invalidation_events', function (Blueprint $table) {
//$table->bigIncrements('id');
$table->bigInteger('id')->unsigned(); // Definiamo l'ID come bigInteger senza autoincrement sennò la primarykey multipla non funziona
$table->enum('type', ['key', 'tag'])->comment('Indicates whether the event is for a cache key or tag');
$table->string('identifier')->comment('The cache key or tag to invalidate');
$table->string('connection_name')->comment('Redis Connection name');
$table->string('reason')->nullable()->comment('Reason for the invalidation (for logging purposes)');
$table->tinyInteger('priority')->default(0)->comment('Priority of the event');
$table->dateTime('event_time')->default(DB::raw('CURRENT_TIMESTAMP'))->comment('Timestamp when the event was created');
$table->boolean('processed')->default(0)->comment('Flag indicating whether the event has been processed');
$table->integer('shard')->comment('Shard number for parallel processing');

// Partition key as a generated stored column
$table->integer('partition_key')->storedAs('
CASE
WHEN `processed` = 0 THEN
(`priority` * `shard`) + `shard` + 1
ELSE
(YEAR(`event_time`) * 10000) + (WEEK(`event_time`, 3) * 100) + (`priority` * `shard`) + `shard`
END
')->comment('Partition key for efficient querying and partitioning');

// Indexes
$table->index(['processed', 'shard', 'priority', 'partition_key', 'event_time'], 'idx_processed_shard_priority');
$table->index(['type', 'identifier'], 'idx_type_identifier');
$table->primary(['id', 'partition_key']);
});

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
*/
public function up(): void
{
if (Schema::hasTable('cache_invalidation_timestamps')) {
return;
}
Schema::create('cache_invalidation_timestamps', function (Blueprint $table) {
$table->enum('identifier_type', ['key', 'tag'])->comment('Indicates whether the identifier is a cache key or tag');
$table->string('identifier')->comment('The cache key or tag');
$table->dateTime('last_invalidated')->comment('Timestamp of the last invalidation');

// Partition key as a generated stored column
$table->integer('partition_key')->storedAs('YEAR(`last_invalidated`) * 100 + WEEK(`last_invalidated`, 3)')->comment('Partition key based on last_invalidated');

$table->primary(['identifier_type', 'identifier', 'partition_key']);
$table->index(['identifier_type', 'identifier'], 'idx_identifier_type_identifier');
$table->index('partition_key', 'idx_partition_key');
Expand Down Expand Up @@ -49,7 +50,7 @@ public function down(): void
protected function generatePartitionSQL(): string
{
$startYear = 2024;
$endYear = 2050;
$endYear = 2030;

$partitionStatements = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
*/
public function up(): void
{
if (Schema::hasTable('cache_invalidation_event_associations')) {
return;
}
Schema::create('cache_invalidation_event_associations', function (Blueprint $table) {
//$table->bigIncrements('id');
$table->bigInteger('id')->unsigned(); // Definiamo l'ID come bigInteger senza autoincrement sennò la primarykey multipla non funziona
$table->unsignedBigInteger('event_id')->comment('Reference to cache_invalidation_events.id');
$table->enum('associated_type', ['key', 'tag'])->comment('Indicates if the associated identifier is a cache key or tag');
$table->string('associated_identifier')->comment('The associated cache key or tag');
$table->string('connection_name')->comment('Redis Connection name');
$table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'))->comment('Timestamp of association creation');

// Partition key as a generated stored column
$table->integer('partition_key')->storedAs('YEAR(`created_at`) * 100 + WEEK(`created_at`, 3)')->comment('Partition key based on created_at');

// Indexes
$table->index('event_id', 'idx_event_id');
$table->index(['associated_type', 'associated_identifier'], 'idx_associated_type_identifier');
Expand All @@ -33,7 +35,7 @@ public function up(): void
});

// Abilitare l'autoincrement manualmente
DB::statement('ALTER TABLE cache_invalidation_events MODIFY id BIGINT UNSIGNED AUTO_INCREMENT');
DB::statement('ALTER TABLE cache_invalidation_event_associations MODIFY id BIGINT UNSIGNED AUTO_INCREMENT');

// Generate partitions
$partitionSQL = $this->generatePartitionSQL();
Expand All @@ -60,7 +62,7 @@ public function down(): void
protected function generatePartitionSQL(): string
{
$startYear = 2024;
$endYear = 2050;
$endYear = 2030;

$partitionStatements = [];

Expand Down
Loading

0 comments on commit f50abe5

Please sign in to comment.