Skip to content
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

Cache pollution issue when switching between query parameters #17

Closed
Thavarshan opened this issue May 11, 2024 · 0 comments · Fixed by #18
Closed

Cache pollution issue when switching between query parameters #17

Thavarshan opened this issue May 11, 2024 · 0 comments · Fixed by #18
Assignees
Labels
bug Something isn't working enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed invalid This doesn't seem right

Comments

@Thavarshan
Copy link
Owner

  • 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}";
}
@Thavarshan Thavarshan added bug Something isn't working invalid This doesn't seem right labels May 11, 2024
@Thavarshan Thavarshan self-assigned this May 11, 2024
@Thavarshan Thavarshan added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels May 13, 2024
@Thavarshan Thavarshan linked a pull request May 16, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed invalid This doesn't seem right
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant