Skip to content

template changes take some seconds to recompile with PHP5 opcache enabled. #72

Closed
@calguy1000

Description

@calguy1000

Problem:

With Smarty opcache enabled (PHP 5.5+) template changes take some seconds to be recompiled.  This can cause problems when using caching of varying sorts to cache the rendered output from a compiled page

Severity: MAJOR

Symptoms:

Making a small change to a smarty template, and then browsing to the page has no effect.  You must wait for a few seconds or continuously reload in order to see the change.

How to reproduce:

1.  Ensure that the PHP 5.5+ opcache is enabled in the php.ini (default settings otherwise)
    opcache.enable=1

2.  Browse to the supplied testcase script with a browser, and see the rendered page

3.  Save a small change in the templates/index.tpl file

4.  Immediately reload the browser page.

Problem Area:

sysplugins/smarty_template_compiled.php smarty_template_compiled::process()
on line 174 the compiled template is 'included' to check for validity and wether it must be 'recompiled'
if it must be recompiled it is recompiled and 'included' again (line 179)
because this is within the opcache revalidate frequency, even though the timestamp on the php file has changed
the include has no effect (the opcache optimizes this out).

Sample code

<?php
include_once('smarty-3.1.27/libs/Smarty.class.php');
define('SMARTY_COMPILE_DIR',getcwd().'/tmp/templates_c');
define('SMARTY_CACHE_DIR',getcwd().'/tmp/cache');

ini_set('opcache.revalidate_freq',10); // by default this is 2

$smarty = new Smarty();
$smarty->setCompileDir(SMARTY_COMPILE_DIR);
$smarty->setCacheDir(SMARTY_CACHE_DIR);
$smarty->addTemplateDir(getcwd().'/templates');
$smarty->addConfigDir(getcwd().'/configs');

$data = [];
$data['title'] = 'Test Smarty and PHP 5.5 opcache';
$data['subtitle'] = 'Template changes have no effect for some seconds.';
$smarty->assign('data',$data);
$conf = opcache_get_configuration();
$smarty->assign('conf',$conf['directives']);
$smarty->display('index.tpl');

Test template

<html>
<head>
  <title>{$data.title}</title>
</head>
<body>
  <h1 style="text-align: center;">{$data.title}</h1>
  <h2 style="text-align: center;">{$data.subtitle}</h2>
  <h3 style="color: orange; text-align: center;">Make a small change to {$smarty.template} and immediately reload this page.</h3>
  <p>some text aa</p>
  <p>some text bb</p>
  <p>some text cc</p>
  <p>some text</p>
  <p>some text</p>
  <p>some text</p>
  <p>some text</p>
  <fieldset>
    <legend>Opcache configuration</legend>
    <table style="width: 100%;">
    {foreach $conf as $key => $val}
      <tr>
        <td>{$key}</td>
    <td>{$val}</td>
      </tr>
    {/foreach}
    </table>
  </fieldset>
</body>
</html>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions