Skip to content

chore: adds APC as L1 cache at cache middleware#410

Closed
smarcet wants to merge 2 commits intomainfrom
feat/l2-apc-cache
Closed

chore: adds APC as L1 cache at cache middleware#410
smarcet wants to merge 2 commits intomainfrom
feat/l2-apc-cache

Conversation

@smarcet
Copy link
Collaborator

@smarcet smarcet commented Oct 14, 2025

No description provided.

@smarcet smarcet requested a review from Copilot October 14, 2025 21:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Introduce APCu-backed L1 cache to reduce Redis hits in the cache middleware and clear APC entries when cache regions are flushed.

  • Add App\Utils\Cache\APC utility for APCu get/put and region tracking/clearing.
  • Update CacheMiddleware to check APC first, then fall back to Redis; add X-Cache-Result distinctions.
  • Update RedisCacheService to clear APC region keys on region flush.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
app/Utils/Cache/APC.php New APCu cache helper with region tracking and clearing.
app/Services/Utils/RedisCacheService.php Flush APC region when clearing a Redis cache region.
app/Http/Middleware/CacheMiddleware.php Introduce APC L1 lookup, Redis fallback, and response headers reflecting cache tier.
Comments suppressed due to low confidence (1)

app/Http/Middleware/CacheMiddleware.php:1

  • When APC hits in the non-tagged branch, $data is never decoded because $data = $this->decode($encoded) is inside the if(!$wasAPCHit) block. This causes $data to remain null and the 'safe guard' below sets it to an empty array, returning an empty response. Move the decode outside the if(!$wasAPCHit) block (mirroring the tagged branch), e.g., place $data = $this->decode($encoded) after the closing brace so it runs for both APC and Redis paths.
<?php namespace App\Http\Middleware;

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +25 to +35
private static function trackKey(string $regionTag, string $key, int $ttl): void
{
try {
$setKey = self::APC_REGION_PREFIX.$regionTag;
$list = self::apc()->get($setKey, []);
if (!\is_array($list)) $list = [];
$list[$key] = \time() + $ttl;
if (\count($list) > 10000) { array_shift($list); }
self::apc()->put($setKey, $list, $ttl);
} catch (\Throwable $e) { /* ignore */ }
}
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The hard-coded 10000 cap is a magic number. Consider extracting it to a class constant (e.g., MAX_KEYS_PER_REGION) or a config value so it can be tuned without code changes.

Copilot uses AI. Check for mistakes.
@smarcet smarcet force-pushed the feat/l2-apc-cache branch 9 times, most recently from b4fb00d to 3424ef2 Compare October 14, 2025 22:55
@smarcet smarcet closed this Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants