views:

86

answers:

3

Hello,

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.

A: 

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.

Ronn0
@Ronn0, thank you for your answer. Is there any ways to set cache sub-directory, while `$smarty->cache_dir` is set to single directory (`$smarty->cache_dir = '/path/to/cache'`). It will solve huge load of problems, I think. I know that if `$smarty->use_sub_dirs = true` we can define path to cache directly in cache_id (`/user/123/`), but Smarty will also add it's own additional sub-directories to our cache_id, so in result we'll be getting something like (`/user/123/%12/%23/%45/`). Do you know how to avoid it?
Kirzilla
Ronn0
@Ronn0: Smarty does both kind of caching, if you enable them. You can cache the compiled templates and the final output too.
djn
@djn didn't know that, thanks!
Ronn0
A: 

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.

FractalizeR
A: 

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

djn