Skip to content

FOUR-20950: Handle Cache Invalidation for ETags #7817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ProcessMaker/Http/Resources/Caching/EtagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ public static function generateEtagFromTables(array $tables, string $source = 'u
// If the source is 'etag_version', use a cached version key as the source of truth.
$lastUpdated = collect($tables)->map(function ($table) use ($source) {
if ($source === 'etag_version') {
// This is not currently implemented but serves as a placeholder for future flexibility.
// The idea is to use a cached version key (e.g., "etag_version_table_name") as the source of truth.
// This would allow us to version the ETag dynamically and invalidate it using model observers or other mechanisms.
// If implemented, observers can increment this version key whenever the corresponding table is updated.
/**
* This is not currently implemented but serves as a placeholder for future flexibility.
* The idea is to use a cached version key (e.g., "etag_version_table_name") as the source of truth.
* This would allow us to version the ETag dynamically and invalidate it using model observers or other mechanisms.
* If implemented, observers can increment this version key whenever the corresponding table is updated.
*/
return Cache::get("etag_version_{$table}", 0);
}

Expand Down
65 changes: 65 additions & 0 deletions tests/Feature/Etag/HandleEtagCacheInvalidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace ProcessMaker\Tests\Feature\Etag;

use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Route;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\User;
use Tests\TestCase;

class HandleEtagCacheInvalidationTest extends TestCase
{
use WithFaker;

protected $faker;

private string $response = 'OK';

private const TEST_ROUTE = '/_test/etag-cache-invalidation';

public function setUp(): void
{
parent::setUp();

// Define a route with the etag middleware and etag_tables default.
Route::middleware('etag')
->get(self::TEST_ROUTE, function () {
return response($this->response, 200);
})
->defaults('etag_tables', 'processes');
}

public function testEtagInvalidatesOnDatabaseUpdate()
{
$user = User::factory()->create();
$this->actingAs($user);

// Create a process record to simulate database changes.
$process = Process::factory()->create([
'updated_at' => now()->yesterday(),
]);

// First request: Get the initial ETag.
$response = $this->get(self::TEST_ROUTE);
$initialEtag = $response->headers->get('ETag');
$this->assertNotNull($initialEtag, 'Initial ETag was set');

// Simulate a database update by changing `updated_at`.
$process->update(['name' => $this->faker->name]);

// Second request: ETag should change due to the database update.
$responseAfterUpdate = $this->get(self::TEST_ROUTE);
$newEtag = $responseAfterUpdate->headers->get('ETag');

$this->assertNotNull($newEtag, 'New ETag was set after database update');
$this->assertNotEquals($initialEtag, $newEtag, 'ETag changed after database update');

// Third request: Simulate a client sending the old ETag.
$responseWithOldEtag = $this->withHeaders(['If-None-Match' => $initialEtag])
->get(self::TEST_ROUTE);

$responseWithOldEtag->assertStatus(200);
$responseWithOldEtag->assertHeader('ETag', $newEtag, 'Response did not return the updated ETag');
}
}
33 changes: 30 additions & 3 deletions tests/Feature/Etag/HandleEtagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace ProcessMaker\Tests\Feature\Etag;

use Illuminate\Support\Facades\Route;
use ProcessMaker\Http\Middleware\Etag\HandleEtag;
use ProcessMaker\Http\Resources\Caching\EtagManager;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\User;
use Tests\TestCase;

Expand All @@ -19,7 +18,7 @@ public function setUp(): void
parent::setUp();

// Define a route that uses the HandleEtag middleware.
Route::middleware(HandleEtag::class)->any(self::TEST_ROUTE, function () {
Route::middleware('etag')->any(self::TEST_ROUTE, function () {
return response($this->response, 200);
});
}
Expand Down Expand Up @@ -84,4 +83,32 @@ public function testDefaultGetEtagGeneratesCorrectEtagWithUser()
$expectedEtag = '"' . md5($user->id . $this->response) . '"';
$response->assertHeader('ETag', $expectedEtag);
}

public function testReturns304NotModifiedWhenEtagMatchesTables()
{
$user = User::factory()->create();
$this->actingAs($user);

Process::factory()->create([
'updated_at' => now()->yesterday(),
]);

Route::middleware('etag')->any(self::TEST_ROUTE, function () {
return response($this->response, 200);
})->defaults('etag_tables', 'processes');

// Initial request to get the ETAg.
$response = $this->get(self::TEST_ROUTE);
$etag = $response->headers->get('ETag');
$this->assertNotNull($etag, 'ETag should be set in the initial response');

// Perform a second request sending the `If-None-Match`.
$responseWithMatchingEtag = $this->withHeaders(['If-None-Match' => $etag])
->get(self::TEST_ROUTE);

// Verify response is 304 Not Modified.
$responseWithMatchingEtag->assertStatus(304);
$this->assertEmpty($responseWithMatchingEtag->getContent(), 'Response content is not empty for 304 Not Modified');
$this->assertEquals($etag, $responseWithMatchingEtag->headers->get('ETag'), 'ETag does not match the client-provided If-None-Match');
}
}
Loading