Skip to content

Commit 8e17493

Browse files
authored
Merge pull request #16714 from niden-code/T16696-cache-lifetime
T16696 cache lifetime
2 parents a1e8d55 + e020540 commit 8e17493

File tree

7 files changed

+98
-6
lines changed

7 files changed

+98
-6
lines changed

CHANGELOG-5.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Fixed `Phalcon\Dispatcher\AbstractDispatcher` when calling action methods that do not define parameters to prevent `Unknown named parameter` error.
2424
- 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)
2525
- Fixed `Phalcon\Filter\Filter` to have the correct docblock for IDE completion
26+
- 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)
2627

2728
### Removed
2829

phalcon/Mvc/Model/Query.zep

+13-3
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ class Query implements QueryInterface, InjectionAwareInterface
262262
*/
263263
public function execute(array bindParams = [], array bindTypes = [])
264264
{
265-
var uniqueRow, cacheOptions, key, cacheService, cache, result,
266-
preparedResult, defaultBindParams, mergedParams, defaultBindTypes,
267-
mergedTypes, type, lifetime, intermediate;
265+
var adapter, cache, cacheLifetime, cacheOptions, cacheService,
266+
defaultBindParams, defaultBindTypes, intermediate, key, lifetime,
267+
mergedParams, mergedTypes, preparedResult, result, type, uniqueRow;
268268

269269
let uniqueRow = this->uniqueRow,
270270
cacheOptions = this->cacheOptions;
@@ -303,6 +303,16 @@ class Query implements QueryInterface, InjectionAwareInterface
303303
);
304304
}
305305

306+
/**
307+
* If the lifetime is different than the cache lifetime, assign
308+
* the cache lifetime to the current cache setting
309+
*/
310+
let adapter = cache->getAdapter();
311+
let cacheLifetime = adapter->getLifetime();
312+
if (lifetime !== cacheLifetime) {
313+
let lifetime = cacheLifetime;
314+
}
315+
306316
let result = cache->get(key);
307317

308318
if !empty result {

phalcon/Storage/Adapter/AbstractAdapter.zep

+10
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface
207207
*/
208208
abstract public function getKeys(string prefix = "") -> array;
209209

210+
/**
211+
* Returns the lifetime
212+
*
213+
* @return int
214+
*/
215+
public function getLifetime() -> int
216+
{
217+
return this->lifetime;
218+
}
219+
210220
/**
211221
* Returns the prefix
212222
*

tests/database/Db/Column/GetScaleCest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class GetScaleCest
2626

2727
public function _before(DatabaseTester $I)
2828
{
29-
$I->skipTest('Temporary disabled');
29+
$I->markTestSkipped('Temporary disabled');
3030
$this->setNewFactoryDefault();
3131
$this->setDatabase($I);
3232
}

tests/database/Mvc/Model/Behavior/SoftDeleteCest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function mvcModelBehaviorSoftDelete(DatabaseTester $I)
8383
public function mvcModelBehaviorSoftDeleteWithBeforeDeleteEvent(DatabaseTester $I)
8484
{
8585
$I->wantToTest('Mvc\Model\Behavior - SoftDelete() - with before delete event');
86-
$I->skipTest('See: https://github.com/phalcon/cphalcon/issues/14904');
86+
$I->markTestSkipped('See: https://github.com/phalcon/cphalcon/issues/14904');
8787

8888
/** ADD BeforeDelete event */
8989
$eventsManager = new EventManager();

tests/database/Mvc/Model/Behavior/TimestampableCest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function mvcModelBehaviorTimestampable(DatabaseTester $I)
8686
public function mvcModelBehaviorTimestampableWithBeforeCreateEvent(DatabaseTester $I)
8787
{
8888
$I->wantToTest('Mvc\Model\Behavior - Timestampable() - with before create event');
89-
$I->skipTest('See: https://github.com/phalcon/cphalcon/issues/14904');
89+
$I->markTestSkipped('See: https://github.com/phalcon/cphalcon/issues/14904');
9090

9191
/** ADD BeforeDelete event */
9292
$eventsManager = new EventManager();

tests/database/Mvc/Model/FindCest.php

+71
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use function ob_get_contents;
3535
use function ob_start;
3636
use function outputDir;
37+
use function sleep;
3738
use function uniqid;
3839
use function var_dump;
3940

@@ -216,6 +217,76 @@ public function mvcModelFindResultsetSecondIteration(DatabaseTester $I)
216217
}
217218
}
218219

220+
/**
221+
* Tests Phalcon\Mvc\Model :: find()
222+
*
223+
* @author Phalcon Team <team@phalcon.io>
224+
* @since 2020-02-01
225+
*
226+
* @group mysql
227+
* @issue 16696
228+
*/
229+
public function testMvcModelFindWithCacheLifetimeFromCacheService(DatabaseTester $I): void
230+
{
231+
/** @var PDO $connection */
232+
$connection = $I->getConnection();
233+
234+
$migration = new ObjectsMigration($connection);
235+
$migration->insert(1, 'random data', 1);
236+
237+
$options = [
238+
'defaultSerializer' => 'Json',
239+
'lifetime' => 2,
240+
'prefix' => 'data-',
241+
];
242+
243+
/**
244+
* Models Cache setup. Lifetime is 2 seconds
245+
*/
246+
$serializerFactory = new SerializerFactory();
247+
$adapterFactory = new AdapterFactory($serializerFactory);
248+
$adapter = $adapterFactory->newInstance('apcu', $options);
249+
$cache = new Cache($adapter);
250+
251+
$this->container->setShared('modelsCache', $cache);
252+
253+
/**
254+
* Get the records (should cache the resultset)
255+
*/
256+
$data = Objects::find(
257+
[
258+
'cache' => [
259+
'key' => 'my-cache',
260+
],
261+
]
262+
);
263+
264+
$I->assertEquals(1, count($data));
265+
266+
$record = $data[0];
267+
$I->assertEquals(1, $record->obj_id);
268+
$I->assertEquals('random data', $record->obj_name);
269+
270+
/**
271+
* Get the models cache
272+
*/
273+
$modelsCache = $this->container->get('modelsCache');
274+
275+
$exists = $modelsCache->has('my-cache');
276+
$I->assertTrue($exists);
277+
278+
/**
279+
* Wait for 3 seconds for the cache to expire
280+
*/
281+
sleep(3);
282+
283+
/**
284+
* Get the data now from the cache - expired
285+
*/
286+
$data = $modelsCache->get('my-cache');
287+
$I->assertNull($data);
288+
}
289+
219290
/**
220291
* Tests Phalcon\Mvc\Model :: find() - with cache/exception
221292
*

0 commit comments

Comments
 (0)