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

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

https://docs.phalcon.io/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;
});


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

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

        // https://docs.phalcon.io/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: https://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 :(.



5.3k

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



5.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



5.3k

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

app cache public