Skip to content

Commit 2556a54

Browse files
committed
Fix bug with cache lifetime priority inside Phalcon\Mvc\Model\Query
1 parent ae4bc23 commit 2556a54

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

Diff for: CHANGELOG-5.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Fixed
1010

1111
- Fixed `Phalcon\Translate\Adapter\Csv` the `escape` argument is explicitly required in PHP 8.4 [#16733](https://github.com/phalcon/cphalcon/issues/16733)
12+
- Fixed `Phalcon\Mvc\Model\Query` to use the cacheOptions lifetime over the "cache" service lifetime
1213

1314
### Removed
1415

Diff for: phalcon/Mvc/Model/Query.zep

+2-8
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,6 @@ class Query implements QueryInterface, InjectionAwareInterface
283283
);
284284
}
285285

286-
/**
287-
* By default use use 3600 seconds (1 hour) as cache lifetime
288-
*/
289-
if !fetch lifetime, cacheOptions["lifetime"] {
290-
let lifetime = 3600;
291-
}
292-
293286
if !fetch cacheService, cacheOptions["service"] {
294287
let cacheService = "modelsCache";
295288
}
@@ -309,7 +302,8 @@ class Query implements QueryInterface, InjectionAwareInterface
309302
*/
310303
let adapter = cache->getAdapter();
311304
let cacheLifetime = adapter->getLifetime();
312-
if (lifetime !== cacheLifetime) {
305+
306+
if !fetch lifetime, cacheOptions["lifetime"] {
313307
let lifetime = cacheLifetime;
314308
}
315309

Diff for: tests/database/Mvc/Model/FindCest.php

+72
Original file line numberDiff line numberDiff line change
@@ -538,4 +538,76 @@ function () {
538538
$expected = 'Use of "static" in callables in deprecated';
539539
$I->assertStringNotContainsString($expected, $actual);
540540
}
541+
542+
public function testMvcModelFindWithCacheOptionsLifetimePriorityOverCacheService(DatabaseTester $I): void
543+
{
544+
$I->wantToTest('Mvc\Model - find() - cache options lifetime priority over adapter lifetime');
545+
546+
/** @var PDO $connection */
547+
$connection = $I->getConnection();
548+
549+
$migration = new ObjectsMigration($connection);
550+
$migration->insert(1, 'random data', 1);
551+
552+
$options = [
553+
'defaultSerializer' => 'Json',
554+
'lifetime' => 2,
555+
'prefix' => 'data-',
556+
];
557+
558+
/**
559+
* Models Cache setup. Adapter's lifetime is 2 seconds
560+
*/
561+
$serializerFactory = new SerializerFactory();
562+
$adapterFactory = new AdapterFactory($serializerFactory);
563+
$adapter = $adapterFactory->newInstance('apcu', $options);
564+
$cache = new Cache($adapter);
565+
566+
$this->container->setShared('modelsCache', $cache);
567+
568+
/**
569+
* Find records with lifetime 5 sec
570+
*/
571+
$data = Objects::find(
572+
[
573+
'cache' => [
574+
'key' => 'my-cache',
575+
'lifetime' => 5,
576+
],
577+
]
578+
);
579+
580+
$I->assertEquals(1, count($data));
581+
582+
$record = $data[0];
583+
$I->assertEquals(1, $record->obj_id);
584+
$I->assertEquals('random data', $record->obj_name);
585+
586+
/**
587+
* Get the models cache
588+
*/
589+
$modelsCache = $this->container->get('modelsCache');
590+
591+
$exists = $modelsCache->has('my-cache');
592+
$I->assertTrue($exists);
593+
594+
/**
595+
* Wait for 3 seconds for the cache to check
596+
* that we still have our cache and it wasn't taken
597+
* from adapter's lifetime
598+
*/
599+
sleep(3);
600+
601+
$data = $modelsCache->get('my-cache');
602+
$I->assertNotNull($data);
603+
604+
/**
605+
* Wait extra 3 seconds for the cache to check
606+
* that our cache is expired
607+
*/
608+
sleep(3);
609+
610+
$data = $modelsCache->get('my-cache');
611+
$I->assertNull($data);
612+
}
541613
}

0 commit comments

Comments
 (0)