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

Empty view cache file sometimes

I use file caching to cache the page output. Sometimes all the cache files are ok. Sometimes a few cache files are zero byte. These empty cache files are random (not consistently the same files).

I am using PHP 5.5.30 and Phalcon 2.0.9. The program is in development environment and I am the only user accessing the pages.

I cannot find similar problem in this forum or the internet so I think maybe I code my program wrongly. Thanks in advance for any suggestion.

Below are codes that I used to set up $di and the controller. The generated cache ID is a camel-cased file name without any folder name.

$di->set('viewCache', function() use ($config){
        //Cache data for one day by default
        $frontCache = new Phalcon\Cache\Frontend\Output(array(
            "lifetime" => 2592000
        ));
        //File backend settings
        $cache = new Phalcon\Cache\Backend\File($frontCache, array(
            "cacheDir" => $config->application->cacheDir,
            //"prefix" => "php"
        ));
        return $cache;
    });

/**
 * Setting up the view component
 */
$di->setShared('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $folder = $config->application->cacheDir.'compile/';
            if ( ! @file_exists($folder))
            {
                @mkdir($folder, 0777, TRUE);
            }

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                //'compiledPath' => $config->application->cacheDir,
                'compiledPath' => $folder,
                'compiledSeparator' => '_'
            ));

            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));

    return $view;
});
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\View;

class FunController extends Controller
{
  public function indexAction()
  {
      $pageCacheID = $this->generatePageCacheID();

      if(!($this->view->getCache()->exists($pageCacheID))) {
          //generate content
      }

      //Save page to cache
      $this->view->cache(array('key' => $pageCacheID, 'lifetime' => 86400));
  }
}


85.5k

hmm, my guess would be that, you got an error in the execution of the script, that's why cache is empty.

Maybe you can disable the cache and play with everything and try to break the application ?



958

Thanks for the sugggestion. That's also my guess. Maybe there is some problem with the controller. I even add in php error handler and monitor the apache error log. I tried breaking the errors and fix all errors but now I have no more errors to fix.

I make some progress in past 24 hours. It seems that my route setting is causing this problem. I modify the simple-subcontroller example (given by phalconPHP) and copy my controller over. My test program is testing the controller every 15 min and I am not getting any empty cache file for the past 8 hours.

I need to test for a few days to confirm.