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

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: https://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.