Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Service 'modelsCache' wasn't found in the dependency injection container

I don't know how to add this service, I google it but there is nothing in result search.

Anyone helps me, please? Thanks in advance.

Edited:

my code:

$parameters["cache"] =  array('lifetime' => 3600, 'key' => 'my-find-key');
$abc= Abc::find($parameters);

http://docs.phalconphp.com/en/latest/reference/models-cache.html#caching-resultsets

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

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

    //Memcached connection settings
    $cache = new \Phalcon\Cache\Backend\Memcache($frontCache, array(
        "host" => "localhost",
        "port" => "11211"
    ));

    return $cache;
});


3.3k
edited Jul '14

Thank you for answering.

I used this:

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

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

        // http://docs.phalconphp.com/en/latest/reference/models-cache.html
        //Memcached connection settings
        // $cache = new \Phalcon\Cache\Backend\Memcache($frontCache, array(
        //     "host" => "192.168.1.44",
        //     "port" => "11211"
        // ));

        // http://docs.phalconphp.com/en/latest/reference/cache.html
        $cache = new \Phalcon\Cache\Backend\File($frontCache, array(
             "cacheDir" => "../app/cache/"
        ));

        return $cache;
});

But it doesn't work.

My function:

public function indexAction()
    {
        // $this->session->conditions = null;
        // $this->session->destroy();
        $numberPage = 1;
        if ($this->request->isPost()) {
            $query = Criteria::fromInput($this->di, "Nhachung", $_POST);
            $this->persistent->searchParams = $query->getParams();
        } else {
            $numberPage = $this->request->getQuery("page", "int");
            if ($numberPage <= 0) {
                $numberPage = 1;
            }
        }

        $parameters = array();
        if ($this->persistent->searchParams) {
            $parameters = $this->persistent->searchParams;
        }

        $parameters["cache"] =  array('lifetime' => 3600, 'key' => 'my-find-key123saf');

        $nhachung = Nhachung::find($parameters);
        if (count($nhachung) == 0) {
            $this->flash->notice("The search did not find any nhachung");
            return $this->forward("nhachung/index");
        }

        $paginator = new Phalcon\Paginator\Adapter\Model(array(
            "data" => $nhachung,
            "limit" => 10,
            "page" => $numberPage
        ));
        $page = $paginator->getPaginate();

        $this->view->setVar("page", $page);
    }

Url test: http://localhost/invo/nhachung/index?page=1

if I comment this line:

// $parameters["cache"] = array('lifetime' => 3600, 'key' => 'my-find-key123saf'); .

Everything works fine.

Chrome does not return anything "No data received". It's really difficult to debug with Phalcon :(.



3.3k

I'm so sorry, I don't know how to format codes.



3.3k
Accepted
answer

I fixed it,

Error with path: change: "cacheDir" => "../app/cache/" to "cacheDir" => "D:/www/invo/cache/volt"

I will try to find how to use relative path



3.3k

relative path: "cacheDir" => "../cache/volt/"

app cache public