Question

I'd like to go slightly deeper into Smarty caching, so I have some simple questions...

  1. To manipulate cache invalidation I want to know what directory Smarty is storing it's cache in. For example, all cached pages related to user_id=123 I want to store at cache/users/123/. Where cache is smarty caching dir. How can I tell smarty to store cache related to user_id=123 at cache/users/123/? Will Smarty store cache of sub-templates in this directory also?

  2. Is there any recommendations about cleaning cache in this directory? I think that simply removing files from this directory can cause some errors if some visitors are currently visiting this pages (Error can occure when smarty will see that template cache is found, but sub-template cache isn't found because it was already removed, for example).

Any recommendations and advices are appreciated.

Thank you.

Was it helpful?

Solution

Take a look at the Smarty docs: the "Cache Groups" section. Does exactly that.

OTHER TIPS

You should NOT clear cache manually. Use clear_cache() and clear_compiled_tpl() for that. You can clear all cache and also do selective clearing with them.

Use can switch the smarty cachedir, depending on where you want is (you can check that with some own if-statements)

// Create smarty object
$smarty = new Smarty();

// Change smarty-dir is like this:
if (isset($_GET['userId'])) {
    $smarty->compile_dir = '/path/to/dir/' . $_GET['userId'];
} else {
    $smarty->compile_dir = '/path/to/dir/default';
}

Point two is a little harder, you have to foreach through all mapps, and assign the new compile dir, and then run the following command:

$smarty->clear_cache();

But as you said, it's possible the file is requested while there is a remove. :-(


But it's not recommended to use different cache folders, Smarty doesn't cache the output, just the compiled PHP-file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top