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

Question with cache

Hello, as explained here: https://docs.phalcon.io/en/latest/reference/cache.html#caching-output-fragments I've implemented this solution to cache my view fragments.

But, if I understand the workflow the right way, this only act in the view level, meaning that all queries called by controllers actions are, even if the cache file exist, executed. Am I right ?

If response is yes, is there a way to check in the controller itself the presence of the cache file and, if true, bypass all the controller action stuff ?

Thank you for help.



43.9k

Thank you, Assuming that I declare the viewCache in the DI, is it possible to define different cache directories like this:

    $di->set('viewCache', function($cache){

        //Cache data for one day by default
        $frontCache = new Phalcon\Cache\Frontend\Output(array(
            "lifetime" => 86400
        ));
        switch($cache) {
        case "cacheDay":
        //File backend settings
        $cacheDay = new Phalcon\Cache\Backend\File($frontCache, array(
            "cacheDir" => __DIR__."/../var/cache/day/",
        ));

        return $cacheDay;
       break;
       case "cacheProfile":
        $cacheProfile = new Phalcon\Cache\Backend\File($frontCache, array(
            "cacheDir" => __DIR__."/../var/cache/profile/",
        ));

        return $cacheProfile;
               break;

        }
    });

and then call the getCache function with parameter like this:

$key = 'day'.$dayId;
$exists = $this->view->getCache(array("cache"=>"cacheDay"))->exists($key);


43.9k

right now, above's code give me an "The injected caching service is invalid" error ....



98.9k

It seems the 'viewCache' isn't returning a valid object



43.9k

hey, thank you for help. Can you please tell me how to use the "service" parameter as shown here (in "downloadAction"): https://docs.phalcon.io/en/latest/reference/views.html#caching-view-fragments



98.9k

You need to set the view service in the 'cache' call:

$this->view->cache(array('service' => 'some-name', 'key' => 'some-key'));

$cache = $this->view->getCache();

In the DI you need to register each service:

//Cache data for one day by default
$frontCache = new Phalcon\Cache\Frontend\Output(array(
    "lifetime" => 86400
));

//Default Cache
$di->set('viewCache', function() use ($frontCache) {    
    return new Phalcon\Cache\Backend\File($frontCache, array(
        "cacheDir" => __DIR__."/../var/cache/default/",
    ));
});

//CacheDay service
$di->set('cacheDay', function() use ($frontCache){
    return new Phalcon\Cache\Backend\File($frontCache, array(
        "cacheDir" => __DIR__."/../var/cache/day/",
    ));     
});

//cacheProfile
$di->set('cacheProfile',  function() use ($frontCache) {
    return new Phalcon\Cache\Backend\File($frontCache, array(
        "cacheDir" => __DIR__."/../var/cache/profile/",
    ));
});


43.9k

Big thank, you rock