Thavarshan / filterable

🔍 Enhance Laravel queries with adaptable, customisable filters and intelligent caching to improve both performance and functionality.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cache pollution issue when switching between query parameters

Thavarshan opened this issue · comments

  • App Version: 1.1.1
  • PHP Version: 8.2.0 / 8.3.4
  • Database Driver & Version: MySQL 8.0

Description

We are encountering an issue in our "filterable" Laravel package where cached results are incorrectly mixed between different query parameters. Specifically, after switching back to a previously made query, the cached results include data from both the current and previous queries.

Steps To Reproduce

  1. Send a GET request to /booking?status=pending. The correct, status-pending bookings are returned.
  2. Send a subsequent GET request to /booking?status=completed. The correct, status-completed bookings are returned.
  3. Reissue the first GET request to /booking?status=pending.

Expected Result: Only bookings with 'pending' status should be returned.
Actual Result: Bookings with both 'pending' and 'completed' statuses are returned, indicating a caching issue where the cache does not differentiate sufficiently between different queries.

Proposed Solution

Enhance Cache Key Generation

Modify the buildCacheKey() function to include all relevant query parameters in the cache key generation. This would ensure that each unique set of parameters has its own cache entry, preventing cache pollution.

protected function buildCacheKey(): string
{
    $userPart = optional($this->forUser)->getAuthIdentifier() ?? 'global';
    $filtersPart = http_build_query($this->getFilterables());
    return "filters:{$userPart}:{$filtersPart}";
}