Question with cache

Hello, as explained here: http://docs.phalconphp.com/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.



32.6k

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


32.6k

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



86.1k

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



32.6k

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



86.1k

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


32.6k

Big thank, you rock