View cache not working

My code

use Phalcon\Mvc\View\Simple as SimpleView;
use Phalcon\Cache\Frontend\Json as CacheFrontend;
use Phalcon\Cache\Backend\Redis as CacheBackend;

$di->setShared('view', function () use ($config) {
    $view = new SimpleView();
    $view->setViewsDir($config->application->viewsDir);
    return $view;
});

$di->set('viewCache', function () use ($config) {

    $frontCache = new CacheFrontend(["lifetime" => 86400]);

    $cache = new CacheBackend(
        $frontCache,
        $config->redis->toArray()
    );

    return $cache;
});

$vars = ['foo' => 'bar'];
$this->view->cache([
"lifetime" => 86400,
"key"      => "prefix:key"
]);

echo $this->view->render("materials/templates/xyz.phtml", $vars);

Redis monitor

1448949074.138914 [0 127.0.0.1:39714] "SELECT" "0"
1448949074.139034 [0 127.0.0.1:39714] "GET" "_PHCRprefix:key"
1448949074.141211 [0 127.0.0.1:39714] "SET" "_PHCRprefix:key" "null"
1448949074.141335 [0 127.0.0.1:39714] "EXPIRE" "_PHCRprefix:key" "86400"
1448949074.141455 [0 127.0.0.1:39714] "SADD" "_PHCR" "prefix:key"

The content set into redis is always "null", why?


I tried to find reason in Phalcon source code.

phalcon/mvc/view/simple.zep

    public function render(string! path, params = null)
    {
        ...
        if typeof cache == "object" {
            if cache->isStarted() === true {
                if cache->isFresh() === true {
                    cache->save();
                } else {
                    cache->stop();
                }
            } else {
                cache->stop();
            }
        }

        ...
    }

phalcon/cache/backend/redis.zep

public function save(keyName = null, content = null, lifetime = null, boolean stopBuffer = true)
{
...
        if !content {
            let cachedContent = frontend->getContent();
        } else {
            let cachedContent = content;
        }

        /**
         * Prepare the content in the frontend
         */
        if !is_numeric(cachedContent) {
            let preparedContent = frontend->beforeStore(cachedContent);
        }
...
        if is_numeric(cachedContent) {
            let success = redis->set(lastKey, cachedContent);
        } else {
            let success = redis->set(lastKey, preparedContent);
        }

phalcon/cache/frontend/json.zep

class Json implements FrontendInterface
{
...
    public function getContent()
    {
        return null;
    }
...
}

Em......

Always return null... why?

Looks like it was not implemented. Investigating...

Nevermind, returning null is normal for the frontend cache. About your question, does the view "materials/templates/xyz.phtml" work and does it actually render something ?



557

Thanks for your anwser, but the view rendered correctly and no error or something else happend.

Nevermind, returning null is normal for the frontend cache. About your question, does the view "materials/templates/xyz.phtml" work and does it actually render something ?

Hi!

Any news? I'm having the same problem.

Thanks