Skip to content

Commit 657f1c0

Browse files
committed
Fix: Prevent false HIT reporting and add cache status to exception handler
Two critical fixes to ensure accurate HIT/MISS reporting: 1. Fixed false HIT on lock timeout fallback (line 191-203) BEFORE: Cache::get() ?? $callback() then default to HIT PROBLEM: If cache is empty, callback executes but reports HIT AFTER: Explicitly check if Cache::get() returns data - If cached data exists: set HIT - If cache is empty: execute callback and set MISS 2. Added cache status to exception handler (line 216-217) BEFORE: No status set in catch block PROBLEM: Exception path has undefined cache status AFTER: Explicitly set MISS when exception occurs Result: Cache status now accurately reflects whether data was pulled from cache (HIT) or computed via callback (MISS)
1 parent f86fa1c commit 657f1c0

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/Support/ApiModelCache.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,19 @@ public static function cacheQueryResult(Model $model, Request $request, \Closure
190190
// This is rare, but we need graceful fallback
191191
if ($result === null) {
192192
// Try to read from cache (might have been populated by another process)
193-
$result = Cache::tags($tags)->get($cacheKey) ?? $callback();
193+
$cached = Cache::tags($tags)->get($cacheKey);
194+
if ($cached !== null) {
195+
// Cache hit from fallback
196+
$result = $cached;
197+
static::$cacheStatus = 'HIT';
198+
} else {
199+
// Cache miss - execute callback
200+
$result = $callback();
201+
static::$cacheStatus = 'MISS';
202+
}
194203
}
195204

196-
// Default to HIT if MISS was never set
205+
// Default to HIT if MISS was never set (normal path through remember())
197206
static::$cacheStatus ??= 'HIT';
198207

199208
// FINAL GUARD: Ensure we never return null/false
@@ -204,7 +213,9 @@ public static function cacheQueryResult(Model $model, Request $request, \Closure
204213
'error' => $e->getMessage(),
205214
]);
206215

207-
$result = $callback();
216+
// Exception means cache failed, so this is a MISS
217+
static::$cacheStatus = 'MISS';
218+
$result = $callback();
208219

209220
// Guard against callback returning null/false
210221
return $result ?? collect([]);

0 commit comments

Comments
 (0)