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

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.



77.7k
Accepted
answer

You can control view caching from controller actions:

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



3.1k

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