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

View cache with custom service

I'm trying to cache view with cache service. When I creating the cache view service with name viewCache, all works fine, but if the name of the service is != viewCache, I got the following error:

Phalcon\DI\Exception :
Service 'viewCache' was not found in the dependency injection container

In spite of I'm registers the cache as follow:

// the service registration
$di->set('myCustomView', function(){

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

    $cache = new Phalcon\Cache\Backend\Apc($frontCache, array(
        'prefix' => 'app-data'
    ));

    return $cache;
});

  // code inside controller
  $this->view->cache(array(
            "service"  => "myCustomView",
            "lifetime" => 10,
            "key"      => 'test')
        );

What I'm missing here? Phalcon 1.3.3 + PHP 5.6.1 + FPM



98.9k

Try adding a backtrace to see where the exception is being generated.

///...
} catch (Exception $e) {
    echo $e->getTraceAsString();
}


19.1k
edited Oct '14

Ok, this is my index.php

<?php
error_reporting(E_ALL);
try {

    $config = new \Phalcon\Config\Adapter\Ini(__DIR__ . "/../app/config/config.ini");
    include __DIR__ . "/../app/config/loader.php";
    include __DIR__ . "/../app/config/services.php";
    include __DIR__ . "/../app/config/elements.php";
    include __DIR__ . '/../app/library/utils/ResourceHelpers.php';
    include __DIR__ . "/../app/library/geoip/geoip.inc";

    $application = new \Phalcon\Mvc\Application($di);
    echo $application->handle()->getContent();

} catch (\Exception $e) {
    echo get_class($e), " : <br><span style='color:red'>";
    echo $e->getMessage(), "</span><br><hr>";
    echo " File : ", $e->getFile(), "<br>";
    echo " Line : ", $e->getLine(), "<br><pre style='color:blue'>";
    echo $e->getTraceAsString(), '</pre>';
    echo $e->getMessage();
}

and this is a exception

Phalcon\DI\Exception : 
Service 'viewCache' was not found in the dependency injection container
File : /home/dima/PhpstormProjects/panetng/app/controllers/get/IndexController.php
Line : 10
#0 [internal function]: Phalcon\DI->getShared('viewCache')
#1 [internal function]: Phalcon\Mvc\View->_createCache()
#2 /home/dima/PhpstormProjects/panetng/app/controllers/get/IndexController.php(10): Phalcon\Mvc\View->getCache()
#3 [internal function]: Panetng\Controllers\Get\IndexController->indexAction()
#4 [internal function]: Phalcon\Dispatcher->dispatch()
#5 /home/dima/PhpstormProjects/panetng/public/index.php(16): Phalcon\Mvc\Application->handle()
#6 {main}
Service 'viewCache' was not found in the dependency injection container


98.9k

Did you check this line? /home/dima/PhpstormProjects/panetng/app/controllers/get/IndexController.php(10)



19.1k
edited Oct '14

This is the action where I calling the cache service


<?php
namespace Panetng\Controllers\Get;

class IndexController extends GetControllerBase
{

    public function indexAction()
    {
        // Next row is line 10 
        $exists = $this->view->getCache()->exists('test');
        if (!$exists) $this->view->setTemplateAfter('main');
        $this->view->cache(array(
            "service" => "myViewCache",
            "key"     => 'test'));

    }
}


98.9k

When you call:

$exists = $this->view->getCache()->exists('test');

It still does not know which service to use, that's why it's trying to access the default service.



19.1k

So how can I specify which cache service to use? I tryed this code:


        $exists = $this->view->getCache()->exists(array("service" => 'myViewCache', "key" => 'test'));

but got same error btw, this is how I registergin the service in DI

$di->set('myViewCache', function () {
    return new Phalcon\Cache\Backend\Apc(new Phalcon\Cache\Frontend\Output(array("lifetime" => 2)), array('prefix' => 'app-data'));
});


98.9k

This must work:

$this->view->cache(array(
            "service" => "myViewCache",
            "key"     => 'test'
));

$exists = $this->view->getCache()->exists('test');
if ($exists) {
    $this->view->setTemplateAfter('main');
} else {
    $this->view->cache(false);
}


19.1k
Accepted
answer

If I using the else statement like you spesifyed I getting broken page

/...
 else {
    $this->view->cache(false);
}

This is what works for me:


    public function indexAction()
    {
        $this->view->cache(array(
            "service" => "myViewCache",
            "key"     => 'test'
        ));

        $exists = $this->view->getCache()->exists('test');
        if (!$exists) {
            $this->view->setTemplateAfter('main');
        } else {
            $this->view->cache(true);
        }
    }

Thanks for help, you guys making great framework!