Skip to content

Commit 355f80f

Browse files
authored
Merge pull request #19 from iazaran/#18-cache-flexible-not-working-as-expected
#18 cache flexible not working as expected
2 parents c4c6a6a + 59238ec commit 355f80f

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/SmartCache.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ protected function determineCacheDriver($store): ?string
374374
*
375375
* Laravel's flexible method uses: [freshTtl, staleTtl]
376376
* - freshTtl: seconds data is considered fresh
377-
* - staleTtl: additional seconds to serve stale data while revalidating
377+
* - staleTtl: absolute seconds to serve stale data while revalidating
378378
*
379379
* @param string $key
380380
* @param array $durations [freshTtl, staleTtl]
@@ -384,8 +384,8 @@ protected function determineCacheDriver($store): ?string
384384
public function flexible(string $key, array $durations, \Closure $callback): mixed
385385
{
386386
$freshTtl = $durations[0] ?? 3600; // Default 1 hour fresh
387-
$staleTtl = $durations[1] ?? 7200; // Default 2 hours stale
388-
$totalTtl = $freshTtl + $staleTtl;
387+
$staleTtl = $durations[1] ?? 7200; // Default 2 hours stale (absolute time)
388+
$totalTtl = $staleTtl;
389389

390390
// Get cached value with timestamp
391391
$metaKey = $key . '_sc_meta';
@@ -424,7 +424,7 @@ protected function generateAndCache(string $key, array $durations, \Closure $cal
424424
$value = $callback();
425425
$freshTtl = $durations[0] ?? 3600;
426426
$staleTtl = $durations[1] ?? 7200;
427-
$totalTtl = $freshTtl + $staleTtl;
427+
$totalTtl = $staleTtl;
428428

429429
// Optimize the value
430430
$optimizedValue = $this->maybeOptimizeValue($value, $key, $totalTtl);

tests/Unit/SmartCacheTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function test_flexible_method_works_with_optimization()
351351
};
352352

353353
// Laravel format: [freshTtl, staleTtl]
354-
$durations = [3600, 7200]; // Fresh for 1 hour, stale for additional 2 hours
354+
$durations = [3600, 7200]; // Fresh for 1 hour, stale until 2 hours
355355

356356
// Test the flexible method with real SmartCache instance
357357
$result = $this->smartCache->flexible($key, $durations, $callback);
@@ -379,7 +379,7 @@ public function test_flexible_method_fresh_data_retrieval()
379379
return $expectedValue . '-' . $callCount;
380380
};
381381

382-
// Fresh for 10 seconds, stale for additional 20 seconds
382+
// Fresh for 10 seconds, stale until 20 seconds
383383
$durations = [10, 20];
384384

385385
// First call should execute callback and cache result
@@ -403,7 +403,7 @@ public function test_flexible_method_stale_while_revalidate_behavior()
403403
return 'value-' . $callCount;
404404
};
405405

406-
// Fresh for 1 second, stale for additional 5 seconds
406+
// Fresh for 1 second, stale until 5 seconds
407407
$durations = [1, 5];
408408

409409
// Initial call
@@ -435,7 +435,7 @@ public function test_flexible_method_expired_data_regeneration()
435435
return 'value-' . $callCount;
436436
};
437437

438-
// Very short durations for testing: fresh for 1 second, stale for 1 additional second
438+
// Very short durations for testing: fresh for 1 second, stale until 1 second (no stale period)
439439
$durations = [1, 1];
440440

441441
// Initial call
@@ -513,7 +513,7 @@ public function test_flexible_method_with_optimization_integration()
513513
return $largeValue;
514514
};
515515

516-
$durations = [3600, 7200]; // Fresh 1h, stale 2h
516+
$durations = [3600, 7200]; // Fresh 1h, stale until 2h
517517

518518
// Test with real SmartCache instance
519519
$result = $this->smartCache->flexible($key, $durations, $callback);

0 commit comments

Comments
 (0)