We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Caching the Orm result question

Stupid question probably..When using a cache system to cache a resultset, does it's needed to explicitely ask the cache (here Redis) if the key exist before triggering the query or the framework do that automaticaly?

example, in my action to retrieve all clients: does this is enough:

    $clients = Clients::find(
            array(
                "cache" => array(
                    "key" => "clients-list"
                )
            ));

or do I need to write :


    $clients   = $this->di->cache->get("clients-list");
    if(!$clients){
            $clients = Clients::find(
                    array(
                        "cache" => array(
                            "key" => "clients-list"
                        )
                    ));
            }


79.0k
Accepted
answer
edited May '16

If you're satisfied with 3600 seconds (1 hour) as cache lifetime, then sure, first example is good.

Otherwise, provide lifetime and custom service which will be responsibile for ORM caching:

'cache' => ['lifetime' => 15 * 60 , 'key' => 'my-query-alias', 'service' => 'myOrmCacheService']  
//'modelsCache' is the default name for the models cache service

Note: you don't have to supply service key, since it will use default 'modelsCache'. I'm using my own service, thus I supply it's name in order to be fetched by IoC.

This is how Phalcon handles it by default:

Class Phalcon\Mvc\Model\Query will get result set from cache for you:

    let result = cache->get(key, lifetime);
                if result !== null {
                .... do it...
                }

So no, you don't have to call cache object manually.



11.6k

thank you for the clarification..magic is everywhere!

Question: have you tried with lifetime > 1 day (86400 seconds), does it work/refresh after that time?



11.6k

I'm using redis cache without lifetime, only manual cleaning when some operations are done that then need the cached query to be rebuild

Question: have you tried with lifetime > 1 day (86400 seconds), does it work/refresh after that time?