Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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"
                        )
                    ));
            }


49.7k
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.

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?

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?