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

Does not cache when view output is disabled

I have follow PHP action:

        public function bookAction() {
            $googleId = $this->dispatcher->getParam('googleId', 'string');

            $cacheKey = $googleId.'.png';
            if ($this->view->getCache()->exists($cacheKey)) {
                echo $this->viewCache->get($cacheKey);
                return false;
            }

            $image = $this->helpers->curlRequest('https://qwe.com');

            $this->view->cache(array(
                'key' => $cacheKey,
            ));

            $this->response->setHeader('Content-Type', 'image/png');
            return $this->response->setContent($image);
        }

And cache service:

    $di->set('viewCache', function () use ($di) {
        $cache = new Phalcon\Cache\Backend\File(
            new Phalcon\Cache\Frontend\Output(array(
                'lifetime' => 2592000,
            )),
            array(
                'cacheDir' => '../app/cache/pages/',
            )
        );

        return $cache;
    });

When there is no output (as in this case), phalcon does not cache the output.

(p.s. this code works fine when there is output)

Any ideas?

I think the problem is in your first if

    $cacheKey = $googleId.'.png';
    if ($this->view->getCache()->exists($cacheKey)) {
        // try this
        $this->viewCache->get($cacheKey);
        return $this->view;

        // or this
        $this->viewCache->get($cacheKey);
        return true;

        // or this
        echo $this->viewCache->get($cacheKey);
        return true;
    }

Good luck



14.3k

That's not the problem. Will explain much detail:

  1. When there is no cache yet, the program gets the actual data ($this->helpers->curl request())
  2. Return in the browser, BUT does NOT CACHE them.

I so think the problem is that I send data via "return $this->response - >setContent()", since I do not use views. Views I do not need, as return the picture.

How do I force phalcon to cache output in this case?