Skip to content

Commit

Permalink
Merge pull request #16714 from niden-code/T16696-cache-lifetime
Browse files Browse the repository at this point in the history
T16696 cache lifetime
  • Loading branch information
niden authored Mar 7, 2025
2 parents a1e8d55 + e020540 commit 8e17493
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Fixed `Phalcon\Dispatcher\AbstractDispatcher` when calling action methods that do not define parameters to prevent `Unknown named parameter` error.
- Fixed `Phalcon\Di\Injectable` to reference the correct instance of `Phalcon\Di\Di` in the docblock property [#16634](https://github.com/phalcon/cphalcon/issues/16634)
- Fixed `Phalcon\Filter\Filter` to have the correct docblock for IDE completion
- Fixed `Phalcon\Mvc\Model\Query` to use the lifetime in the "cache" service if none has been supplied by the options [#16696](https://github.com/phalcon/cphalcon/issues/16696)

### Removed

Expand Down
16 changes: 13 additions & 3 deletions phalcon/Mvc/Model/Query.zep
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ class Query implements QueryInterface, InjectionAwareInterface
*/
public function execute(array bindParams = [], array bindTypes = [])
{
var uniqueRow, cacheOptions, key, cacheService, cache, result,
preparedResult, defaultBindParams, mergedParams, defaultBindTypes,
mergedTypes, type, lifetime, intermediate;
var adapter, cache, cacheLifetime, cacheOptions, cacheService,
defaultBindParams, defaultBindTypes, intermediate, key, lifetime,
mergedParams, mergedTypes, preparedResult, result, type, uniqueRow;

let uniqueRow = this->uniqueRow,
cacheOptions = this->cacheOptions;
Expand Down Expand Up @@ -303,6 +303,16 @@ class Query implements QueryInterface, InjectionAwareInterface
);
}

/**
* If the lifetime is different than the cache lifetime, assign
* the cache lifetime to the current cache setting
*/
let adapter = cache->getAdapter();
let cacheLifetime = adapter->getLifetime();
if (lifetime !== cacheLifetime) {
let lifetime = cacheLifetime;
}

let result = cache->get(key);

if !empty result {
Expand Down
10 changes: 10 additions & 0 deletions phalcon/Storage/Adapter/AbstractAdapter.zep
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface
*/
abstract public function getKeys(string prefix = "") -> array;

/**
* Returns the lifetime
*
* @return int
*/
public function getLifetime() -> int
{
return this->lifetime;
}

/**
* Returns the prefix
*
Expand Down
2 changes: 1 addition & 1 deletion tests/database/Db/Column/GetScaleCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GetScaleCest

public function _before(DatabaseTester $I)
{
$I->skipTest('Temporary disabled');
$I->markTestSkipped('Temporary disabled');
$this->setNewFactoryDefault();
$this->setDatabase($I);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/database/Mvc/Model/Behavior/SoftDeleteCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function mvcModelBehaviorSoftDelete(DatabaseTester $I)
public function mvcModelBehaviorSoftDeleteWithBeforeDeleteEvent(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model\Behavior - SoftDelete() - with before delete event');
$I->skipTest('See: https://github.com/phalcon/cphalcon/issues/14904');
$I->markTestSkipped('See: https://github.com/phalcon/cphalcon/issues/14904');

/** ADD BeforeDelete event */
$eventsManager = new EventManager();
Expand Down
2 changes: 1 addition & 1 deletion tests/database/Mvc/Model/Behavior/TimestampableCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function mvcModelBehaviorTimestampable(DatabaseTester $I)
public function mvcModelBehaviorTimestampableWithBeforeCreateEvent(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model\Behavior - Timestampable() - with before create event');
$I->skipTest('See: https://github.com/phalcon/cphalcon/issues/14904');
$I->markTestSkipped('See: https://github.com/phalcon/cphalcon/issues/14904');

/** ADD BeforeDelete event */
$eventsManager = new EventManager();
Expand Down
71 changes: 71 additions & 0 deletions tests/database/Mvc/Model/FindCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use function ob_get_contents;
use function ob_start;
use function outputDir;
use function sleep;
use function uniqid;
use function var_dump;

Expand Down Expand Up @@ -216,6 +217,76 @@ public function mvcModelFindResultsetSecondIteration(DatabaseTester $I)
}
}

/**
* Tests Phalcon\Mvc\Model :: find()
*
* @author Phalcon Team <[email protected]>
* @since 2020-02-01
*
* @group mysql
* @issue 16696
*/
public function testMvcModelFindWithCacheLifetimeFromCacheService(DatabaseTester $I): void
{
/** @var PDO $connection */
$connection = $I->getConnection();

$migration = new ObjectsMigration($connection);
$migration->insert(1, 'random data', 1);

$options = [
'defaultSerializer' => 'Json',
'lifetime' => 2,
'prefix' => 'data-',
];

/**
* Models Cache setup. Lifetime is 2 seconds
*/
$serializerFactory = new SerializerFactory();
$adapterFactory = new AdapterFactory($serializerFactory);
$adapter = $adapterFactory->newInstance('apcu', $options);
$cache = new Cache($adapter);

$this->container->setShared('modelsCache', $cache);

/**
* Get the records (should cache the resultset)
*/
$data = Objects::find(
[
'cache' => [
'key' => 'my-cache',
],
]
);

$I->assertEquals(1, count($data));

$record = $data[0];
$I->assertEquals(1, $record->obj_id);
$I->assertEquals('random data', $record->obj_name);

/**
* Get the models cache
*/
$modelsCache = $this->container->get('modelsCache');

$exists = $modelsCache->has('my-cache');
$I->assertTrue($exists);

/**
* Wait for 3 seconds for the cache to expire
*/
sleep(3);

/**
* Get the data now from the cache - expired
*/
$data = $modelsCache->get('my-cache');
$I->assertNull($data);
}

/**
* Tests Phalcon\Mvc\Model :: find() - with cache/exception
*
Expand Down

0 comments on commit 8e17493

Please sign in to comment.