Skip to content

Commit 20c66f2

Browse files
committed
Fix: Remove spl_object_id() from cache key generation to enable cache hits
Root cause: Perpetual cache MISS due to spl_object_id() in callback hash PROBLEM: - Line 60 used: md5(spl_object_id($queryCallback)) - Each HTTP request creates a NEW Closure instance - spl_object_id() is unique per object instance, not per behavior - Same code path produces different object ID every request - Result: Cache key always different, never reuses cached data SYMPTOMS: ✓ Every request reports X-Cache-Status: MISS ✓ Redis shows growing number of cache keys ✓ Cache TTLs expire unused ✓ No stampedes, but no hits either SOLUTION: - Removed callback_hash from additionalParams - Keep has_callback flag for debugging - Callback effects already captured by request parameters - Company UUID, filters, sorts already in cache key - Cache versioning handles invalidation AFTER THIS FIX: - First request: MISS (cache empty) - Subsequent identical requests: HIT - Cache reuse now works correctly Reference: Laravel_Cache_spl_object_id_Callback_Key_Issue_AI_Guide.pdf
1 parent 657f1c0 commit 20c66f2

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

src/Traits/HasApiModelCache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public function queryFromRequestCached(Request $request, ?\Closure $queryCallbac
5454
// Generate additional params from query callback
5555
$additionalParams = [];
5656
if ($queryCallback) {
57-
// Extract parameters that might affect the query
57+
// Mark that a callback is present, but don't hash it
58+
// The callback's effect on results is already captured by request parameters
59+
// Using spl_object_id() would create unique keys per request, preventing cache hits
5860
$additionalParams['has_callback'] = true;
59-
// Use object ID instead of serialize (closures can't be serialized)
60-
$additionalParams['callback_hash'] = md5(spl_object_id($queryCallback));
6161
}
6262

6363
return ApiModelCache::cacheQueryResult(

0 commit comments

Comments
 (0)