Phalcon models cache "lifetime" hardcoded in query object
Phalcon models cache lifetime hardcoded in query object = 3600
Expected scenario: if not set "lifetime" in model options cache use "lifetime" option from current frontend adapter (if exists)
Details
- Phalcon version: (3.0.4)
Hi,
By default (according to docs) \Phalcon\Cache\BackendInterface returns the frontend's lifetime when lifetime is set to NULL (example here); so changing here to NULL would cause this to use the frontend's default lifetime.
Regards, Federico.
Edit: I manually set all the query lifetimes when I need them because of this. Edit2: fixed solution.
Hi,
My git skills are quite lowsy.
I've written and tested the fix and works fine.
- The test file uses
sqlitewith:memory;db, and it's created on runtime. - The test file is OS independent
patch to fix this:
cphalcon/phalcon/mvc/model$ git branch
* (HEAD detached at v3.2.2)
...
cphalcon/phalcon/mvc/model$ git diff query.zep
diff --git a/phalcon/mvc/model/query.zep b/phalcon/mvc/model/query.zep
index 0ad8a5cb4..e25675961 100644
--- a/phalcon/mvc/model/query.zep
+++ b/phalcon/mvc/model/query.zep
@@ -3328,7 +3328,7 @@ class Query implements QueryInterface, InjectionAwareInterface
* By default use use 3600 seconds (1 hour) as cache lifetime
*/
if !fetch lifetime, cacheOptions["lifetime"] {
- let lifetime = 3600;
+ let lifetime = null;
}
/**
self testing file:
<?php
use Phalcon\Di;
use Phalcon\Db\Adapter\Pdo\Sqlite as SqliteAdapter;
use Phalcon\Cache\Frontend\Data as FrontendCache;
use Phalcon\Cache\Backend\File as BackendCache;
use Phalcon\Mvc\Model;
class Test extends Model {
public $id;
public $number;
public function initialize() {
$this->skipAttributes(['id']);
}
}
// Dependencies
$di = new Di();
$di->setShared('modelsManager', '\Phalcon\Mvc\Model\Manager');
$di->setShared('modelsMetadata', 'Phalcon\Mvc\Model\MetaData\Memory');
$di->setShared('db', function() {
$db = new SqliteAdapter(['dbname' => ':memory:']);
$db->execute("CREATE TABLE `test` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `number` INTEGER);");
return $db;
});
$di->setShared('modelsCache',
function () {
// Cache data for 2 seconds
return new BackendCache(new FrontendCache(['lifetime' => 2]),
['cacheDir' => rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR]);
}
);
// Create random model value to cache
$t = new Test();
$t->number = rand(1, 1000);
$t->save();
echo "Inserted-> id: {$t->id}, number: {$t->number}" . PHP_EOL;
// Set query parameters with cache key only
$parameters = [
"id = {$t->id}",
'cache' => [
'key' => "test_{$t->id}",
],
];
echo "Frist find isFresh: " . (int)Test::find($parameters)->isFresh() . PHP_EOL;
echo "Second find isFresh: " . (int)Test::find($parameters)->isFresh() . PHP_EOL;
//Make sure cache expires.
sleep(3);
echo "Third find isFresh: " . (int)Test::find($parameters)->isFresh() . PHP_EOL;
First I tried to test it with \Phalcon\Cache\Backend\Memory but lifetime is disregarded and it never expires, I'll write a patch for that on another issue.
@sergeyklay Will you be kind and add the lifetime fix on query?
Thanks! Federico.
@kornerita
but lifetime is disregarded
Because those methods which have in Zephir something like:
int lifetime = null
will cast lifetime to 0 in the method body. Actually there is a trick to use:
int lifetime = -1
and check in the method body:
// User passed the lifetime arg via method call
if lifetime >= 0
@sergeyklay
Nice trick, when I wrote this I wasn't very fluent in Zephyr, later I've found that typed variables can't have null values, I'll add it to my trick list :)
Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues