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.

How to serve phtml views with memcache

Hello, I use memcache with queries and is OK. Apart of this I want to cache my phtml views with memcache, but I don't know how :(

I started to save the views in a folder with this and works fine....

    $view->registerEngines([
        //'.phtml' => PhpEngine::class // así estaba antes y con lo de abajo aparecen las view en /tmp/cache
        '.phtml' => function($view, $di) {
            $phtml = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
            $phtml->setOptions(array(
                 "compiledPath" => BASE_PATH . '/tmp/cache/',
                 'compiledSeparator' => '_',
                 'compileAlways' => false,
            ));
            return $phtml;
        }
    ]);

...but I dom't know how to serve those pages saved into BASE_PATH . '/tmp/cache/', instead render the view again.

My code in services.php

use Phalcon\Cache\Frontend\Data as FrontendData;
use Phalcon\Cache\Backend\Memcache as BackendMemcache;

$di->setShared(
    'modelsCache',
    function () {
        $config = $this->getConfig();
        // Cache data for one day (default setting)
        $frontCache = new FrontendData(
            [
                'lifetime' => 86400
            ]
        );
        // Memcached connection settings
        $cache = new BackendMemcache(
            $frontCache,
            [
                'host' => 'localhost'
                'port' => '11211',
            ]
        );
        return $cache;
    }
);

With this the pages is rendering again and again.

Any clue, any idea?

Regards.



69.9k
Accepted
answer

You can control view caching from controller actions:

https://docs.phalcon.io/3.4/en/views#caching-view-fragments



1.1k

Yes it is works. I thought in something globally, but it's ok. Thx!