We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

Querying model cache

I have set up and can successfully set a model query in a cache file using:

app\config\services.php

$di->set('modelsCache', function() use ($config) {
    //Cache data for one hour by default
    $frontCache = new Phalcon\Cache\Frontend\Data(array(
        "lifetime" => 3600
    ));
    //backend params
    $backCache = array(
        'cacheDir' => $config->application->cacheDir
    );
    //backend cache
    $cache = new Phalcon\Cache\Backend\File($frontCache, $backCache);
    return $cache;
});

In a controller this saves a cache file in the cache directory with a name specified.

    $agent = Agents::findFirst(array(
            "conditions" => "first_name = :first: AND last_name = :last: AND active =1",
            "bind"       => array("first" => $first_name, "last" => $last_name),
            'cache'      => array('key' => $cache_key , 'lifetime' => 7200)
        ));
    };

Following this in the docs: http://docs.phalcon.io/en/latest/reference/cache.html#file-backend-example

I tried to retireve the cache with:

 try {
        $agent = $this->getDI()->getService('modelsCache')->get($cache_key);
    } catch (\Exception $e) {
        die($e->getMessage());
    }

But get blank screen. Any suggestions or does the framework try to retrieve the query automatically?



4.7k
Accepted
answer

Through trial and error I realized the query is retrieved automatically. Sorry I should have done my own research.