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

Extending cache lifetime

Hi, I have a question about if and how it is possibile to extend cache lifetime of view. Supposing to have defined a cache in services:

$frontCache = new Phalcon\Cache\Frontend\Output(array(
        "lifetime" => 120
));

//File backend settings
$cache = new Phalcon\Cache\Backend\File($frontCache, array(
        "cacheDir" => __DIR__."/../cache/",
        "prefix" => "php-"
));

and supposing to cache a content with:

$this->view->cache(array("lifetime" => 300, "key" => 'hp'));

I'd like to know if it is possibile to extend the duration of 300 seconds. In good situation after 300 seconds I should create the new content quering backend but the query could fail and in this situation I 'd like to use the old content of cache. Is it possible? Thanks



79.0k
Accepted
answer
edited Oct '16
   // Cache this view for 1 hour
        $this->view->cache(
            [
                'lifetime' => 3600,
                'key'      => 'test'
            ]
        );

If you init this over and over again, it will extend it each time.



2.0k

Hi Jonathan so if I understand well when the cache is expired and the connection to backend fails I can use the old content of cache, extending for other 5 minutes, with this simple code:

$this->view->cache( [ 'lifetime' => 300, 'key' => 'hp' ] );

Is it correct?



2.0k
edited Oct '16

Other question. Is it true also for models cache?

I have this modelscache in services:

$di->set('modelsCache', function() {

$frontCache_apc = new \Phalcon\Cache\Frontend\Data(array(
        "lifetime" => 120
));

$cache_apc = new Phalcon\Cache\Backend\Apc ($frontCache_apc, array(
        'prefix' => 'apccache-'
));

return $cache_apc;
});

How can I extend content apc cache with lifetime ended?