smarty-php / smarty-php/smarty

"Invalid compiled template for" thrown if write to cache fails

Open
#846 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
2.3k
Forks
709
PR merge metrics
No merged PRs in 30d

Description

When caching is enabled and entry is missing in cache, then smarty generates new content, stores it inside cache, and later reads it back to process it.

However, if storing to cache fails for some reason (or if the cache is cleaned before the read), smarty later throws exception

Invalid compiled template for ...

I see two problems here:

  1. reading back what we just wrote to cache is resource wasting, especially if the cache is remote (redis/memcached/db)
  2. caches (by definition) do not guarantee that item will be available after writing. Stored items can be purged at any time. In this context, smarty treats cache as a temporary storage, which is wrong.
Repro-code
/**
 * Simple NO-OP cache implementation. Stored data are immediately forgotten
 */
class CacheNoOP extends Smarty_CacheResource_Custom
{
    protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime)
    {
        $content = null;
        $mtime = null;
    }

    protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content)
    {
        return false;
    }

    protected function delete($name, $cache_id, $compile_id, $exp_time)
    {
        return 0;
    }

}

$smarty = new Smarty();
$smarty->registerCacheResource('noop', new CacheNoOp());
$smarty->caching_type = 'noop';
$smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;
$smarty->setCompileDir(__DIR__ . '/compile');
$smarty->setTemplateDir(__DIR__.'/tpl');

$smarty->fetch('whatever.tpl');

This is possibly related to #698

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the CacheNoOP resource and the Smarty fetch('whatever.tpl') entry point, then trace the cache miss, save, and subsequent read flow. Compare the behavior with the related issue #698; done means a failed or unavailable cache write does not produce an “Invalid compiled template” exception while processing the generated content.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.